For loop condition statement
Nov 16, 2015 at 8:26am UTC
This is a bit of a silly question, but why does the following not execute the body
1 2
for (int i = 0; i ; i++)
cout << "print " << endl;
i is suppose to be zero, which is true. Doesn't that mean the body should execute.
Its just confusing me because an expression like the following can execute, but the for loop does not.
1 2 3 4
int num = 2;
if (num)
cout << "print" << endl;
Nov 16, 2015 at 8:43am UTC
for (int i = 0; i ; i++)
Because thats not how for-loops work. You're suppose to tell if how long it should run. If you only want it to run once, then you wouldnt need a for-loop at all would you?
Nov 16, 2015 at 9:34am UTC
>> i is suppose to be zero, which is true.
i is zero on first iteration, which is false in C++ (false equals zero)
Nov 16, 2015 at 5:35pm UTC
Thanks for the answering the question Kevin C, makes sense now actually.
Topic archived. No new replies allowed.