Question

I've read a book by Mark Lee, C++ absolute beginner, and one of the code snippet is :

while(true)
{
    cout << description.c_str() << "\n\n";
    int response = 0;
    do
    {
        cout << "What would you like to do?\n";
        if(enemy)
        cout << "1) Attack the evil "
             << enemyName.c_str() << "\n";
        else if(!enemy)
        cout << " 1) Move to the next room.";
        if(treasure)
        cout << " 2) Pick up the "
             << treasureName.c_str() << "\n";
        cin  >> response;
    }while(response < 1 || response > 2);

    switch(response)
    {
        case 1 :  if(enemy)
                  {
                    enemy = !enemy;
                    cout << "You slay the deadly "
                         << enemyName.c_str() << "\n";
                  }
                  else if(!enemy)
                    return;
                  break;
        case 2:   treasure = !treasure;
                  cout << "You pick up the "
                       << treasureName.c_str() << "\n";
                  break;
    }
}

I think you can ignore about what this program intention is, but the question is, why the part of "while(true)" is exist ? I think, there are no ways out of the loop, right ? Because, I think the "true" value is always return 1, and the "while(true)" part is same with "while(true == 1)", so this loop is like infinity loop, am I wrong or ? Any help is appreciated.

Était-ce utile?

La solution

Yes:

this loop is like infinity loop

You're correct in that while(true) is an instruction to loop forever.

However, there are a few other ways to exit loops:

  • A return statement will exit the function (and consequently also terminate the loop)
  • A break statement will exit the closest for, while or switch statement.
  • A goto statement might cause the code to jump to a label outside the loop
  • An exit() call will cause the whole program to terminate.
  • A throw statement will throw an exception that will break out to the nearest appropriate catch statement (which might be outside the loop).

In this case, the return; near the bottom of the loop causes the function to exit.

The break; statements do not cause the loop to stop, because they belong to the switch.


A caveat on the use of goto - many programmers consider it poor style, as it can lead to code that is difficult to follow. There is quite a bit of further discussion on this question. In C++, typically throw is more appropriate for situations where goto might be used.


However, there are situations in pure C where goto can be very useful. This answer provides an excellent overview of why goto is historically considered poor style, and even provides some examples of where it might be appropriate to use goto.

Of course, a good rule for beginners might be "pretend goto doesn't exist". Especially in C++.

Autres conseils

If you look closely, there is a

return;

statement in the code. This will exit the loop and the function the loop is in.

(This is worth answering since it has fooled at least one 5,000 reputation user and therefore demonstrates the importance of writing code that is clear).

The return statement is the loop terminator. This will exit the function.

It's buried deep within the function. I'd criticise the code therefore for that style: it's hard to follow and difficult to debug.

The only exit from the while(true){...} is the return statement, which terminates the surrounding function.

I don't know the book, but I've never seen another suggestion of writing if statements this redundant way:

if(enemy)
{ ...
}
else if(!enemy)
{...
}

Redundancy is usually to be avoided since it makes maintenance more difficult.

And I very much dislike:

    case 2:   treasure = !treasure;
              cout << "You pick up the "
                   << treasureName.c_str() << "\n";

This will let you pick up the treasure, but you stay in the loop and can select '2' once more, telling you "You pick up the x" once more, but negating variable treasure once more. Hmm, let's hope this isn't a full quote from the book!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top