-
while loop question
If I had the following while loop:
Code:
int count = 0;
cin >> value;
while (value < 0 && count < 5)
{
cout <<"Value must be non-negative;"
<<"enter value again. " <<endl;
cin >>value;
count++;
}
What would the value of count after the loop is executed with the following data keyed in:
-1
-2
-1
0
3
--Would it be 3, b/c when it enters 0 then it jumps out?
thanks
-
The simple answer would be to compile it and run it
-
It is 3. That in my opinion is really poor programming though as it's going to increase the count even after you enter 0. In glancing over it you expect it to STOP counting when the value is not less than 0 but it counts one more. I would suggest the do while loop.