How to decipher this loop?


#1

I am currently working through this exercise right now to find out the end value of i, x, and y. So far, I think that

i = 3
x = 15

But I am not sure of y.

Also, for i, I know the loop keeps going as long as i is less than 3, but does the loop go for one more round once i hits 3, thus making x increment up by another 5?


int i = 0;
int x = 0;
int y;

while (i<3) {
   x = x+5;
   i++;
}

#2

No, the loop does not iterate a fourth time. i < 3 is called the exit condition. i will be 3 because it is set (incremented) before terminating the loop. x will be 15 as expected.


#3

Thank you for your help! It makes a lot of sense! Do you know what y would be?


#4

y is undefined, meaning it has no value.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.