Switch Case Statement

 The switch statement in C is used to make decisions based on the value of an expression. 

It's like having a menu of options, and depending on the value of the expression, the program chooses which option to execute.


We have a variable day that represents the day of the week (1 for Monday, 2 for Tuesday, and so on).

The switch statement evaluates the value of day.

Depending on the value, it executes the corresponding case. Each case represents a different option.

If none of the case values match the value of the expression (day in this case), the default case is executed (similar to an "else" statement in an if-else ladder).

The break statement is used to exit the switch statement after a case is executed. This prevents fall-through, meaning it stops the execution from continuing into the next case.


In this example, if day is 3, the program will print "It's Wednesday. Halfway through the week!" because that's the corresponding case for the value 3. If day were 8, it would print "Invalid day. Please enter a number between 1 and 7." because there is no case for 8, and the default case would be executed.

Post a Comment

0 Comments