C++ for Engineers and Scientists - Repetition Statements


Objectives

Basic Loop Structures

while Loops

Interactive while Loops

for loops

Loop Programming Techniques

 

Objectives (continued)

Nested Loops

do while Loops

Common Programming Errors

 

 

Basic Loop Structures

Repetition structure has four required elements:

Repetition statement

Condition to be evaluated

Initial value for the condition

Loop termination

Repetition statements include

while

for

do while

Basic Loop Structures (continued)

The condition can be tested

At the beginning: pretest or entrance-controlled loop

At the end: posttest or exit-controlled loop

Something in the loop body must cause the condition to change, to avoid an infinite loop, which never terminates

while Loops

while Loops (continued)

while Loops (continued)

Interactive while Loops

Interactive while Loops (continued)

Interactive while Loops (continued)

Interactive while Loops (continued)

break statement: forces an immediate break, or exit, from switch, while, for, and do-while statements

break statement violates pure structured programming, but is useful for breaking out of loops when an unusual condition is detected

Interactive while Loops (continued)

Interactive while Loops (continued)

continue statement: applies to while, do-while, and for statements; causes the next iteration of the loop to begin immediately

continue statement is useful for skipping over data that should not be processed in this iteration, while staying within the loop

Null statement: a semicolon with nothing preceding it; a do-nothing statement required for syntax purposes only

 

for Loops

for Loops (continued)

Altering list: provides the increment value that is added or subtracted from the counter in each iteration of the loop

If initializing list is missing, the counter initial value must be provided prior to entering the for loop

If altering list is missing, the counter must be altered in the loop body

Omitting the expression will result in an infinite loop

for Loops (continued)

for Loops (continued)

 

 

Loop Programming Techniques

These techniques are suitable for pretest loops (for and while):

Interactive Input within a Loop

Includes a cin statement within a while  or for loop

Selection within a Loop

Using a for or while loop to cycle through a set of values to select those values that meet some criteria

 

 

Loop Programming Techniques (continued)

Loop Programming Techniques (continued)

Loop Programming Techniques (continued)

Loop Programming Techniques (continued)

Interactive Loop Control

Variable is used to control the loop repetitions

Provides more flexibility at run-time

 

Loop Programming Techniques (continued)

Nested Loops

Nested loop: a loop contained within another loop

All statements of the inner loop must be completely contained within the outer loop; no overlap allowed

Different variables must be used to control each loop

For each single iteration of the outer loop, the inner loop runs through all of its iterations

 

Nested Loops (continued)

Nested Loops (continued)

do while Loops

do while loop is a posttest loop

Loop continues while the condition is true

Condition is tested at the end of the loop

    Syntax:

          do

                   statement;

          while (expression);

All statements are executed at least once in a posttest loop

do while Loops (continued)

do while Loops: Validity Checks

Useful in filtering user-entered input and providing data validation checks

 

 

 

 

Can enhance with if-else statement

 

 

Common Programming Errors

Making the off by one error: loop executes one too many or one too few times

Using the assignment operator (=) instead of the equality comparison operator (==) in the condition expression

Testing for equality with floating-point or double-precision operands; use an epsilon value instead

Common Programming Errors (continued)

Placing a semicolon at the end of the for clause, which produces a null loop body

Using commas instead of semicolons to separate items in the for statement

Omitting the final semicolon in a do statement

Summary

Summary (continued)