-3

I do not understand how this loop repeats when the user inputs 'Y'. All of the code is in the "do" part; and the do part repeats once? After that while statement is just a return statement.

int main()
{   
   int score1, score2, score3; //three scores
   double average;             // Average score
   char again;                 // To hold Y or N input

   do
   {
       /Get three Scores.
       cout << "Enter 3 scores and i will average them: ";
       cin >> score1 >> score2 >> score 3;

       //calculate and display the average
       average = (score1 + score2 + score 3)/3.0;
       cout << "The average is " << average << :.\n;

       // Does the user want to average another set?
       cout << "Do you want to average another set? (Y/N) ":
       cin >> again;
   } while(again == 'Y' || again == 'y');

    return 0;   

}

The text book explanation is too brief and not clicking in my head. Thank you for your time.

VickTree
  • 889
  • 11
  • 26
  • 1
    If the user inputs a 'Y', it will repeat the loop. Why do you expect it to not repeat? – VHS Jan 15 '19 at 16:43
  • I know i am missing something but there is nothing in the while loop, how does entering 'Y' make it go back? do is supposed to happen once and then the while loop executed. I am not understanding what is making the loop go back around – VickTree Jan 15 '19 at 16:44
  • 1
    `while ()` indicates the condition which repeats the do code. It is not the loop. – Michael Surette Jan 15 '19 at 16:47

5 Answers5

3

What you are looking at is a do-while loop. It is a different construct than a while loop.

From cppreference.com, emphasis mine:


'while' loop

while ( <condition> )
{
    // code
}

Executes a statement repeatedly, until the value of condition becomes false. The test takes place before each iteration.


'do-while' loop

do
{
     // code
} while ( <condition> );

Executes a statement repeatedly, until the value of expression becomes false. The test takes place after each iteration.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
3

You don't have a while loop after the do. You don't have a while loop anywhere! The while is just a way of terminating the do loop.

Roy
  • 101
  • 4
1

I'll reduce it to the relevant parts, and in pseudo code:

do
{
   // do stuff that's irrelevant here
   ask user to type Y or N and store into "again" // that's the cin >> again line
}
while (again is Y or y)

The key is that the loop runs until the variable "again" is either Y or y.

And the variable again is set inside the loop, based on the user input.

Take these two things together, and you find that the loop runs until the user inputs Y or y.

Kevin Keane
  • 1,506
  • 12
  • 24
1

I know i am missing something but there is nothing in the while loop, how does entering 'Y' make it go back? do is supposed to happen once and then the while loop executed. I am not understanding what is making the loop go back around

The while keyword expects a condition to determine whether the code within its body should be executed or not. In your case you have given a condition that the loop should be executed if the variable again is set to either 'y' or 'Y'.

} while(again == 'Y' || again == 'y');

Refer to this link for a detailed explanation on while vs do-while loop constructs.

VHS
  • 9,534
  • 3
  • 19
  • 43
1

The body of the loop starts at the { after the do and ends at the } before the while.

It seems like you are familiar with the while loop and now you just need to understand that a do-while loop has slightly different structure (condition comes after the body as opposed to before).

Example:

while ( condition ) {    // while loop
     body
}

do {                     // do-while loop
    body
} while (condition);     

Note that they are not equivalent, you typically choose one over the other depending on whether it is more natural to check the condition before or after doing the iterations.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185