Why does “Hellow"not print even once ?
for (i=0;i> 10; i++)
cout<<"Hellow"
Answers
Explanation:
As you have it now, it won’t compile due to the missing for keyword and the equally missing semicolon at the end. Also, I can’t assume that i was declared prior to this point so I”m going to assume not in that case too. Fixing those (but not yet the logical error, just getting it to compile for now), you’d now have:
int i;
for (i==1;i<=3;i++) printf(“Hello\n”);
Now the logic error: The == does NOT initialize i, it merely compares the indeterminate value to 1, returning 1 is equal or 0 is not. That return value disappears completely and is NOT assigned to i.
Then, the preexisting indeterminate value of i is passed into the loop, which does the compare, print, increment business until i is no longer <=3 (if it doesn’t seg fault as this is actually an undefined result).
Due to the fact that it is indeterminate what i was at first, and the == has no effect on its value what-so-ever, this has undefined results. It could print 4 times like in your claim, 3 times (the expected value), 0 times, over 2 billion times, anywhere in between, or even “Segmentation Fault” (undefined behavior can do anything).
Final corcection should be:
int i;
for (i=1; i<=3; i++) printf(“Hello\n”);
Or (better yet, if you only intend to use i in the loop, should work in C unless you are using C89, which I think only K&R use nowadays…):
for (int i=0; i<3; i++) printf(“Hello\n”);
This is because starting at 0 and using < is preferred over starting at 1 and using <=. If you program a computer, you should think like one and start everything at 0.
Two == means compare the values.
One = means assign the value to the variable.
EDIT: Spelling corrections…typing too fast causes typos
Looping condition is wrong