Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

09 - Switch Statement

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Switch Statement

Programming
Fundamentals
Introduction to Switch Statement

Switch case statements are a substitute for long if


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.
switch(expression) {
case x:
// code block
Syntax of break;
Switch case y:
Statement // code block
break;
default:
// code block
}
Syntax
switch (n)
{
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
}
Working of “break” Statement
while (true) {
// take input from the user
cout << "Enter a number: ";
cin >> number;

// 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.

// 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)
Important Points
Duplicate case values are not allowed.

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

In terms of the flowchart’s structure, it includes the entrance


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
Applications of Switch Statements

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

When C++ reaches a break keyword, it breaks out of the


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.
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

• Using switch statement Write a C++ program to input marks of five


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
Switch Exercises 04

Program to check whether a number is divisible by 5 and 11 or not.

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.
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

Syntax Examples Practical Exercises

Important Points Flow Chart

You might also like