LOOPS Control Structures Types of Loops The while loop The do/while loop The for loop Nested Loops The break and continue options Control Structures Sequence -- one after another Selection -- take one path or another if if...else switch Iteration -- loop while do...while for Types of Loops Pretest -- test a condition each time BEFORE the loop is executed (while) Posttest -- test a condition each time AFTER the loop is executed (do...while) Fixed repetition -- execute the loop a pre-determined number of times (for) The while loop while (expression) { statement-1; statement-2; ... statement-n; } if expression != 0 (true), execute block and return to while statement if expression == 0 (false), skip block If expression false first time encountered, statement block not executed even once while (melt < final_score) { cin >> plenum; melt += plenum; discrep = mass * accel; } while (number != 0) {...} same as... while (number) {...} Be careful of un-ending loops... while (melt < final_score) {...} Either melt must increase or final_score must decrease... The do/while loop do { statement-1; statement-2; ... statement-n; } while (expression); If expression false first time encountered, statement block still executed once if expression != 0 (true), execute block again and return to while statement if expression == 0 (false), do not execute block again Note the important semi-colon after (expression) do { pay = base - deduct + overtime; hours -= BASE_HOURS; } while (hours > 0); do {...} while (number != 0); same as... do {...} while (number); The for loop for (initialization; expression; increment) { statement-1; statement-2; ... statement-n; } for (count = 1; count <= 12; ++count) { cin >> temp; cout << "The temperature at " << count; cout << ":00 is " << temp << endl; } Initialization -- give initial value to variable Expression -- test variable against some limit Increment -- Add (or subtract) something (usually 1) to (from) variable Loop variable (count) can be used in loop, but probably should not be altered... // Caution - this does not do what you want... for (count = 1; count <= 12; ++count) { cin >> temp; cout << "The temperature at " << count; cout << ":00 is " << temp << endl; count = count +1; } for (count = 0; count <= TABLE_SIZE; count = count + 5) {...} Suppose TABLE_SIZE is -9. Loop statements will not be executed even once (pretest loop). for (count = 25; count > 0; --count) {...} for (count = 25; count; --count) {...} for (letter = 'a'; letter <= 'z'; ++letter) Nested Loops for (hours = 0; hours < 24; ++hours) { for (mins = 0; mins < 60; ++mins) { for (secs = 0; secs < 60; ++secs) { ... } } } While and do...while loops can be nested as well The break and continue options break statement -- immediately terminates loop Usually used with if statement within loop to terminate if some condition develops while (radiation > LOWEST) { ... if (radiation > HIGHEST) break; ... } Some loops are un-ending on purpose... char code; const char STOP = 'q'; while (1) { cin >> code; if (code == STOP) break; ... } continue statement -- skips this iteration Usually used with if statement within loop to avoid some problems but to continue the looping mechanism while (input_flag == MORE_DATA) { ... if (group_size == 0) continue; average = sum / group_size; ... }