true the loop is exited when condition becomes false Useful in menu driven

True the loop is exited when condition becomes false

This preview shows page 22 - 34 out of 38 pages.

true ; the loop is exited when condition becomes false Useful in menu-driven programs to bring user back to menu to make another choice 5-22
Image of page 22
The for Loop Pretest loop that executes zero or more times Useful for counter-controlled loop Format: for( initialization ; test ; update ) { 1 or more statements; } 5-23 No ; goes here Required ;
Image of page 23
for Loop Mechanics for ( initialization ; test ; update ) { statement(s) ; } 1. Perform initialization 2. Evaluate test expression : 3a. If true , execute statement(s) 3b. If false , terminate loop execution 4. Execute update , then re-evaluate test expression 5-24
Image of page 24
for Loop Flow of Control 5-25 true statement(s) false test initialization code update code
Image of page 25
for Loop Example int sum = 0, num; for (num = 1; num <= 10; num++) sum += num; cout << "Sum of numbers 1 – 10 is " << sum << endl; 5-26
Image of page 26
for Loop Notes If test is false the first time it is evaluated, the body of the loop will not be executed The update expression can increment or decrement by any amount Variables used in the initialization section should not be modified in the body of the loop 5-27
Image of page 27
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 statements with commas Example: int sum=0; for (int num = 0; num <= 10; num++) sum += num; 5-28
Image of page 28
More for Loop Modifications ( These are NOT Recommended ) Can omit initialization if already done int sum = 0, num = 1; for (; num <= 10; num++) sum += num; Can omit update if done in loop for (sum = 0, num = 1; num <= 10;) sum += num++; Can omit test – may cause an infinite loop for (sum = 0, num = 1; ; num++) sum += num; 5-29
Image of page 29
Keeping a Running Total running total : accumulated sum of numbers from each repetition of loop accumulator : variable that holds running total int sum = 0, num = 1; // sum is the while (num <= 10) // accumulator { sum += num; num++; } cout << "Sum of numbers 1 – 10 is " << sum << endl; 5-30
Image of page 30
Sentinels sentinel : value in a list of values that indicates end of data Special value that cannot be confused with a valid value, e.g. , -999 for a test score Used to terminate input when user may not know how many values will be entered 5-31
Image of page 31
Sentinel Example int total = 0; cout << "Enter points earned " << "(or -1 to quit): "; cin >> points; while (points != -1) // -1 is the sentinel { total += points; cout << "Enter points earned: "; cin >> points; } 5-32
Image of page 32
Deciding Which Loop to Use while : pretest loop (loop body may not be executed at all) do-while
Image of page 33
Image of page 34

You've reached the end of your free preview.

Want to read all 38 pages?

  • Spring '12
  • NAVABI
  • loop, do-while loop

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes