1 / 12

CMPT 128: Introduction to Computing Science for Engineering Students

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 {

Download Presentation

CMPT 128: Introduction to Computing Science for Engineering Students

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements

  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; } }

  3. Flowchart for multiple selection Statement 1A; Statement nA; T condition F Statement 1B; Statement nB; T condition2 F Statement 1C; Statement nC;

  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; }

  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; }

  6. Flowchart for multiple selection Statement 1A; Statement nA; condition condition2 T T F F Statement 1; Statement n; F Statement 1C; Statement nC;

  7. Flowchart for multiple selection condition&& condition2 Statement 1A; Statement nA; T F Statement 1B; Statement nB; T condition F Statement 1C; Statement nC;

  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; }

  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

  10. switch Structures switch(controling expression) { casevalue1: statements1; break; casevalue2: statements2; break; ... case valuen: statementsn; break; default: statements; }

  11. switch Statement expression = value1 expression = value2 expression = valuen

  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++; }

More Related