C++ Programming Lecture 7 Control Structure I (Repetition) Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department
while Repetition Structure I Repetition structure Programmer specifies an action to be repeated while some condition remains true. Also called looping or simply loop. Psuedocode while there are more items on my shopping list Purchase next item and cross it off my list while loop repeated until condition becomes false where the next line of code after while loop will be executed. Example int product = 2; while ( product <= 1000 ) product = 2 * product; The body of the while loop is the code block contained within the braces after the while, otherwise it is the first statement after the while only. 2
while Repetition Structure II Flowchart of while loop. Infinite loop: Logical error in the while structure. The condition of the while is always true, i.e. the body of the while loop does modify the condition value. product <= 1000 false true product = 2 * product 3
Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: The name of a control variable (or loop counter). The initial value of the control variable. The condition that tests for the final value of the control variable (i.e., whether looping should continue). The increment (or decrement) by which the control variable is modified each time through the loop. Example: int counter =1; //initialization while (counter <= 10){ //repetition condition cout << counter << endl; ++counter; //increment } As possible avoid the usage of floating point counter values since floating points are approximate. 4
for Repetition Structure I Handles all the details of counter-controlled repetition in a concise way. The general format when using for loops is: for (initialization; LoopContinuationTest; increment/decrement ) statement Example: for( int counter = 1; counter <= 10; counter++ ) cout << counter << endl; Prints the integers from one to ten Pay attention to the off-by-one error. 5
for Repetition Structure II After the condition of the for is violated, the first statement after the for loop is executed. The for loop body is the code block after it (if braces exist) otherwise it is the first statement after for structure. for loops can usually be rewritten as while loops: initialization; while ( loopcontinuationtest){ statement increment; } Initialization and increment as comma-separated lists for (int i = 0, j = 0; j + i <= 10; j++, i++) cout << j + i << endl; 6
for Repetition Structure III Scope of the counter variable defined inside for loop differs based on the used C++ compiler: Known only inside for structure. Or known inside the whole program. The three parts of the for loop are optional, if condition is omitted this will create an infinite loop since the compiler assumes that the for condition is true. for loop parts can contain arithmetic expressions. Flowchart of the for loop is similar to the while loop. 7
Examples Using for Loop On board. 8
do...while Repetition Structure I The do/while repetition structure is similar to the while structure, Condition for repetition tested after the body of the loop is executed Format: do { statement } while ( condition ); Example (letting counter = 1): do { cout << counter << " "; } while (++counter <= 10); This prints the integers from 1 to 10 Pay attention to post/pre increment/decrement. All actions are performed at least once. 9
do...while Repetition Structure II Flowchart of do...while loop action(s) condition true false 10
break Statement break Causes immediate exit from a while, for, do/while or switch structure Program execution continues with the first statement after the structure Common uses of the break statement: Escape early from a loop Skip the remainder of a switch structure Using break outside a loop or switch (e.g. inside if/else) statement is a syntax error. 11
continue Statement continue Skips the remaining statements in the body of a while, for or do/while structure and proceeds with the next iteration of the loop. Also, can be used with switch. In while and do/while, the loop-continuation test is evaluated immediately after the continue statement is executed In the for structure, the increment/decrement expression is executed, then the loop-continuation test is evaluated Using continue outside a loop or switch (e.g. inside if/else) statement is a syntax error. 12
Additional Notes This lecture covers the following material from the textbook: Chapter 2: Sections 2.7, 2.13, 2.14, 2.15, 2.17, 2.18 13