Download presentation
Presentation is loading. Please wait.
Published byJuliet Daniels Modified over 10 years ago
1
© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements
2
© Janice Regan, CMPT 128, Jan 2007 1 Multiple Selections (Nested if) if (condition) { // Series of actions to be taken when the condition is true action 1A; action nA; } else { if (condition 2) { // Series of actions to be taken when condition is false and condition2 true action 1B; action nB; } else { // Series of actions to be taken when condition and condition 2 are false action 1C; action nC; }
3
© Janice Regan, CMPT 128, Jan 2007 2 Flowchart for multiple selection condition2 Statement 1A; Statement nA; T F Statement 1C; Statement nC; condition F Statement 1B; Statement nB; T
4
© Janice Regan, CMPT 128, Jan 2007 3 Multiple Selections (elseif) if (condition) { // Series of actions to be taken when condition is true action 1A; action nA; } else if (condition 2) { // Series of actions to be taken when condition is false and condition 2 is true action 1B; action nB; } else { // Series of actions to be taken when the condition and condition2 are false action 1C; action nC; }
5
© Janice Regan, CMPT 128, Jan 2007 4 Multiple Selections (Nested if) if (condition) { if (condition 2) { // Series of actions to be taken when condition is true and condition2 true action 1A; action nA; } else { // Series of actions to be taken when condition is true and condition 2 is false action 1B; action nB; } else { // Series of actions to be taken when condition is false, condition2 either action 1C; action nC; }
6
© Janice Regan, CMPT 128, Jan 2007 5 Flowchart for multiple selection condition2 Statement 1; Statement n; T F Statement 1C; Statement nC; condition F Statement 1A; Statement nA; T F
7
© Janice Regan, CMPT 128, Jan 2007 6 Flowchart for multiple selection condition Statement 1A; Statement nA; T F Statement 1C; Statement nC; condition&& condition2 F Statement 1B; Statement nB; T
8
© Janice Regan, CMPT 128, Jan 2007 7 Multiple Selections (elseif) if (condition && condition2) { // Series of actions to be taken when condition is true and condition2 true action 1A; action nA; } else if (condition) { // Series of actions to be taken when condition is true and condition2 is false action 1; action n; } else { // Series of actions to be taken when the condition and condition2 are false action 1; action n; }
9
© Janice Regan, CMPT 128, Jan 2007 8 The switch statement An alternative method to if statements with multiple else clauses The controlling expression (selector) must have an integral type Characters also work because they can be converted to integers Case labels are particular values of the controlling expression Break statements exit from the switch structure
10
© Janice Regan, CMPT 128, Jan 2007 9 switch Structures switch(controling expression) { case value1: statements1; break; case value2: statements2; break;... case valuen: statementsn; break; default: statements; }
11
© Janice Regan, CMPT 128, Jan 2007 10 switch Statement expression = value1 expression = value2 expression = valuen
12
© Janice Regan, CMPT 128, Jan 2007 11 Sample case statement /* This is in a loop that reads */ /* nextInt each time */ switch (nextInt) { case 0: count0++; break; case 1: count1++; break; case 2: count2++; break; case 4: count4++; break; case 5: count5++; break; case 6: count6++; break; case 7: count7++; break; default: countOther++; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.