You are here

An Example

6 February, 2015 - 17:07

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

do  {  cout  << "nnWhat is your age? ";  cin  >> age_user;  cout  << "nnWhat is your friend's age? ";  cin  >> age_friend;  cout  >> "nnTogether your ages add up to: ";  cout  >> (age_user + age_friend);  cout  << "nnDo 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 fag 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 fag variable to the lower case character of y.

This type of loop control is called an event controlled loop. The fag 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 fag update is normal industry practice within the C++ community.