Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Istruzione Switch | Strutture di Controllo
Introduzione a Golang

bookIstruzione Switch

L'istruzione switch offre un modo pratico per confrontare un'espressione con più valori. Di seguito è riportata la sintassi di base dell'istruzione switch:

switch (expression) {
    case value1: {
        // Code to be executed if the expression equals value1
    }
    case value2: {
        // Code to be executed if the expression equals value2
    }
    case ...
    ...
    default: {
        // Code to be executed if the expression doesn't match any of the values
    }
}

Ecco alcuni punti importanti riguardo l'istruzione switch:

  • L'expression viene valutata una sola volta e il suo valore viene confrontato con i valori specificati in ciascun case;
  • Il blocco di codice sotto il case corrispondente viene eseguito e i restanti casi vengono ignorati;
  • Se nessun case corrisponde, viene eseguito il blocco di codice sotto default. Il caso default è facoltativo e può essere omesso.

Di seguito un esempio di come switch può essere utilizzato in un programma:

index.go

index.go

copy
12345678910111213141516171819202122232425262728
package main import "fmt" func main() { // Let's create a program that prints a message based on the day of the week. dayOfWeek := 5 // Assuming it's Friday switch dayOfWeek { case 1: fmt.Println("It's Monday! Start the week with enthusiasm.") case 2: fmt.Println("It's Tuesday! Keep pushing forward.") case 3: fmt.Println("It's Wednesday! Halfway through the week.") case 4: fmt.Println("It's Thursday! Almost there, don't give up.") case 5: fmt.Println("It's Friday! Time to celebrate the weekend.") case 6: fmt.Println("It's Saturday! Enjoy your day off.") case 7: fmt.Println("It's Sunday! Relax and recharge for the week ahead.") default: fmt.Println("Invalid day of the week.") } }

Nota

È possibile omettere le parentesi graffe per i blocchi di codice case.

question mark

Come si avvia una switch statement in Go?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 5

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you show me a complete example of a switch statement in JavaScript?

What happens if I forget to include a break statement in a case?

Can you explain when to use a switch statement instead of if-else?

Awesome!

Completion rate improved to 1.96

bookIstruzione Switch

Scorri per mostrare il menu

L'istruzione switch offre un modo pratico per confrontare un'espressione con più valori. Di seguito è riportata la sintassi di base dell'istruzione switch:

switch (expression) {
    case value1: {
        // Code to be executed if the expression equals value1
    }
    case value2: {
        // Code to be executed if the expression equals value2
    }
    case ...
    ...
    default: {
        // Code to be executed if the expression doesn't match any of the values
    }
}

Ecco alcuni punti importanti riguardo l'istruzione switch:

  • L'expression viene valutata una sola volta e il suo valore viene confrontato con i valori specificati in ciascun case;
  • Il blocco di codice sotto il case corrispondente viene eseguito e i restanti casi vengono ignorati;
  • Se nessun case corrisponde, viene eseguito il blocco di codice sotto default. Il caso default è facoltativo e può essere omesso.

Di seguito un esempio di come switch può essere utilizzato in un programma:

index.go

index.go

copy
12345678910111213141516171819202122232425262728
package main import "fmt" func main() { // Let's create a program that prints a message based on the day of the week. dayOfWeek := 5 // Assuming it's Friday switch dayOfWeek { case 1: fmt.Println("It's Monday! Start the week with enthusiasm.") case 2: fmt.Println("It's Tuesday! Keep pushing forward.") case 3: fmt.Println("It's Wednesday! Halfway through the week.") case 4: fmt.Println("It's Thursday! Almost there, don't give up.") case 5: fmt.Println("It's Friday! Time to celebrate the weekend.") case 6: fmt.Println("It's Saturday! Enjoy your day off.") case 7: fmt.Println("It's Sunday! Relax and recharge for the week ahead.") default: fmt.Println("Invalid day of the week.") } }

Nota

È possibile omettere le parentesi graffe per i blocchi di codice case.

question mark

Come si avvia una switch statement in Go?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 5
some-alt