09 - Switch Statement
09 - Switch Statement
09 - Switch Statement
Programming
Fundamentals
Introduction to Switch Statement
// break condition
if (number < 0) {
break;
}
Important Points
The expression provided in the switch should result in a constant value otherwise it would not be valid.
The default statement is optional. Even if the switch case statement do not have a default
statement, it would run without any problem.
The break statement is used inside the switch to terminate a statement sequence.
Nesting of switch statements are allowed, which means you can have switch statements
inside another switch.
Switch statements are limited to integer values only in the check condition.
Switch Statement Flowchart
Apart from
supporting • Simple calculator configuration
programming • Selections of available food items at restaurants
tasks, the switch • Architectural services
• Shipping services, especially for retail businesses
case flowchart has • Hospitality services with the help of tracking
a wide range of customers to provide essential services
other applications,
such as:
Flowchart
switch (x)
{
case 1:
cout << "Choice is 1";
break;
Simple case 2:
example of C+ cout << "Choice is 2";
break;
+ program to
case 3:
demonstrate cout << "Choice is 3";
syntax of break;
default:
switch. cout << "Choice other than 1,
2 and 3";
break;
}
return 0;
}
The Break Keyword
A break can save a lot of execution time because it "ignores" the execution of all the rest of the code
in the switch block.
int day = 4;
switch (day) {
case 6:
The Default cout << "Today is Saturday";
break;
Keyword case 7:
cout << "Today is Sunday";
The default keyword specifies some code to
run if there is no case match as shown in the break;
given example.
default:
cout << “Today is Weekend";
}
Switch Exercises 01
int day = 2;
switch (____) { • Write C++ program to print number of days in a month.
• Write C++ program to print day of week name.
____ 1:
• Write C++ program to create calculator.
cout << "Saturday";
• Write C++ program to check even or odd number.
break;
• Write C++ program to check vowel or consonant.
____ 2:
• Write C++ program to print gender (Male/Female) program
cout << "Sunday"; according to given M/F.
______ ; • Write C++ Program to find maximum number.
}
Switch Exercises 02
• 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 = Rs. 200
• French Fries= Rs. 50
• Pizza= Rs. 500
• Sandwiches= Rs. 150
Switch Exercises 03
Program to take the value from the user as input all sides of a triangle and
check whether the triangle is valid or not.
Program to check whether the triangle is an equilateral, isosceles or scalene
triangle.
Switch Exercises 05
C++ Program to take the value from the user as input electricity unit
charges and calculate total electricity bill according to the given
condition:
An
For the For the For the For unit additional
first 50 next 100 next 100 above 250 surcharge
units Rs. units Rs. units Rs. Rs. of 20% is
0.50/unit 0.75/unit 1.20/unit 1.50/unit added to
the bill
Switch Exercises 06
Program to take the hours and minutes as input by the user and the show that
whether it is AM or PM by using the switch statement.
Program to convert a positive number into negative number and negative number
into a positive number using switch statement.
Write a program to swap the values of two numbers if the values of both variables
are not the same using a switch statement.
Program to Convert even number into its upper nearest odd number Switch
Statement.
End of Switch Statement
Introduction to
Break Keyword Default Keyword
Switch Statement