Programming in C++ Switch (letter) icase X: Statement 1 break case 'L caseM. Statement2 break case 'S’: Statement3; break default Statement Statements
6 Switch(letter) {case ‘X’ : Statement1; break; case ‘L’ : case ‘M’: Statement2; break; case ‘S’ : Statement3; break; default : Statement4; } Statement5;
Programming in C++ >In this example, letter is the switch expression >The statement means %If letter is X, execute Statement1 and continue with Statements If letter is" L' or 'M, execute Statement2 and continue with Statements %If letter isS, execute Statement and continue with Statements %If letter is none of the characters mentioned execute Statement and continue with Statements >The Break statement causes an immediate exit from the Switch statement
7 ➢In this example, letter is the switch expression. ➢The statement means ❖If letter is ‘X’,execute Statement1 and continue with Statement5. ❖If letter is ‘L’ or ‘M’, execute Statement2 and continue with Statement5. ❖If letter is ‘S’, execute Statement3 and continue with Statement5. ❖If letter is none of the characters mentioned, execute Statement4 and continue with Statement5. ➢The Break statement causes an immediate exit from the Switch statement
Programming in C++ Control in Switch Statement o control branches to the statement following the case label that matches the value of IntegralExpression. Control proceeds through all remaining statements including the default, unless redirected with break o if no case label matches the value of IntegralExpression, control branches to the default label(if present)--otherwise control passes to the statement following the entire switch statement .g forgetting to use break can cause logical errors
8 Control in Switch Statement ❖ control branches to the statement following the case label that matches the value of IntegralExpression. Control proceeds through all remaining statements, including the default, unless redirected with break ❖ if no case label matches the value of IntegralExpression, control branches to the default label(if present)——otherwise control passes to the statement following the entire switch statement ❖ forgetting to use break can cause logical errors
Programming in C++ Switch(grade) //Wrong Version case ' A case 'B: cout <<Good Work case 'C:cout<<“ Average Work” case 'D case 'F. cout < Poor Work numberIn trouble++ default: cout < grade < is not a valid letter grade
9 Switch (grade) // Wrong Version { case ‘A’ : case ‘B’ : cout <<“Good Work”; case ‘C’ : cout <<“Average Work”; case ‘D’ : case ‘F’: cout <<“Poor Work”; numberInTrouble++; default : cout << grade <<“is not a valid letter grade.” }
Programming in C++ )If grade is A, the resulting output is this Good WorkAverage WorkPoor WorkA is not a valid letter grade Remember After a branch is taken to a specific case label, control proceeds sequentially until either a Break statement or the end of the Switch statement occurs 10
10 ➢If grade is ‘A’, the resulting output is this: Good WorkAverage WorkPoor WorkA is not a valid letter grade. ➢Remember: After a branch is taken to a specific case label, control proceeds sequentially until either a Break statement or the end of the Switch statement occurs