Programming Language Control Structures: Selection (switch) Eng. Anis Nazer First Semester 2018-2019
Multiple selection choose one of two things if/else choose one from many things multiple selection using nested if/else switch switch is another structure for multiple selection
Multiple selection expression multiple selection can be performed using a switch statement what is the difference between switch and nested if/else? expr == v1 false expr == v2 false true true Statement(s) (1) Statement(s) (2) expr == vn true Statement(s) (n) false Statement(s) (n+1)
switch switch ( expression ) { case v1 : Syntax: Statements 1 break ; } case v2 : Statements 2 break ;... case vn : Statements n break ; default : Statements n+1
Write a program that reads a number that represents a day and displays the name of the day : 1 Sunday 2 Monday 7 Saturday otherwise invalid day number
cin >> day; switch ( day ) { case 1 : cout << "Sunday"; case 2 : cout << "Monday"; case 3 : cout << "Tuesday"; case 4 : cout << "Wednesday"; case 5 : cout << "Thursday"; case 6 : cout << "Friday"; case 7 : cout << "Saturday"; default : cout << "Invalid day number"; }
switch Notes: switch, case, default are reserved keywords the value of the expression is compared to the values in the cases, execution jumps to the matching case after the jump execution continues from there sequentially if no matching case exists, execution jumps to the default case
switch More notes: break statement ends the switch statement, execution continues after the switch break statement is not part of the syntax, can be removed... so why use it? default case is optional, you can write a switch without a default case again, why use it? the cases can be ordered as you wish Note the colon : after the value in the cases also note that the statements may be written without braces
We can combie similar cases together by writing an empty case. Example: Write a program that reads a number that represents a day and displays whether the day is a work day or a weekend : 1,2, 3, 4, 5 Workday 6, 7 Weekend otherwise invalid day number
cin >> day; switch ( day ) { case 1 : case 2 : case 3 : case 4 : case 5 : cout << "Workday"; case 6 : case 7 : cout << "Weekend"; default : cout << "Invalid day number"; }
Write a program that reads a letter grade and displays a message as follows: grade equals 'A' grade equals 'B' grade equals 'C' grade equals 'D' grade equals 'F' otherwise excellent very good good fair fail invalid input
cin >> grade; switch ( grade ) { case 'A' : cout << "Excellent"; case 'B' : cout << "Very Good"; case 'C' : cout << "Good"; case 'D' : cout << "Fair"; case 'F' : cout << "Fail"; default : cout << "Invalid grade"; }
- improved Our last example works for capital letter grades, What happens if the user enters a small letter grade. if the user enters the grade a, the program outputs an invalid grade error How to fix this? We can write cases that perform the same thing using an empty case without a break
cin >> grade; switch ( grade ) { case 'a' : case 'A' : cout << "Excellent"; case 'b' : case 'B' : cout << "Very Good"; case 'c' : case 'C' : cout << "Good"; case 'd' : case 'D' : cout << "Fair"; case 'f' : case 'F' : cout << "Fail"; default : cout << "Invalid grade"; }
Write a program that reads two numbers and reads a character that represents an arithmetic operation ( +, -, /, * ). The program displays the result of the operation. for example: Output Window Enter two numbers, please: 21.2 5.9 Enter the operation: * 21.2 * 5.9 = 125.08
What does the following program do? cin >> N; switch ( N%2 ) { case 0 : cout << N << " is even"; case 1 : cout << N << " is odd"; }
What does the following program do? int n = 13; switch ( n % 9 ) { case 1: cout << n*3 << endl; case 2: cout << n+3 << endl; case 3: cout << n 3 << endl; case 4: cout << n/3 << endl; default: cout << n << endl; }
What does the following program do? int x=1, y = 9; switch(y%4) { case 3: x += 10; case 0: case 1: x += 5; case 2: x += 3; } cout << x;
Rewrite the following if/else using a switch statement int val; cin >> val; if ( val >= 5 && val < 8 ) cout << "AAA" << endl; else if ( val > 2 && val < 5 ) cout << "BBB" << endl; else cout << "CCC" << endl;
Questions questions? anyone?