Do While Loop

Overview

A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block.[1]

Some languages may use a different naming convention for this type of loop. For example, the Pascal language has a repeat until loop, which continues to run until the control expression is true (and then terminates) — whereas a “while” loop runs while the control expression is true (and terminates once the expression becomes false).[2]

Discussion

Introduction to Test After Loops

There are two commonly used test after loops in the iteration (or repetition) category of control structures. They are: do while and repeat until. This module covers both.

Understanding Iteration in General – do while

The concept of iteration is connected to possibly wanting to repeat an action. Like all control structures, we ask a question to control the execution of the loop. The term loop comes from the circular looping motion that occurs when using flowcharting. The basic form of the do while loop is as follows:

do
    some statements or action
    some statements or action
    some statements or action
    update the flag
while the answer to the question is true

In most programming languages the question (called a test expression) is a Boolean expression. The Boolean data type has two values – true and false. Let’s rewrite the structure to consider this:

do
    some statements or action
    some statements or action
    some statements or action
    update the flag
while expression is true

Within the do while control structure there are three attributes of a properly working loop. They are:

  • Action or actions
  • Update of the flag
  • Test expression

The English phrasing is, “You do the action while the expression is true”. This is looping on the true. When the test expression is false, you stop the loop and go on with the next item in the program. Notice, because this is a test after loop the action will always happen at least once. It is called a test after loop because the test comes after the action. It is also sometimes called a post-test loop, meaning the test is post (or Latin for after) the action and update.

Understanding Iteration in General – repeat until

The concept of iteration is connected to possibly wanting to repeat an action. Like all control structures, we ask a question to control the execution of the loop. The term loop comes from the circular looping motion that occurs when using flowcharting. The basic form of the repeat until loop is as follows:

repeat
    some statements or action
    some statements or action
    some statements or action
    update the flag
until the answer to the question becomes true

In most programming languages the question (called a test expression) is a Boolean expression. The Boolean data type has two values – true and false. Let’s rewrite the structure to consider this:

repeat
    some statements or action
    some statements or action
    some statements or action
    update the flag
until expression becomes true

Within the repeat until control structure, there are three attributes of a properly working loop. They are:

  • Action or actions
  • Update of the flag
  • Test expression

The English phrasing is, “You repeat the action until the expression becomes true”. This is looping on the false. When the test expression becomes true, you stop the loop and go on with the next item in the program. Notice, because this is a test after loop the action will always happen at least once. It is called a “test after loop” because the test comes after the action. It is also sometimes called a post-test loop, meaning the test is post (or Latin for after) the action and update.

An Example

Do
    Output "What is your age? "
    Input user_age
    Output "What is your friend's age? "
    Input friend_age
    Output "Together your ages add up to: "
    Output user_age + friend_age
    Output "Do you want to try it again? y or n "
    Input loop_response
While loop_response == 'y'

The three attributes of a test after loop are present. The action part consists of the 6 lines that prompt for data and then displays the total of the two ages. The update of the flag is the displaying the question and getting the answer for the variable loop_response. The test is the equality relational comparison of the value in the flag variable to the lower case character of y.

This type of loop control is called an event controlled loop. The flag updating is an event where someone decides if they want the loop to execute again.

Using indentation with the alignment of the loop actions and flag update is the normal industry practice.

Infinite Loops

At this point, it is worth mentioning that good programming always provides for a method to ensure that the loop question will eventually be false so that the loop will stop executing and the program continues with the next line of code. However, if this does not happen, then the program is in an infinite loop. Infinite loops are a bad thing. Consider the following code:

loop_response = 'y'
Do
    Output "What is your age? "
    Input user_age
    Output "What is your friend's age? "
    Input friend_age
    Output "Together your ages add up to: "
    Output user_age + friend_age
While loop_response == 'y'

The programmer assigned a value to the flag before the loop and forgot to update the flag. Every time the test expression is asked it will always be true. Thus, an infinite loop because the programmer did not provide a way to exit the loop (he forgot to update the flag).

Consider the following code:

do
    Output "What is your age? "
    Input user_age
    Output "What is your friend's age? "
    Input friend_age
    Output "Together your ages add up to: "
    Output user_age + friend_age
    Output "Do you want to try it again? y or n "
    Input loop_response
While loop_response = 'y'

No matter what the user replies during the flag update, the test expression does not do a relational comparison but does an assignment. It assigns ‘y’ to the variable and asks if ‘y’ is true? Since all non-zero values are treated as representing true, the answer to the text question is true. Viola, you have an infinite loop.

Key Terms

action item
An attribute of iteration control structures.
at least once
Indicating that test after loops execute the action at least once.
do while
A test after iteration control structure.
infinite loop
A sequence of instructions which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over.[3]
repeat until
A test after iteration control structure alternative available in some programming languages.
test item
An attribute of iteration control structures.
update item
An attribute of iteration control structures.

References


License

Icon for the Creative Commons Attribution-ShareAlike 4.0 International License

Programming Fundamentals Copyright © 2018 by Authors and Contributors is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book