Skip to content Skip to navigation

OpenStax-CNX

You are here: Home » Content » Do While Loop

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Endorsed by Endorsed (What does "Endorsed by" mean?)

This content has been endorsed by the organizations listed. Click each link for a list of all content endorsed by the organization.
  • CCQ display tagshide tags

    This module is included in aLens by: Community College of QatarAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "Used in the Computer Programming Fundamentals I course."

    Click the "CCQ" link to see all content they endorse.

    Click the tag icon tag icon to display tags associated with this content.

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • OrangeGrove display tagshide tags

    This module is included inLens: Florida Orange Grove Textbooks
    By: Florida Orange GroveAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Click the "OrangeGrove" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • Houston Community College display tagshide tags

    This module is included in aLens by: Houston Community CollegeAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "COSC1436 Programming Funaamentals I"

    Click the "Houston Community College" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • Featured Content display tagshide tags

    This module is included inLens: Connexions Featured Content
    By: ConnexionsAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "Programming Fundamentals - A Modular Structured Approach Using C++ is a new course written by Kenneth Leroy Busbee, a faculty member at Houston Community College. This text introduces students to […]"

    Click the "Featured Content" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

Also in these lenses

  • Busbee's Compter Science display tagshide tags

    This module is included inLens: Busbee's Computer Science Lens
    By: Kenneth Leroy BusbeeAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "Texas Common Course Numbering: COSC1336 or COSC1436"

    Click the "Busbee's Compter Science" link to see all content selected in this lens.

    Click the tag icon tag icon to display tags associated with this content.

  • Lens for Engineering

    This module is included inLens: Lens for Engineering
    By: Sidney Burrus

    Click the "Lens for Engineering" link to see all content selected in this lens.

  • eScience, eResearch and Computational Problem Solving

    This module is included inLens: eScience, eResearch and Computational Problem Solving
    By: Jan E. OdegardAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Click the "eScience, eResearch and Computational Problem Solving" link to see all content selected in this lens.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

Do While Loop

Module by: Kenneth Leroy Busbee. E-mail the author

Summary: An introduction to the do while control structure with examples in the C++ programming language.

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 the: do while.

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 every language that I know 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.

The do while Structure within C++

Syntax

The syntax for the do while control structure within the C++ programming language is:


do
  {
  statement;
  statement;
  statement;
  statement;    // This statement updates the flag;
  }
while (expression); 

Note:

The test expression is within the parentheses, but this is not a function call. The parentheses are part of the control structure. Additionally, there is a semicolon after the parenthesis following the expression.

An Example

Example 1: C++ source code: do while loop


do
  {
  cout << "\nWhat is your age? ";
  cin >> age_user;
  cout << "\nWhat is your friend's age? ";
  cin >> age_friend;
  cout >> "\nTogether your ages add up to: ";
  cout >> (age_user + age_friend);
  cout << "\nDo you want to do it again? y or n ";
  cin >> 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 normal industry practice within the C++ community.

Infinite Loops

At this point it's worth mentioning that good programming always provides for a method to insure 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:

Example 2: C++ source code: infinite loop


loop_response = 'y';
do
  {
  cout << "\nWhat is your age? ";
  cin >> age_user;
  cout << "\nWhat is your friend's age? ";
  cin >> age_friend;
  cout >> "\nTogether your ages add up to: ";
  cout >> (age_user + age_friend);
  }
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:

Example 3: C++ source code: infinite loop


do
  {
  cout << "\nWhat is your age? ";
  cin >> age_user;
  cout << "\nWhat is your friend's age? ";
  cin >> age_friend;
  cout >> "\nTogether your ages add up to: ";
  cout >> (age_user + age_friend);
  cout << "\nDo you want to do it again? y or n ";
  cin >> 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 within the Boolean concepts of the C++ programming language, the answer to the text question is true. Viola, you have an infinite loop.

Definitions

Definition 1: do while
A test after iteration control structure available in C++.
Definition 2: action item
An attribute of iteration control structures.
Definition 3: update item
An attribute of iteration control structures.
Definition 4: test item
An attribute of iteration control structures.
Definition 5: at least once
Indicating that test after loops execute the action at least once.
Definition 6: infinite loop
No method of exit, thus a bad thing.

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks