• 02-16-2003
Arooj
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
• 02-16-2003
alpha
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
• 02-16-2003
Arooj
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)
```    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     }```