Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Switch Statement | Control Structures
Introduction to GoLang

bookSwitch Statement

The switch statement provides a convenient way to compare an expression against multiple values. Below is the basic syntax of the switch statement:

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

Here are some important points regarding the switch statement:

  • The expression is evaluated once, and its value is compared against the values specified in each case;
  • The block of code under the matching case is executed, and the remaining cases are ignored;
  • If no case matches, the code block under default is executed. The default case is optional and can be left out.

Here is an example of how switch can be used in a program:

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

Note

You can omit braces for the case code blocks.

question mark

How is a switch statement initiated in Go?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookSwitch Statement

The switch statement provides a convenient way to compare an expression against multiple values. Below is the basic syntax of the switch statement:

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

Here are some important points regarding the switch statement:

  • The expression is evaluated once, and its value is compared against the values specified in each case;
  • The block of code under the matching case is executed, and the remaining cases are ignored;
  • If no case matches, the code block under default is executed. The default case is optional and can be left out.

Here is an example of how switch can be used in a program:

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

Note

You can omit braces for the case code blocks.

question mark

How is a switch statement initiated in Go?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 5
some-alt