0
who can explain me why it's 2 ?
How many times will the following loop run? for (int i = 2; i < 10; i = i*i) { System.out.println(i); }
9 odpowiedzi
+ 7
2*2=4
4*4=18
done with the loop.
you are multiplying I by itself and that is causing it to rise so fast.
+ 7
The loop that you have given is a for loop. A for loop will continue to execute for as long as the condition is met.
The condition in this loop is i < 10. So, as long as the variable 'i' has a value less than 10, the for loop will continue to execute.
When the loop starts, i = 2, and because 2 is less than 10, the code within the loop executes. After the first loop, 'i' is multiplied by itself. So, 2 * 2 = 4. Now the value of 'i' is 4 but because it is less than 10, the loop will execute again for the second time. And after the second loop the value of 'i' will again be multiplied by itself; 4 * 4 = 16.
After the second loop, the value of 'i' will become 16, and because 16 is higher than 10, the loop will not execute for a third time.
+ 6
2
+ 3
2 ans
+ 3
2
+ 2
2
0
2
0
2
0
2