1. Switch statement is a conditional statement, that allows us to
execute one block of code when the case matches.
What is switch statement?
You can do same thing with the if else if ladder. But switch seems to
be more simple. And does not complex overall program.
2. switch(expression)
{
case constant1:
//statements
break;
case constant2:
//statements
break;
default:
//default statements
}
Syntax of switch
1. switch is the keyword used to define switch statement
2. expression is any constant expression.
3. case is the keyword used to define different cases.
4. break is used to terminate the flow of execution after the
statements have been executed.
5. default is optional runs when none of them is executed.
5. 6 important points on switch case
1. The expression in the switch has to be evaluated as a constant value, so that
it can be compared with the case labels as an exact match.
2. The expression passed in the switch has to be an integral constant or a
character value only.
3. Break statement in the switch is used to terminate the sequence of execution.
6. 5. Default statement is optional; it runs when there is no match with all the
cases.
4. Break is optional, you should include break after each case at the end of the
statement, if there will be no break all of the cases run after the match.
6. Nesting of switch can be done, which means switch under switch but we avoid
it as it makes the switch statement and program complex.