-1

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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Hikikomori
  • 39
  • 6
  • there's a break instruction below, that is the loop exit. Or even the return would work (exit from the function) – Marco A. Apr 09 '14 at 06:55
  • This question appears to be off-topic because it is about not looking up basic language keywords, such as break and return. – sashoalm Apr 09 '14 at 06:58
  • @DavidKernin, but I think that's break works only for switch condition, not while loop. – Hikikomori Apr 09 '14 at 06:58
  • The break breaks the switch, not the loop. But you're correct that the return is what leaves the loop. – Timothy Jones Apr 09 '14 at 06:58
  • Why don't you first look up all the language keywords. StackOverflow is not a good place to ask questions that are answered by an introductory book. – sashoalm Apr 09 '14 at 06:59
  • @Sashoalm, if so, I'm really sorry I'm very beginner on this, but I don't know where to ask this, because I learn by myself. So only on you guys, I can depend on. – Hikikomori Apr 09 '14 at 07:00
  • Okay I'll delete this question, sorry for troubling you. – Hikikomori Apr 09 '14 at 07:01
  • @user3234329 the switch isn't a loop, you just execute it once. So break refers to the innermost loop where that instruction is located (i.e. your while) – Marco A. Apr 09 '14 at 07:01
  • Please go easy on the criticism: in my opinion this is not a bad question at all: especially given the `break` statements in the `switch`. – Bathsheba Apr 09 '14 at 07:04
  • @user3234329 Reading a book **is** learning by yourself. – sashoalm Apr 09 '14 at 07:04
  • @sashoalm: This question is fine (more or less); sometimes beginners need to know where to look/what to look for, and a little human interaction is both more efficient and more effective than no human interaction in the learning process. – Cornstalks Apr 09 '14 at 07:06

4 Answers4

2

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++.

Community
  • 1
  • 1
Timothy Jones
  • 21,495
  • 6
  • 60
  • 90
2

If you look closely, there is a

return;

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

Toni Makkonen
  • 131
  • 1
  • 5
1

(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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I myself as a beginner realize there are some of the program which I feel peculiar on this book, but I'm not sure with myself. The important is I get the knowledge from you and all of guys here. In case my english not so well. – Hikikomori Apr 09 '14 at 07:14
0

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!

laune
  • 31,114
  • 3
  • 29
  • 42