0% found this document useful (0 votes)
2 views11 pages

5 - Switch Statement

The document explains the switch statement in programming, highlighting its ease of use compared to the if...else..if ladder. It describes how the switch statement evaluates an expression and executes corresponding statements based on case labels, including a default case if no match is found. Additionally, it includes a practical assignment to create a C program using the switch statement to display a food menu and calculate total charges based on user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

5 - Switch Statement

The document explains the switch statement in programming, highlighting its ease of use compared to the if...else..if ladder. It describes how the switch statement evaluates an expression and executes corresponding statements based on case labels, including a default case if no match is found. Additionally, it includes a practical assignment to create a C program using the switch statement to display a food menu and calculate total charges based on user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

The Case

Control
Structure
Lesson 2- Final Term
The Case Control
Structure

• The switch statement allows us to


execute one code block among many
alternatives.

• You can do the same thing with the


if...else..if ladder. However, the syntax of
the switch statement is much easier to
read and write.
The Case Control
Structure Syntax
How does the switch
statement work?
• The expression is evaluated once and
compared with the values of each case label.

• If there is a match, the corresponding


statements after the matching label are
executed. For example, if the value of the
expression is equal to constant2, statements
after case constant2: are executed until break
is encountered.

• If there is no match, the default statements are


executed.
How does the switch
statement work?
• The expression is evaluated once and
compared with the values of each case label.

• If there is a match, the corresponding


statements after the matching label are
executed. For example, if the value of the
expression is equal to constant2, statements
after case constant2: are executed until break
is encountered.

• If there is no match, the default statements are


executed.
How does the switch
statement work?
Example 1
Example 2
References

• https://www.programiz.com/c-
programming/c-switch-case-statement
Quiz Next Week
Seatwork
Create a C Program
○ Using Switch statement, write a program that displays the
following menu for the food items available to take order from
the customer:
• B= Burger
• F= French Fries
• P= Pizza
• S= Sandwiches
The program inputs the type of food and quantity. It finally
displays the total charges for the order according to following
criteria:
• Burger = Php200
• French Fries= Php50
• Pizza= Php500
• Sandwiches= Php150

You might also like