120 likes | 212 Views
CMPT 128: Introduction to Computing Science for Engineering Students. Control Structures Nested ifs, switch statements. Multiple Selections (Nested if). if (condition) { // Series of actions to be taken when the condition is true action 1A; action nA; } else {
E N D
CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements
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; } }
Flowchart for multiple selection Statement 1A; Statement nA; T condition F Statement 1B; Statement nB; T condition2 F Statement 1C; Statement nC;
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; }
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; }
Flowchart for multiple selection Statement 1A; Statement nA; condition condition2 T T F F Statement 1; Statement n; F Statement 1C; Statement nC;
Flowchart for multiple selection condition&& condition2 Statement 1A; Statement nA; T F Statement 1B; Statement nB; T condition F Statement 1C; Statement nC;
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; }
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
switch Structures switch(controling expression) { casevalue1: statements1; break; casevalue2: statements2; break; ... case valuen: statementsn; break; default: statements; }
switch Statement expression = value1 expression = value2 expression = valuen
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++; }