What is Switch Case?

AnuragSrivastava272 630 views 7 slides Jun 22, 2020
Slide 1
Slide 1 of 7
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7

About This Presentation

This PPT is about the Switch Case, if you want to see in detail visit the Be a Programmer channel on YouTube.


Slide Content

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.

s witch case 1 True F alse s tatements b reak; o ut of switch case 2 True F alse s tatements b reak; 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