dont understand forloop

#include <iostream>

using namespace std; // So the program can see cout and endl

int main()
{
// The loop goes while x < 10, and x increases by one every loop
for ( int x = 0; x < 10; x++ ) {
// Keep in mind that the loop condition checks
// the conditional statement before it loops again.
// consequently, when x equals 10 the loop breaks.
// x is updated before the condition is checked.
cout<< x <<endl;
}
cin.get();
}
after i compile why it will show 0,1,2,..... 9 x++ doesnt mean oledi x = x + 1

therefore cout<<x<<endl should be 1,2.... correct me if i wrong.

thanks in advance.




Answer this question

dont understand forloop

  • dealogic

    It does not increase x until it has executed once.

    1. Declare and set x to 0
    2. Check if x is less than 10, go to step 6 if not
    3. Execute the statement block: cout << x << endl;
    4. Increase x with 1
    5. Go to step 2
    6. Done

    x is 0 for the first cout.



  • dont understand forloop