Here's one of the quiz questions from this site:
"What is the final value of x when the code for(int x=0; x<10; x++) is run?"
Why is the answer 10? It clearly says "x < 10" in the for loop. I'm pretty sure 10 isn't less than 10.
Printable View
Here's one of the quiz questions from this site:
"What is the final value of x when the code for(int x=0; x<10; x++) is run?"
Why is the answer 10? It clearly says "x < 10" in the for loop. I'm pretty sure 10 isn't less than 10.
Here's a walkthrough.
Thus, at the end of the loop, x == 10.Code:x=0;
x<10? yes
add 1 to x
x<10? yes(x=1)
add 1 to x
x<10? yes(x=2)
...
add 1 to x
x<10? yes(x=9)
add 1 to x
x<10? no(x=10)
loop done
from 0 to 9 = 10.
because the loop doesnt stop until the midlle condition is no longer true. At 9, the condition is still true so it iterates and then increments x (so x is now 10), then checks the condition, sees that x is no longer less than 10, and exits the loop. Get it?
Oh...um...ok :)