Lecture-7
Lecture-7
Switch Statement
Break Statement
2
Decision Making in C++
Example
A company insures driver in the following case
if driver is married
If the driver is unmarried, male and above 30 years of age
If the driver is unmarried, female and above 25 years of age
3
Decision Making in C++
int main() {
char gender,ms;
int age;
cout<< "enter age , gender, marital status";
cin>>age>>gender>>ms;
if(ms=='M')
cout<<"driver is insured";
else {
if(gender=='M') {
if(age>30)
cout<<“Dri
ver is
insure
d";
else
cout<<“Driv
er is
not
insure
d";
} 4
else {
Decision Making - Use of Logical Operators
main() {
char gender,ms;
int age;
cout<< "enter age , gender, marital status";
cin>>age>>gender>>ms;
insured";
else
5
cout<<"driver is not insured";
Decision Making in C++
Switch Structure
All the problem we discussed so far, we see that there are two approaches
for a multi way decision.
In the first approach, we use as many if statements as needed. This is an
expensive approach.
The second is the use of nested if statements. Which is little more efficient
than the first one.
In the 'nested if statements' the nested else is not executed if the first
if condition is true and the control goes out of the if block.
The C++ language provides us a stand-alone construct to handle these
instances.
This construct is switch structure.
6
Decision Making in C++
Switch Structure
The switch structure is a multiple-selection construct that is used in such
cases (multi way decisions) to make the code more efficient and easy to
read and understand.
A switch statement allows a variable to be tested for equality against a list
of values.
Each value is called a case, and the variable being switched on is checked
for each case.
if a case label matches, the statements after the case label are executed.
If no case label matches the switch expression, the code under the
default label is executed (if it exists).
7
Decision Making in C++
Switch Structure
The Syntax for switch statement is as follow
switch(expression){
case constant-expression :
statement(s);
break; //optional
case constant-
expression :
statement(s);
break; //optional
//any number of
case statements.
default :
//Optional
statement(s); 8
Decision Making in C++
9
Decision Making in C++
10
Decision Making in C++
11
Decision Making in C++
Switch Implementation
Write a program that has a switch structure. The switch structure will check
different cases for grades. Implement your switch structure as follows
If grade is A then display “Excellent”
If grade is B or C then display “Well Done”
If grade is D the display “You passed”
If grade is F then display “You are Fail”
12
Decision Making in C++
main ()
{ char grade ;
switch(grade) {
case
'A' :
cout << "Excellent!\n";
break;
case 'B' :
case 'C' :
cout << "Well done\n";
break;
case 'D' :
cout << "You passed\n“;
break;
case 'F' :
cout << “You are Fail\
n"; break;
default :
cout << "Invalid grade\ 17
Decision Making in C++
Switch Implementation
We will see the use of ' the letter a' instead of 'A' can affect our program.
We want our program to be user- friendly. We don’t want to restrict the
user to enter the grade in capital letters only. So we have to handle both
small and capital letters in our program.
Here comes the limitations of switch statement.
We have to make two separate cases so we write
case ‘A’:
case ‘a’ :
statements
;
Home Activity:
Rewrite Code
14
Decision Making in C++
Break Statement
The break statement interrupts the flow of control.
In switch statement, when a true case is found, the flow of control goes
through every statement down ward.
We want that only the statements of true case should be executed and the
remaining should be skipped.
For this purpose, we write the break statement after the statements of a
case. Thus, when a true case is found and its statements are executed
then the break statement interrupts the flow of control and the control
jumps out of the switch statement.
15
Decision Making in C++
Break Statement
If we want to do the same task for two cases, like in previous example for
‘A’ and ‘a’, then we don't put break statement after the first case.
We write both the cases (or the cases may be more than two) line by line
then write the common statements to be executed for these cases and
then write the break statement.
The break statement is necessary in switch structure, without it the switch
structure becomes illogic. As without it all the statement will execute after
first match case is found.
16
Decision Making in C++
17
Decision Making in C++
Practice Problem
Write a program that prompts the user to enter two numbers and one
“Arithmetic Operator”. The program will perform arithmetic operation on
the number according the arithmetic operator and display result.
You have to use switch statement.
20
21