0% found this document useful (0 votes)
13 views14 pages

Lecture 2 Week 5

The document explains the switch statement in programming, detailing its syntax and use cases. It emphasizes the importance of the break statement to control the flow of execution within the switch cases. Additionally, it includes examples and practice programs related to the switch statement, such as checking if a number is even or odd and creating a calculator.

Uploaded by

Umer Blouxh
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)
13 views14 pages

Lecture 2 Week 5

The document explains the switch statement in programming, detailing its syntax and use cases. It emphasizes the importance of the break statement to control the flow of execution within the switch cases. Additionally, it includes examples and practice programs related to the switch statement, such as checking if a number is even or odd and creating a calculator.

Uploaded by

Umer Blouxh
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/ 14

PROGRAMMING

FUNDAMENTALS
LAB
Umer Ahmad
Lecture 2 week 5
Switch statement
•Aswitch 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.
switch(expression) {
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
Syntax break; //optional
// you can have any number of case
statements.
default :
//Optional
statement(s);
}
FLOW
DIAGRA
M
Without break statement
case 3:
#include <iostream>
using namespace std;
cout<<"Case3 "<<endl;
int main() case 4: Output:
{ Case2
cout<<"Case4 "<<endl;
Case3
int i=2;
default: Case4
switch(i) Default
{
cout<<"Default "<<endl;
case 1: }
cout<<"Case1 "<<endl; return 0;
case 2:
}
cout<<"Case2 "<<endl;
Example • Inthe above program, we have the
variable i inside switch braces, which
Explained means whatever the value of variable i
is, the corresponding case block gets
executed. We have passed integer
value 2 to the switch, so the control
switched to the case 2, however we
don’t have break statement after the
case 2 that caused the flow to continue
to the subsequent cases till the end.
However this is not what we wanted,
we wanted to execute the right case
block and ignore rest blocks. The
solution to this issue is to use the break
statement in after every case block.
Break Statement Used
Break statements are used when you want your program-flow to
come out of the switch body. Whenever a break statement is
encountered in the switch body, the execution flow would directly
come out of the switch, ignoring rest of the cases. Therefore, you
must end each case block with the break statement.
Tell me the output??

#include <iostream> case 3:


using namespace std; cout<<"Case3 "<<endl;
int main()
break;
{
int i=2; case 4:
switch(i) cout<<"Case4 "<<endl;
{ break;
case 1: default:
cout<<"Case1 "<<endl;
cout<<"Default "<<endl;
break;
case 2:
}
cout<<"Case2 "<<endl; return 0;
break; }
Why didn’t I use break statement after
default?
The control would itself come out of the switch
after default so I didn’t use break statement
after it, however if you want you can use it,
there is no harm in doing that.
Example • #include <iostream>
program • using namespace std;
• int main () {
• // local variable declaration:
• char grade = 'D’;
• switch(grade) {
• case 'A’ :
• cout << "Excellent!" << endl;
• break;
• case 'B’ :

Continue..
• case 'C’ :

• cout << "Well done" << endl;

• break;

• case 'D’ :

• cout << "You passed" << endl;

• break;

• case 'F’ :

• cout << "Better try again" << endl;

• break;

• default :

• cout << "Invalid grade" << endl;

• }

• cout << "Your grade is " << grade << endl;

• return 0;

• }
PRACTICE
PROGRAM 1
Checking the number whether number is even or odd
PRACTICE PROGRAM 2
Print weekday using switch statement
LAB TASK
Calculator using switch statement

You might also like