Why is the output this? Please help, hurry..

why is the output = 9?

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

I really need an explanation please help..
Step through it using a debugger. It's fairly obvious.
what's a debugger? XD sorry, I'm still a beginner..
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?
closed account (48T7M4Gy)
Change c=8 to c =17. What output do you get?
is it because "012345678" are 9 digits so i=9?

No. Well, not exactly. You are incrementing by one each time. when you get to 8 it is still passing, so it increments again to 9. When it tests again it fails and comes out of the loop. So at that point i=9.

Read this:
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
Topic archived. No new replies allowed.