0

java 16.1

How many times will the following loop run? for (int i = 2; i < 10; i = i*i) { System.out.println(i); } i think it should be 3 or until i<10 help me

16th Dec 2022, 5:05 PM
Alex Williams
Alex Williams - avatar
1 Answer
+ 4
The next value of i is calculated by multiplying with itself. As long as it's smaller than 10, the loop runs. 2 is the starting value 2 * 2 = 4 is smaller than 10, ok 4 * 4 = 16 the loop condition is false. So the loop runs only twice, with values of 2 and 4.
16th Dec 2022, 5:15 PM
Tibor Santa
Tibor Santa - avatar