Why use while(true)?

Is there an alternative to using a while(true) loop and why is it useful here?
Could (t.kind=='*'||'/') be used instead?

instead of this "while(true)"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   while(true) {
        switch (t.kind) {
        case '*':
            left *= primary();
            t = ts.get();
break;
        case '/':
            {
                double d = primary();
                if (d == 0) error("divide by zero");
                left /= d;
                t = ts.get();
                break;
            }
        default:
            ts.putback(t);     // put t back into the token stream
            return left;
        }


Is while (true) simply an infinite loop, created for the purpose of being an infinite loop? Why doesn't the above example have any breaks, the breaks are only in the switch statement.

Why does while(cin) work? in a while(cin) loop. Can while accept anything as an expression?
Last edited on
Is while (true) simply an infinite loop, created for the purpose of being an infinite loop?
Yes.

Why doesn't the above example have any breaks, the breaks are only in the switch statement.
It has a return statement (on line 15) which will break the loop as well.

Why does while(cin) work?
The isteam/cin has an overloaded bool operator which is used by the while() statement:

http://www.cplusplus.com/reference/ios/ios/operator_bool/

It returns false when an error occurs (like eof) otherwise true. If ts is an istream it would be better to use that for the while().

Can while accept anything as an expression?
No, while (like if, for) accepts numeric expressions only. 0 is negative otherwise positive.


The loop above is set to run once because we have a return statement that breaks the loop. Why not write the code without the loop?

Modifying the code above I wrote the code below and deleted the while loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{


    char t ='*';

        switch (t) {
        case '*':
            cout<<"k\n";
            break;
        case '/':break;
        default:
            cout<<"t";   // put t back into the token stream
        }
        cout<< t << ;
        return 7;

Last edited on
No it isn't "set to run once". The return isn't executed every iteration, but only if the default of the switch is taken.
i modified it because the original code came broken. i now edited the original post to reflect that. In those instances it would have looped because of no break.

Would these edits change the question below?
The while loop will run until a break, and all cases have breaks... so it will only run 1 time?
Since it only runs 1 time, because of our breaks, why make the loop in the first place?

The calculating program I got this from worked even if I removed a while(true) loop from the main function.
Last edited on
you don't seem to fully understand break.
break ends the nearest thing.
if you have

while(true)
while(true)
break

it will never end, because the outer loop isnt broken.

same for switches, it only affects the case, not the loop around the case, to break in a case.

there are a bunch of ways to get out of while(true) loops. .. goto, return, break, error handler (throw), to name a few of them.

all that to say,

writing stuff to depend on break seems clunky to me. I prefer the rewrites that avoid this approach, but its a style thing.
Last edited on
Ok I understand now.

I just didn't understand. I saw that a return inside the switch statement broke the loop and it was not the breaks in the switch statement.

Thanks for the clarfication folks.
Topic archived. No new replies allowed.