2

What does this condition mean in a while loop?

 int x;
 cin >> x;
 while(x) {
   ...
 }
PerryC
  • 1,173
  • 1
  • 11
  • 27

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
  • 107,498
  • 14
  • 145
  • 201
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
  • 74
  • 2
  • 8