Оператор Switch
switch оператор надає зручний спосіб порівняти вираз із кількома значеннями. Нижче наведено базовий синтаксис оператора 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
}
}
Ось кілька важливих моментів щодо оператора switch:
expressionобчислюється один раз, і його значення порівнюється зі значеннями, вказаними в кожномуcase;- Блок коду під відповідним
caseвиконується, а решта випадків ігноруються; - Якщо жоден
caseне збігається, виконується блок коду підdefault. Випадокdefaultє необов'язковим і може бути пропущений.
Нижче наведено приклад використання switch у програмі:
index.go
12345678910111213141516171819202122232425262728package 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.") } }
Примітка
Дужки для блоків коду
caseможна опускати.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
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
Оператор Switch
Свайпніть щоб показати меню
switch оператор надає зручний спосіб порівняти вираз із кількома значеннями. Нижче наведено базовий синтаксис оператора 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
}
}
Ось кілька важливих моментів щодо оператора switch:
expressionобчислюється один раз, і його значення порівнюється зі значеннями, вказаними в кожномуcase;- Блок коду під відповідним
caseвиконується, а решта випадків ігноруються; - Якщо жоден
caseне збігається, виконується блок коду підdefault. Випадокdefaultє необов'язковим і може бути пропущений.
Нижче наведено приклад використання switch у програмі:
index.go
12345678910111213141516171819202122232425262728package 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.") } }
Примітка
Дужки для блоків коду
caseможна опускати.
Дякуємо за ваш відгук!