Presentation is loading. Please wait.

Presentation is loading. Please wait.

Alternate Version of STARTING OUT WITH C++ 4th Edition

Similar presentations


Presentation on theme: "Alternate Version of STARTING OUT WITH C++ 4th Edition"— Presentation transcript:

1 Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 5 Looping Copyright 2004 Scott/Jones Publishing

2 Topics (continued) 5.8 for Loops 5.9 Deciding Which Loop to Use
5.10 Nested Loops 5.13 Using Loops for Data Validation Chapter 5 slide 2

3 5.8 for Loops Top test loop that executes zero or more times
Useful for counter-controlled loop Format: for( initialization; test; update ) { 1 or more statements; } Required ; No ; goes here Chapter 5 slide 3

4 for Loop Mechanics for(initialization; test; update)
{ // {} may be omitted statement(s); // if loop body contains } // only 1 statement Perform initialization Evaluate test expression If true, execute statement(s) If false, terminate loop execution Execute update, then re-evaluate test expression Chapter 5 slide 4

5 for Loop Flow of Control
initialization code update code false test true statement(s) Chapter 5 slide 5

6 for Loop Example int sum = 0, num; for (num = 1; num <= 10; num++)
cout << "Sum of numbers 1 – 10 is " << sum << endl; See pr5-13.cpp Chapter 5 slide 6

7 for Loop Modifications
Can define variables in initialization code Their scope is the for loop Initialization code, test, or update code can contain more than one statement Separate with commas Example: for (int sum = 0, num = 1; num <= 10; num++) sum += num; See pr5-14.cpp Chapter 5 slide 7

8 5.9 Deciding Which Loop to Use
while: pretest loop (loop body may not be executed at all) for: pretest loop (loop body may not be executed at all); has initialization and update code; is useful with counters or if precise number of repetitions is known Chapter 5 slide 8

9 5.10 Nested Loops A nested loop is a loop inside the body of another loop Example: for (row = 1; row <= 3; row++) { for (col = 1; col <= 3; col++) cout << row * col << endl; } outer loop inner loop See pr5-15.cpp Chapter 5 slide 9

10 Nested Loops Notes Inner loop goes through all its repetitions for each repetition of outer loop Inner loop repetitions complete sooner than outer loop Total number of repetitions for inner loop is product of number of repetitions of the two loops. In previous example, inner loop repeats 9 times Chapter 5 slide 10

11 5.13 Using Loops for Data Validation
Loops are the most appropriate structure for validating user input data Prompt and read in the data. Use a top test loop to test if data is valid. Enter the loop only if data is not valid. Inside the loop, prompt the user to re-enter the data. The loop will not be exited until valid data has been entered. Chapter 5 slide 11

12 Data Validation Loop Example
cout << “Enter a number (1-100) and” << “ I will guess it. ”; cin >> number; while (number < 1 || number > 100) { cout << “Number must be between 1 and 100.” << “ Re-enter your number. ”; } // Code to use the valid number goes here. See pr5-18.cpp Chapter 5 slide 12

13 Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 5 Looping Copyright 2004 Scott/Jones Publishing


Download ppt "Alternate Version of STARTING OUT WITH C++ 4th Edition"

Similar presentations


Ads by Google