Home > other >  Does while loop consider zero 0 as a False Condition
Does while loop consider zero 0 as a False Condition

Time:07-02

I have a Question About While Loop.

int space = 4;
while(space){
    cout<< "*";
    space --;
}

This While Loop will run 4 times and stop when value reaches to Zero 0, So my Question is we do not specify any condition like while(space > 0){...} then why it Stop.

Or this Zero 0 consider as False , and first our while loop is true and when Reaches to 0 it becomes False and Stop.

Please Tell me , i am little confused about it.

CodePudding user response:

int gets converted to bool in a boolean context using space != 0.

CodePudding user response:

The while loop takes a condition which is a bool. In this case, you've passed in an int instead, which will get implicitly converted to bool. That bool will be false if the int is 0, and will be true for any other value.

  • Related