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.
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.
switch
case 1
True
False
statements
break;
out of switch
case 2
True
False
statements
break;
default
default
statements
.
.
.
Let’s see an example on
switch statement
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.
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.
It’s the time!! Let’s make
Calculator

What is Switch Case?

  • 1.
    Switch statement isa 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: //defaultstatements } 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.
  • 3.
    switch case 1 True False statements break; out ofswitch case 2 True False statements break; default default statements . . .
  • 4.
    Let’s see anexample on switch statement
  • 5.
    6 important pointson 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 statementis 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.
  • 7.
    It’s the time!!Let’s make Calculator