Switch-Case statementSwitch-Case statement

Switch-Case statement

When we have many conditions to check, using multiple if-else statements may not be convenient.




The code looks confusing, but we can make it more readable. For such cases, we can use the switch-case statement.

Switch - case statement

The switch-case statement consists of several parts:



A switch-case statement is a construct that allows you to execute a block of code based on the value of a variable. The variable is called the switch variable. The switch variable is evaluated once, and the corresponding block of code is executed.



  • In this example, the switch variable is dayOfWeek. The switch variable is evaluated once, and the corresponding code block is executed.
  • If one of the conditions is met, then after executing its code block, the subsequent conditions will not be checked, and the code blocks of those conditions will not be executed either.
  • In this case, the code block for dayOfWeek is "Friday". If the value of dayOfWeek does not match any of the cases, the default code block is executed. In this case, the default code block is "Weekend".


What is default?

Select the correct answer

Everything was clear?

Section 3. Chapter 4