1. ## A question about while loop

I thought I understood while and for loops quite well. However, recently I saw a program which had a while loop which only contained:

while (1)
Can anyone please help me understand as to what condition is being tested here for being false of true? Thanks a lot.

Arooj

2. while(1) == while(true)

I have barely used it, actually come to think of it, I don't think I ever have. But it could be used to show infinite loops.

1 == true

3. Thanks Alpha, if it is an infinite loop, then how does the program terminate. Actually I had to write a program which keeps on taking input until it encounters 2 successive instances of zeros. This is how I wrote it. Tell me if this is the correc way.

void ()
{
int num1, num2;
cout << "Enter a digit: ";
cin >> num1;
cout << "Enter a digit: ";
cin >> num2;
while (1) {
if (num1 == 0 && num2 == 0)
break;
cout << "Enter a digit: ";
cin >> num1;
if (num1 == 0 && num2 == 0)
break;
cout << "Enter a digit: ";
cin >> num2;
}

}

while (num1 != 0 && num2 != 0)

4. then it will loop while num1 and num2 does not equal 0...so it will loop forever until num1 and num2 equal to zero...

5. Code:
```    int num1,num2;
cin >> num1 >> num2;
while(num1!=0 && num2!=0) {
num1 = num2;
cin >> num2;
if(!cin.good()) {
cerr << "invalid input (expecting digit)" << endl;
break; // or throw something
}
//      do something with num1 & num2
}```
The previous code would fail on 5 0 0, assuming you wanted to stop on those consecutive zeros.

6. Thanks a lot. Actually program only checks 2 separate inputs to make sure both are not zeros. Thanks again.
Arooj