0

could someone explain, please?

What is the output of the following code? int f=1, i=2; while(++i<5) f*=i; System.out.println(f);

7th Jan 2020, 6:21 AM
alp
2 Answers
+ 4
while (3<5) // true f=f*i=1*3=3 // f is 3 now while (4<5) // true f=f*i=3*4=12 // f is 12 now while(5<5) // false, the loop breaks So final value of f is 12.
7th Jan 2020, 7:04 AM
Avinesh
Avinesh - avatar
+ 1
Add some manual tracing for the variables, it may help understanding what happens int f = 1, i = 2; System .out.println ("Before loop f = " + f + ", i = " + i); while(++i < 5) { f *= i; System .out.println ("* In loop f = " + f + ", i = " + i); } System.out.println("After loop f = " + f + ", i = " + i);
7th Jan 2020, 6:44 AM
Ipang