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

Switch Statements: Programming Fundamentals Zainab Ishfaq

Switch statements allow a variable to be tested for equality against a list of case values. The variable is checked against each case and if it matches, the statements for that case are executed. It provides an alternative to multiple if/else statements. The syntax includes a switch expression in parentheses followed by cases and statements. If a break is encountered, it exits the switch; otherwise it continues to the next case.

Uploaded by

Aneeqa Sohail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views11 pages

Switch Statements: Programming Fundamentals Zainab Ishfaq

Switch statements allow a variable to be tested for equality against a list of case values. The variable is checked against each case and if it matches, the statements for that case are executed. It provides an alternative to multiple if/else statements. The syntax includes a switch expression in parentheses followed by cases and statements. If a break is encountered, it exits the switch; otherwise it continues to the next case.

Uploaded by

Aneeqa Sohail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Switch Statements

Programming Fundamentals
Zainab Ishfaq

1
Switch Statements
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.

Syntax
The syntax for a switch statement in C++ is as follows

2
Multiple Selection:
The switch Statement

Syntax:
switch (<selector expression>)
{
case <label1> : <sequence of statements>;
break;
case <label2> : <sequence of statements>;
break;
case <labeln> : <sequence of statements>;
break;
default : <sequence of statements>;
4
}
Multiple Selection:
The switch Statement

Syntax:
switch (<selector expression>)
{
case <label1> : <sequence of statements>;
break;
case <label2> : <sequence of statements>;
break; • Evaluate selector expression.
case <labeln> : <sequence of statements>; • The selector expression can only be: a bool, an integer,
or a char.
break;
• Match case label.
default : <sequence of statements>;
• Execute sequence of statements of matching label.
}
• If break encountered,
go to end of the switch statement.
• Otherwise continue execution.
5
Multiple Selection:
The switch Statement

Syntax:
switch (<selector expression>)
{
case <label1> : <sequence of statements>;
break;
case <label2> : <sequence of statements>;
break; • Evaluate selector expression.
case <labeln> : <sequence of statements>; • The selector expression can only be: a bool, an integer,
or a char.
break;
• Match case label.
default : <sequence of statements>;
• Execute sequence of statements of matching label.
}
• If break encountered,
go to end of the switch statement.
• Otherwise continue execution.
6
#include <iostream>
using namespace std;
int main ( )
{
char ch ;
cout << "\n Enter the grade of student:"<<endl ;
cin >> ch;
switch (ch)
{
case 'A' :
case 'a' :
cout<<"Excellent";
break;
case 'B' :
case 'b' :
cout<<"Good";
break;
case 'C' :
case 'c' :
cout<<"O.K";
break;
case 'D' :
case 'd' :
cout<<"Poor";
break;
case 'F' :
case 'f' :
cout<<"Fail";
break;
default: cout<<"invalid letter grade";
}
} 8
#include <iostream>
using namespace std;
int main ( )
{
char ch ;
cout << "\n Enter the grade of student:"<<endl ; Let suppose user enters D
cin >> ch;
switch (ch)
{ Ch=D
D does not match to: A
case 'A'
case 'a'
D does not match to: a
cout<<"Excellent";
break;
case 'B' :
D does not match to:B
case 'b'
D does not match tocout<<"Good";
b
break;
case 'C'
D does not match to: C
case 'c' :
D does not match tocout<<"O.K";
c Execute sequence of statements
break; of matching label.
D matchescase
to D'D' :
continue execution
case 'd' :
cout<<"Poor";
break;
case 'F' : go to end of the switch statement
case 'f' :
cout<<"Fail";
break;
default: cout<<"invalid letter grade";
}
} 9
#include <iostream> Out put
using namespace std;
int main ( )
{
char ch ;
cout << "\n Enter the grade of student:"<<endl ;
cin >> ch;
switch (ch)
{
case 'A' :
case 'a' :
cout<<"Excellent";
break;
case 'B' :
case 'b' :
cout<<"Good";
break;
case 'C' :
case 'c' :
cout<<"O.K";
break;
case 'D' :
case 'd' :
cout<<"Poor";
break;
case 'F' :
case 'f' :
cout<<"Fail";
break;
default: cout<<"invalid letter grade";
} 10
}
#include <iostream>
using namespace std;

int main()
{ int x,y;
cout << "Enter 2 integer number: ";
cin >> x>>y;
switch (x+y)
{
case 6: cout << "Too small, sorry!";
break;
case 5: cout << "Good job!\n";
break;
case 4: cout << "Nice Pick!\n";
break;
case 3: cout << "Excellent!\n";
break;
case 2: cout << "Masterful!\n";
break;
case 1: cout << "Incredible!\n";
break;
default: cout << "Too large!\n";
}
cout << "\n\n"; 11
}
#include <iostream>
using namespace std;

int main()
{ int x,y;
cout << "Enter 2 integer number: ";
cin >> x>>y;
switch (x+y)
{ case 7: cout << "Too small, sorry!";
break;
case 5: cout << "Good job!\n";
break;
case 4: cout << "Nice Pick!\n";
case 3: cout << "Excellent!\n";
break;
case 2: cout << "Masterful!\n";
break;
case 1: cout << "Incredible!\n";
break;
default: cout << "Too large!\n";
}
cout << "\n\n";
}
12
#include <iostream>
Out Put
using namespace std;

int main()
{ int x,y;
cout << "Enter 2 integer number: ";
cin >> x>>y;
switch (x+y)
{ case 7: cout << "Too small, sorry!";
break;
case 5: cout << "Good job!\n";
break;
case 4: cout << "Nice Pick!\n";
case 3: cout << "Excellent!\n";
break;
case 2: cout << "Masterful!\n";
break;
case 1: cout << "Incredible!\n";
break;
default: cout << "Too large!\n";
}
cout << "\n\n";
}
13

You might also like