Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Switch-lause | Ohjausrakenteet
Johdatus Golangiin

bookSwitch-lause

switch-lause tarjoaa kätevän tavan verrata lauseketta useisiin arvoihin. Alla on switch-lauseen perussyntaksi:

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
    }
}

Tässä on joitakin tärkeitä huomioita switch-lauseesta:

  • expression arvioidaan kerran, ja sen arvoa verrataan jokaiseen case-arvoon;
  • Vastaavan case-lohkon koodi suoritetaan, ja loput tapaukset ohitetaan;
  • Jos mikään case ei täsmää, suoritetaan default-lohkon koodi. default-tapaus on valinnainen ja sen voi jättää pois.

Alla on esimerkki siitä, miten switch-lausetta voidaan käyttää ohjelmassa:

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.") } }

Huom

Voit jättää aaltosulkeet pois case-koodilohkoista.

question mark

Miten switch-lause käynnistetään Go:ssa?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 5

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 1.96

bookSwitch-lause

Pyyhkäise näyttääksesi valikon

switch-lause tarjoaa kätevän tavan verrata lauseketta useisiin arvoihin. Alla on switch-lauseen perussyntaksi:

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
    }
}

Tässä on joitakin tärkeitä huomioita switch-lauseesta:

  • expression arvioidaan kerran, ja sen arvoa verrataan jokaiseen case-arvoon;
  • Vastaavan case-lohkon koodi suoritetaan, ja loput tapaukset ohitetaan;
  • Jos mikään case ei täsmää, suoritetaan default-lohkon koodi. default-tapaus on valinnainen ja sen voi jättää pois.

Alla on esimerkki siitä, miten switch-lausetta voidaan käyttää ohjelmassa:

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.") } }

Huom

Voit jättää aaltosulkeet pois case-koodilohkoista.

question mark

Miten switch-lause käynnistetään Go:ssa?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 5
some-alt