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
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
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
CS 161 Intro To Programming switch statement switch ( variable name ) { case ‘a’ : statements; case ‘b’ : statements; case ‘c’ : statements; … } CS 161 Intro To Programming
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
CS 161 Intro To Programming switch statement case ‘A’ : cout << “ Excellent ” ; … … CS 161 Intro To Programming
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
CS 161 Intro To Programming break; CS 161 Intro To Programming
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
CS 161 Intro To Programming default : default : cout << “ Please Enter Grade from ‘A’ to ‘D’ or ‘F’ “ ; CS 161 Intro To Programming
Flow Chart of switch statement switch (grade) case ‘A’ : Display “Excellent” case ‘B’ : Display “Very Good” … Default : “……..” CS 161 Intro To Programming
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
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
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
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 1 + 2 : case 4: : CS 161 Intro To Programming
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
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
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
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
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
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
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
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
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
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
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
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
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
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