0
for loop condition >= leads in "no output"
I have a question concerning the condition of a for loop. During a challenge there was a question about a for loop with a condition like this: for(int i=0; i>10;i++){...} Normally loops are no problem so far but my answer was wrong. I was wondering about that, so I tried to find out the reason. I have checked the loop in C and Java with the same result: "no output". I tried an older gcc compiler (C99) also because I thought about some trouble with newer compiler versions...but I didn't find a solution. Can anybody please help? Why is a condition in a for loop ...;>x;... or ...;>=x;... leading in "no output" ?
3 Answers
+ 5
The loop checks i > 10 condition even before it started, that loop actually never get started because <i> was zero. To verify this, print something after the loop, you'll see how the loop does nothing there : )
Hth, cmiiw
+ 3
You're welcome, been there too though, no big deal đ
+ 1
Yes, sure! Thx @Ipang !!
...and if I start at 11 the condition is true and the loop will never end...
I'm such a muppet... :(