- 1

How many times does what execute? Plz break it down into steps. And explain.

How many times will the following loop execute? int x = 1; while (x++ < 5) { if (x % 2 == 0) x += 2; }

26th Jan 2017, 4:33 PM
Christopher
2 Respuestas
+ 2
First, x is equal to 1 which is smaller than 5, therefore the loop will execute. After comparing x to 5, it will be incremented and now has the value 2. The if-statement is true and x is set to 4. This is still smaller than 5 and the loop will be executed again. x is then incremented to 5 and the loop won't repeat again.
26th Jan 2017, 6:20 PM
Iris B
Iris B - avatar
+ 1
Thanks I understand, so because x will pass the limit the second time we try to execute the code, it won't because its 6.
26th Jan 2017, 11:02 PM
Christopher