Download presentation
Presentation is loading. Please wait.
Published byPrimrose Little Modified over 6 years ago
1
Intro to Programming Week # 4 Switch Statement Lecture # 7
Department of Computer Science & Engineering Air University Intro to Programming Week # 4 Switch Statement Lecture # 7 By: Saqib Rasheed
2
CS 161 Intro To Programming
Quiz Time [20 Mins] Marks[10] Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three Angles is equal to 180 degrees CS 161 Intro To Programming
3
CS 161 Intro To Programming
switch statement The control statement that allows us to make a decision from the number of choices is called a switch statement CS 161 Intro To Programming
4
CS 161 Intro To Programming
switch statement switch ( variable name ) { case ‘a’ : statements; case ‘b’ : statements; case ‘c’ : statements; … } CS 161 Intro To Programming
5
CS 161 Intro To Programming
switch statement switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : … … } CS 161 Intro To Programming
6
CS 161 Intro To Programming
switch statement case ‘A’ : cout << “ Excellent ” ; … … CS 161 Intro To Programming
7
CS 161 Intro To Programming
Example switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : cout << “Good ” ; case ‘D’ : cout << “ Poor ” ; case ‘F’ : cout << “ Fail ” ; } CS 161 Intro To Programming
8
CS 161 Intro To Programming
break; CS 161 Intro To Programming
9
CS 161 Intro To Programming
Example char grade; cin>>grade; switch ( grade ) { case ‘A’ : cout << “ Excellent ” ; break ; case ‘B’ : cout << “ Very Good ” ; break ; case ‘C’ : cout << “Good ” ; break ; case ‘D’ : cout << “ Poor ” ; break ; case ‘F’ : cout << “ Fail ” ; } CS 161 Intro To Programming
10
CS 161 Intro To Programming
default : default : cout << “ Please Enter Grade from ‘A’ to ‘D’ or ‘F’ “ ; CS 161 Intro To Programming
11
Flow Chart of switch statement
switch (grade) case ‘A’ : Display “Excellent” case ‘B’ : Display “Very Good” … Default : “……..” CS 161 Intro To Programming
12
CS 161 Intro To Programming
Example int grade; cin>>grade; switch ( grade ) { case 1 : cout << “ Excellent ” ; break ; case 2 : cout << “ Very Good ” ; break ; case 3 : cout << “Good ” ; break ; case 4 : cout << “ Poor ” ; break ; case 5 : cout << “ Fail ” ; } CS 161 Intro To Programming
13
CS 161 Intro To Programming
Example # 1 int i = 2 ; switch ( i ) { case 1 : cout<<"I am in case 1 \n"; break; case 2 : cout<<"I am in case 2 \n"; break; case 3 : cout<<"I am in case 3 \n"; break; default : cout<<"I am in default \n"; } Output I am in case 2 CS 161 Intro To Programming
14
CS 161 Intro To Programming
if-else Switch switch (x) { case 1: cout << "x is 1"; break; case 2: cout << "x is 2"; default: cout << "value of x unknown"; } if (x == 1) { cout << "x is 1"; } else if (x == 2) cout << "x is 2"; else cout << "value of x unknown"; CS 161 Intro To Programming
15
switch Versus if-else Ladder
There are some things that you simply cannot do with a switch. These are: A float expression cannot be tested using a switch Cases can never have variable expressions (for example it is wrong to say case a +3 : Multiple cases cannot use same expressions. Thus the following switch is illegal: switch ( a ) { case 3.2 : case 4: case : case 4: : CS 161 Intro To Programming
16
CS 161 Intro To Programming
Example # 2 int day; cout<<"Eneter Day Number"; cin>>day; switch (day) { case 1 : cout << "\nSunday"; break; case 2 : cout << "\nMonday"; case 3 : cout << "\nTuesday"; case 4 : cout << "\nWednesday"; case 5 : cout << "\nThursday"; case 6 : cout << "\nFriday"; case 7 : cout << "\nSaturday"; default : cout << "\nNot an allowable day number"; } CS 161 Intro To Programming
17
CS 161 Intro To Programming
switch (day) { case 1 : case 7 : cout << "This is a weekend day"; break; case 2 : case 3 : case 4 : case 5 : case 6 : cout << "This is a weekday"; default : cout << "Not a legal day"; break; } Break 1 Break 2 CS 161 Intro To Programming
18
CS 161 Intro To Programming
Example # 3 char ch ; cout<< "Enter any of the alphabet a, b, or c "; cin>>ch; switch ( ch ) { case 'a' : case 'A' : cout<<"a is an apple" ; break ; case 'b' : case 'B' : cout<<"b as in brain"; break ; case 'c' : case 'C' : cout<<"c as in cookie"; break ; default : cout<<"wish you knew what are alphabets" ; } CS 161 Intro To Programming
19
Is switch a replacement for if?
Yes and no. Yes, because it offers a better way of writing programs as compared to if, and no because in certain situations we are left with no choice but to use if. The disadvantage of switch is that one cannot have a case in a switch which looks like: case i <= 20 : CS 161 Intro To Programming
20
switch works faster than an equivalent if-else ladder.
compiler generates a jump table for a switch during compilation during execution it simply refers the jump table to decide which case should be executed . if-elses are slower because they are evaluated at execution time CS 161 Intro To Programming
21
CS 161 Intro To Programming
Question # 1 Make a calculator using switch statement It should take two numbers from user. Ask the user the function to perform Display the result CS 161 Intro To Programming
22
CS 161 Intro To Programming
Question # 2 Que Write a menu driven program program in C++ using switch statement that contain option as under.( The Menu should ) Enetr 1--> To Find Largest Number Among Three Variables. Enetr 2--> To Find ODD or EVEN Enetr 3--> To Find Condition of Water Enetr 4--> To Find Grade Of Student CS 161 Intro To Programming
23
CS 161 Intro To Programming
Question # 2 (Code) #include<iostream.h> Void main () { int option; cout<<"\n\t\tEnter the option\n"; cout<<"Enetr 1--> To Find Largest Number Among Three Variables.\n"; cout<<"Enetr 2--> To Find ODD or EVEN\n"; cout<<"Enetr 3--> To Find Condition of Water\n"; cout<<"Enetr 4--> To Find Grade Of Student\n"; cin>>option; CS 161 Intro To Programming
24
CS 161 Intro To Programming
Question # 2 (Code) switch (option) { case 1: int a,b,c, larg; cout<<"Enter First Integer="; cin>>a; cout<<"Enter Second Integer="; cin>>b; cout<<"Enter Third Integer="; cin>>c; CS 161 Intro To Programming
25
CS 161 Intro To Programming
Question # 2 (Code) if (a > b) larg = a; else larg = b; if (larg > c ) cout<<"Largest is ="<<larg<<endl; cout<<"Largest is ="<<c<<endl; break; CS 161 Intro To Programming
26
CS 161 Intro To Programming
Question # 2 (Code) case 2: int value; cout<<"Enter an Interger value "; cin>>value; if (value % 2 == 0) cout<<"Your number is EVEN\n"; else cout<<"Your number is ODD\n"; break; CS 161 Intro To Programming
27
CS 161 Intro To Programming
case 3: int t; cout<<"Temperature Less than 0 = ICE \n" <<"Temperature Greater than 0 & " <<"Temperature Less than 100 = Water\n" <<"Temperature Greater than 100 = STEAM\n"; cout<<"\n\n\t\tPlease enter the Temperature="; cin>>t; if ( t <= 0 ) cout<<"Form of water is \"ICE\""<<endl; else if( t > 0 && t < 100 ) cout<<"Form is \"WATER\"\n"; else if ( t >= 100 ) cout<<"Form of water is \"steam\"\n“; break; CS 161 Intro To Programming
28
CS 161 Intro To Programming
case 4: int grade; cout<<"Enter the grade of student="; cin>>grade; if ( grade >= 90 ) cout<< " Grade A \n"; else if (grade >= 80 ) cout<<" Grade B \n"; else if ( grade >=70 ) cout<<" Grade C \n"; else if (grade >=60) cout<<" Grade D \n"; CS 161 Intro To Programming
29
CS 161 Intro To Programming
else { cout<<" Grade F \n"; cout<<" You have to take the classes again\n"; cout<<" Work Hard To Get Good Grade\n"; } break; default: cout<<"You Entered Invalid Option\n"; cout<<"Enetr A Valid Option\n"; } //End Of Switch } //End Of Main CS 161 Intro To Programming
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.