2

What does this condition mean in a while loop?

 int x;
 cin >> x;
 while(x) {
   ...
 }
PerryC
  • 1,233
  • 2
  • 12
  • 28

2 Answers2

6

int has an implicit conversion to bool. Basically 0 converts to false, all nonzero values convert to true

So more verbosely, your condition would read

while (x != 0)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

As @CoryKramer says, when you have a condition which only contains a variable, even if is a char, int, float, etc. the value 0 is considered as false, and any other as true. If you are using pointers is the same: the NULL value is considered as false, and any other direction is considered as true.

mangasaske
  • 84
  • 2
  • 8