0% found this document useful (0 votes)
17 views

Lesson 9 Switch Statement

Uploaded by

j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lesson 9 Switch Statement

Uploaded by

j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

SWITCH STATEMENT

WHAT IS A SWITCH STATEMENT?

❖ Switch statements are another way of handling decision making in your program.

❖ Switch statements work just like if-else statements.

❖ The reason when they are used is when you have a bunch of if else if statements and it becomes
difficult to read then you it change your code to use Switch statements.

❖ A switch statement allows a variable to be tested for equality against a list of values.

❖ It is a multiple choice selection statement.

❖ It will be executed according to user choice.


HOW DOES A SWITCH STATEMENT WORKS
IN C ++ PROGRAM?

1. The switch expression is evaluated once.


2. The value of the expression is compared with the values of each case.
3. If there is a match ,the associated block of code is executed.
4. If there is no match, the default block of code is executed.
FLOWCHART OF SWITCH STATEMENT
SYNTAX: HOW TO WRITE SWITCH STATEMENT?
RULES OF SWITCH STATEMENT

• An expression must always execute to a result.


• Case labels must be constants and unique.
• Case labels must end with a colon ( : ).
• A break keyword must be present in each case.
• There can be only one default label.
• We can nest multiple switch statements.
• The expression used in switch must be integral type ( int, char and enum). Any other type of
expression is not allowed.
SAMPLE CODE 1:
SAMPLE CODE 2:
SAMPLE CODE 1:
LET’S EXECUTE :
SOLVE THE FOLLOWING PROBLEMS IN C++ PROGRAM USING SWITCH
STATEMENT.
1. Write a C++ program to print day of week name with your emotion each day using switch case statement.

2. Write a C++ program to assess your weight status during the quarantine period using switch statement.

You might also like