how did it came to this output?

why is the output = 9?

I got the desired output, which is "9"
but how? I need an explanation please.. I'm a beginner

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int c=8,i=0;
for(i=0;i<=c;i++)
{
}
cout<<i;
getch();
return 0;
}

I really need an explanation please help..
I got the output that I wanted.. which is "9"..
but my question is, how did it came to this output?
is it because "012345678" are 9 digits so i=9?
You are doing "i <= 8"
Which will make it 8 <= 8 in the last pass.
When that pass is done, it will increment it again.
Because you define (i<=8), it mean that when it's 8 the result is until OK ! That's way FOR LOOP do i++ --> i = 9; And Then 9 > 8 (c), the loop is over.
Last edited on
And you need to know about FOR Loop. And Here is :

***** FOR Loop steps *****
For example:

for(int i = 0; i <= 8; i++){

//do something
}

1) i (int) is set 0 --> " i " is checking the condition " i <= 8" -> at present it's ok! And then it's going into the loop to "do something".

2) after did something into FOR Loop, "i++" --> "i" is checking again the condition. And then it's going into the loop to "do something".

3) Do again the second step. When "i" is not the same condition (9 ( i ) >= 8). The loop is over !!!

DONE!
Last edited on
Topic archived. No new replies allowed.