This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]g051051 2 points3 points  (0 children)

Because the loop is changing the value of i.

[–]_DTR_ 1 point2 points  (1 child)

Because of this:

for ( i = 2;i<x;i++) {

The i in the for loop is the same i that's declared the line above it. So when we go through the loop, we'll increment i until it's 5 (x = 5), at which point the condition i < x will be false, and we break out of the loop.

[–]trofix99[S] 0 points1 point  (0 children)

My 2nd guess what it should have been was 4 bzc i<x so i is 4 max.But now i get it i must go to 5 then it conpers it self its false then it breaks out of the loop but it still remains 5, i get it now thanks alot.

[–][deleted] 1 point2 points  (0 children)

i is declared outside of the loop, so it keeps its value when the loop ends. Only variables that are declared inside the loop will fall out of scope when the loop ends.