Post on 02-Jan-2016
description
© Janice Regan, CMPT 128, Jan 2007 1
CMPT 128: Introduction to Computing Science for Engineering Students
Control Structures
Nested ifs, switch statements
© Janice Regan, CMPT 128, Jan 2007 2
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;
} }
© Janice Regan, CMPT 128, Jan 2007 3
Flowchart for multiple selection
condition2
Statement 1A;
Statement nA;T
F
Statement 1C;
Statement nC;
condition
F
Statement 1B;
Statement nB;
T
© Janice Regan, CMPT 128, Jan 2007 4
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;
}
© Janice Regan, CMPT 128, Jan 2007 5
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;
}
© Janice Regan, CMPT 128, Jan 2007 6
Flowchart for multiple selection
condition2
Statement 1;
Statement n;
T
F
Statement 1C;
Statement nC;
condition
F
Statement 1A;
Statement nA;T
F
© Janice Regan, CMPT 128, Jan 2007 7
Flowchart for multiple selection
condition
Statement 1A;
Statement nA;T
F
Statement 1C;
Statement nC;
condition&&condition2
F
Statement 1B;
Statement nB;
T
© Janice Regan, CMPT 128, Jan 2007 8
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;
}
© Janice Regan, CMPT 128, Jan 2007 9
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
© Janice Regan, CMPT 128, Jan 2007 10
switch Structuresswitch(controling expression){case value1:
statements1;break;
case value2: statements2;break;
...case valuen:
statementsn; break;
default: statements;
}
© Janice Regan, CMPT 128, Jan 2007 11
switch Statement
expression = value1
expression = value2
expression = valuen
© Janice Regan, CMPT 128, Jan 2007 12
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++;}