Switch Statement
Designed & Prepared by Mr. Kamran Ahmad Awan
© Common Creative License 4.0
Programming
Fundamentals
Kamran Ahmad Awan
Department of Information
Technology
University of Haripur
Department of Information Technology – The University of haripur
2
Introduction to Switch Statement
Designed & Prepared by Mr. Kamran Ahmad Awan
Switch case statements are a substitute for long if
© Common Creative License 4.0
statements that compare a variable to several
integral values.
It provides an easy way
Switch is a control
The switch statement is to dispatch execution to
statement that allows a
a multiway branch different parts of code
value to change control
statement. based on the value of
of execution.
the expression.
Department of Information Technology – The University of haripur
3
switch(expression) {
case x:
// code block
Designed & Prepared by Mr. Kamran Ahmad Awan
Syntax of break;
© Common Creative License 4.0
Switch case y:
Statement // code block
break;
default:
// code block
}
Department of Information Technology – The University of haripur
4
Syntax
Designed & Prepared by Mr. Kamran Ahmad Awan
switch (n)
© Common Creative License 4.0
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}
Department of Information Technology – The University of haripur
5
Working of “break” Statement
Designed & Prepared by Mr. Kamran Ahmad Awan
while (true) {
// take input from the user
© Common Creative License 4.0
cout << "Enter a number: ";
cin >> number;
// break condition
if (number < 0) {
break;
}
Department of Information Technology – The University of haripur
6
Important Points
Designed & Prepared by Mr. Kamran Ahmad Awan
The expression provided in the switch should result in a constant value otherwise it would not be valid.
© Common Creative License 4.0
// Constant expressions allowed
switch(1+2+23)
switch(1*2+3%4)
// Variable expression are allowed provided
// they are assigned with fixed values
switch(a*b+c*d)
switch(a+b+c)
Department of Information Technology – The University of haripur
7
Important Points
Designed & Prepared by Mr. Kamran Ahmad Awan
Duplicate case values are not allowed.
© Common Creative License 4.0
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.
Department of Information Technology – The University of haripur
8
Switch Statement Flowchart
Designed & Prepared by Mr. Kamran Ahmad Awan
In terms of the flowchart’s structure, it includes the entrance
© Common Creative License 4.0
expression, cases’ conditions, and the default block.
Additionally, you can add a break announcement block anywhere
to skip a labeled announcement for jumping right to the end.
Specifically, users can understand the needed conditions that
execute the value of the expression of switch blocks. Thus,
defining the keyboard command to run the announcement
Department of Information Technology – The University of haripur
9
Applications of Switch Statements
Designed & Prepared by Mr. Kamran Ahmad Awan
Apart from
© Common Creative License 4.0
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:
Department of Information Technology – The University of haripur
10
Flowchart
Designed & Prepared by Mr. Kamran Ahmad Awan
© Common Creative License 4.0
Department of Information Technology – The University of haripur
11
switch (x)
{
case 1:
cout << "Choice is 1";
Designed & Prepared by Mr. Kamran Ahmad Awan
break;
Simple case 2:
example of C+
© Common Creative License 4.0
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;
}
Department of Information Technology – The University of haripur
12
The Break Keyword
Designed & Prepared by Mr. Kamran Ahmad Awan
When C++ reaches a break keyword, it breaks out of the
© Common Creative License 4.0
switch block.
This will stop the execution of more code and case testing
inside the block.
When a match is found, and the job is done, it's time for a
break. There is no need for more testing.
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.
Department of Information Technology – The University of haripur
13
int day = 4;
switch (day) {
Designed & Prepared by Mr. Kamran Ahmad Awan
case 6:
The Default cout << "Today is Saturday";
© Common Creative License 4.0
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";
}
Department of Information Technology – The University of haripur
14
Switch Exercises 01
Designed & Prepared by Mr. Kamran Ahmad Awan
int day = 2;
© Common Creative License 4.0
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.
}
Department of Information Technology – The University of haripur
15
Switch Exercises 02
• Using Switch statement, write a program that displays the following
Designed & Prepared by Mr. Kamran Ahmad Awan
menu for the food items available to take order from the customer:
© Common Creative License 4.0
• 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
Department of Information Technology – The University of haripur
16
Switch Exercises 03
Designed & Prepared by Mr. Kamran Ahmad Awan
• Using switch statement Write a C++ program to input marks of five
© Common Creative License 4.0
subjects Physics, Chemistry, Biology, Mathematics and Computer.
• Calculate percentage and grade according to following:
• Percentage >= 90% : Grade A
• Percentage >= 80% : Grade B
• Percentage >= 70% : Grade C
• Percentage >= 60% : Grade D
• Percentage >= 40% : Grade E
• Percentage < 40% : Grade F
Department of Information Technology – The University of haripur
17
Switch Exercises 04
Designed & Prepared by Mr. Kamran Ahmad Awan
Program to check whether a number is divisible by 5 and 11 or not.
© Common Creative License 4.0
Program to check whether a character is an alphabet or not.
Program to check whether a character is an uppercase or lowercase alphabet.
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.
Department of Information Technology – The University of haripur
18
Switch Exercises 05
Designed & Prepared by Mr. Kamran Ahmad Awan
C++ Program to take the value from the user as input electricity unit
charges and calculate total electricity bill according to the given
© Common Creative License 4.0
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
Department of Information Technology – The University of haripur
19
Switch Exercises 06
Designed & Prepared by Mr. Kamran Ahmad Awan
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.
© Common Creative License 4.0
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.
Department of Information Technology – The University of haripur
20
End of Switch Statement
Designed & Prepared by Mr. Kamran Ahmad Awan
Introduction to
Break Keyword Default Keyword
Switch Statement
© Common Creative License 4.0
Syntax Examples Practical Exercises
Important Points Flow Chart
Department of Information Technology – The University of haripur