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?
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;
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.