1 / 15

Overview

Overview. do/while loop break statement continue statement switch statement. do/while loop. do { statement; } while (condition); Step1: Execute statements inside braces Step2: Evaluate condition. Step3: If condition is true repeat Step1 through 3, otherwise

melita
Télécharger la présentation

Overview

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. Overview • do/while loop • break statement • continue statement • switch statement

  2. do/while loop do { statement; } while (condition); Step1: Execute statements inside braces Step2: Evaluate condition. Step3: If condition is true repeat Step1 through 3, otherwise continue execution at next statement following the do/while statement Usually use { } even when not necessary Useful when you want the statements to execute at least once. i.e reading input.

  3. do while example do { cout << “Enter a number between 1 and 10: “; cin >> number; } while (number <= 0 || number > 10);

  4. do/while example What will the output of the following program sequence be? int j = 4; do { j = j - 2; cout << “Hi”; } while (++j > 0); Walk through this example showing how j changes.

  5. Loop Exercises 1) How many times does the statement inside the do/while body execute? What gets displayed? int i = 0, x = 2; do { x = x + i; } while (++ i < 3); cout << “x = “ << x << endl; 2) How many times does the cout statement execute? for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 3; j++) { cout << “Hello” << endl; } }

  6. continue statement skip the rest of this iteration and continue with the next iteration of the loop int x = 1; while ( x <= 10) { if (5 == x) { x++; continue; } cout << x << endl; x++; } condition (x <= 10) is next statement executed after the continue statement.

  7. continue statement • for (int x = 1; x <= 10; x++) { • if (5 == x) • continue; • cout << x << endl; • } • next statements executed after continue are • x ++; • x <= 10;

  8. break statement • Breaks out of the loop. Next statement executed after a • break statement is the statement immediately following the loop. • for (int x = 1; x <= 10; x++) { • if (5 == x) • break; • cout << x << endl; • } • Next statement executed after the break statement is the • statement immediately following the for loop.

  9. else if example if (‘A’ == lettergrade) gpa = 4.0; else if (‘B’ == lettergrade) gpa = 3.0; else if (‘C’ == lettergrade) gpa = 2.0; else if (‘D’ == lettergrade) gpa = 1.0; else if (‘F’ == lettergrade) gpa = 0.0; else cout << “Illegal letter grade” << endl;

  10. switch statement switch (lettergrade) { case ‘A’: gpa = 4.0; break; case ‘B’: gpa = 3.0; break; case ‘C’: gpa = 2.0; break; case ‘D’: gpa = 1.0; break; case ‘F’: gpa = 1.0; break; default: cout << “Illegal letter grade” << endl; }

  11. switch statement syntax switch (controlling expression) { case label1: statement(s); break; case label2: statement(s); break; . . . case labeln: statement(s); break; default: statement(s); }

  12. switch statement rules • controlling expression must be of type int or char • labels must be constants that match the type of the controlling expression • default statement can be anywhere. More intuitive at bottom. • can have multiple case labels sharing the same code case ‘A’: case ‘a’ : statements; break;

  13. else if that cannot be implemented as a switch int score; cout << “Enter score: “; cin >> score; cout << “Your grade is “; if (score < 68) cout << “NR” << endl; else if (score < 78) cout << “C” << endl; else if (score < 88) cout << “B” << endl; else cout << “A” << endl;

  14. common programming errors • Forgetting the break statement. Statements for next case get executed by mistake. • Forgetting the space between the word case and the label. Will not be flagged by compiler but won’t execute the way you intend.

  15. Grandma’s Attic for (char letter = 'a'; letter <= ‘i'; letter++) { cout << "I'm going to Grandma's attic and I'm going to get "; switch (letter) { case 'i': cout << "an igloo, "; case 'h': cout << "a hat, "; case 'g': cout << "a goat, "; case 'f': cout << "a fox, "; case 'e': cout << "an elephant, "; case 'd': cout << "a dinosaur, "; case 'c': cout << "a cookie, "; case 'b': cout << "a ball and "; case 'a': cout << "an apple" << endl; default: break; } }

More Related