Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.

Similar presentations


Presentation on theme: "Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass."— Presentation transcript:

1 Chapter 6 - Repetition

2 while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass loop body if test false Lesson 6.1

3 General Structure while (expression) { statement1 statement2 … } Logical expression (variable or arithmetic expression) Boolean results (True or False) Lesson 6.1

4 Steps In Using a while Loop 1. Initialize variable acting as test expression 2. Evaluate the boolean test 3. If true execute statements within loop 4. Update the variable 5. Go back to step 2 Note: if no change to variable then infinite loop (never ends) is created Lesson 4.1

5 Example while Loop int i= 0, number = 1; while (number) { cout << “Please type a number. ” << “Type 0 (zero) to stop execution.\n”; cin >> number; i++; if (i > 50) break; } Initialize variables Variable as expression value other than zero tests true Loop body Statements that provide exit from loop Lesson 6.2

6 Example while Loop int i= 0; while (i <= 5) { cout << “Loop number is “ << i; i++; } Initialize test expression variable Expression evaluated, when true, statements in loop executed Statements within loop Changing variable to provide exit from loop Lesson 6.2

7 do-while Loop u Used when you want to execute loop at least once u Loops on test being true and exits when test is false Lesson 6.3

8 General Syntax do { statement1; statement2; … } while (expression); Lesson 6.3

9 do while Loop Example u Bare bones u Does not alert user of problems do { cout << “\n enter an id number:”; cin >> id_num; } while ((id_num 1999)); Lesson 6.3

10 Better do while Loop do { cout << “\n Enter an id number:”; cin >> id_num; if ((id_num 1999)) { cout << “\n invalid number entered” << “\n please check and re-enter”; } else break; } while (1); Decision Statement – Chapter 5 Expression always true Exit loop if id number is valid Lesson 6.3

11 Recap while(s) u while –Most commonly used when repetition not counter controlled –Pre-tested –Loop body may not be executed u do-while –Convenient when at least one repetition needed –Post-tested Lesson 6.3

12 For Loop u Convenient for counter controlled loops u Pre-tested loop u Same behavior as while –Set lcv to initial value –Test lcv against terminating value –Update lcv to next value u All done in for header statement Lesson 6.4

13 General Syntax for (initialize lcv; test lcv; update lcv) { statement; } Lesson 6.4

14 for Loop (cont.) u Example: for (int i = 0; i < 10; i++) cout << i; u Initialization, testing and updating in same statement u Semicolons required u Braces optional if only one statement Lesson 6.4

15 Example: int finalValue; cin >> finalValue; for (int counter = 0; counter < finalValue; counter++) cout << "*"; Display Screen 3 finalValue 3 counter 0 True * 1 * 2 * 3 False Lesson 6.4

16 For Loop Examples float sum = 0; for (float x = 0.5; x < 20.1; x += 0.5) sum += sqrt (x); for (int count = 0; count < n; count++) cout << ch; Lesson 6.4

17 Comparison: int finalvalue; cin >> finalValue; for (int counter = 0; counter < finalValue; counter++) cout << "*"; counter = 0; cin >> finalValue; while (counter < finalValue) { cout << "*"; counter++;} int counter, finalValue;int finalvalue; cin >> finalValue; for(int counter = 0; counter < finalValue; counter ++) cout << "*"; Lesson 6.4

18 Debugging and Testing u Off by one errors –Check loops to ensure you are executing the correct number of times –x < 10 or x <= 10 u Check loop boundaries –Can create infinite loop u Does answer make sense Lesson 6.4

19 For Loop Modifications u break statement –Can use within loop to terminate early –Controversial u Multiple expressions for initialization and increment –Use comma separated list –for (I = 1, j = 2; I < 10; I++, j++) Lesson 6.4

20 Summary u Create while loops u Create do-while loops u Create for loops u Trace and debug loops u Use loops to solve problems Learned how to: Chapter 6


Download ppt "Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass."

Similar presentations


Ads by Google