Looping Structures
Introduction
This section builds on the concepts of the Simple Sequence Structure and Decision Structures. Like Decision Structures, a Looping Structure relies on a boolean test. Unlike Decision Structures, the boolean test is used as an entry or exit condition of the Looping Structure, i.e., a repeating block of code. The Looping Structure is used to surround code that needs to run repeatedly until the exit condition is met. These concepts will be used throughout this course.
There are only 2 logic, flowchart, structures for loops, Pre-test and Post-test. These will be explored in the coding topics below.
Pre-test Loop
while(test)
{
Process1;
Process2;
}
A pre-test loop (While loop) has the test before the process. This means that the process executes 0 or more times. In the factorial example, if the user enters a 0 or 1 the process never executes.
Post-test Loop
do
{
Process1;
Process2;
}
while(test);
A post-test loop (sometimes called a do-until loop) has the test after the process. This means that the process executes 1 or more times.
Coding Topics
While Loop
Do Loop
For Loop
Totals & Sentinel Values
Nested Loops
Deciding Which Loop to Use
CONCEPT: Although most repetitive algorithms can be written with any of the three types of loops, each works best in different situation.
Common Errors to Avoid
- Using the increment (
++
) or decrement (--
) operator in the wrong mode. - Forgetting to enclose the bool expression in a
while
loop or ado-while
loop inside parentheses. - Placing a semicolon at the end of a
while
orfor
loop’s header. - Forgetting to write the semicolon at the end of the
do-while
loop. - Forgetting to enclose multiple statements in the body of a loop in braces.
- Using commas instead of semicolons to separate the initialization, test, and update expression in a for loop.
- Forgetting to write code in the body of a
while
ordo-while
loop that modifies the loop control variable. - Using a sentinel value that can also be a valid data value.
- Forgetting to initialize an accumulator to zero.
Practice Problems
Loop Problems 1
Loop Problems 2
Loop Problems 3