+ 3

Short Crazy Quiz Java

[In The Last Java Quiz In SoloLearn] /** Start Quiz **/ What is the output of the following code? int f=1, i=2; while(++i<5) f*=i; System.out.println(f); /** End Quiz **/ Please help me ! The Correct Answer Is : 12; Buttttttt !!! I Don't know Why not 48 Or 60 :/ . " ++i will increment the value of i, and then return the incremented value. " f=1 i=2 --First Loop i (in the while) =2 , original i = 3 because ++i; f = 3 because 1*3 = 3; --Second Loop i (in the while) = 3, original i = 4 because ++i; f = 12 because 3*4 = 12; --third Loop i (in the while) = 4, original i = 5 because ++i; f = 60 because 12*5 = 60; Whyyyyyyyyyyyyyyyyyyyyyyyy The Answer is 12 ????????! I will kill myself if I do not know why ! :\(

18th Jun 2018, 10:41 PM
Yousef Eabitan
Yousef Eabitan - avatar
4 Respostas
+ 5
the I is pre incremented so it's value before the condition is 3. 1 × 3; system.out.println(f); f = 3. now it would loop again this time i would be 4 4 x 3; system.out.println(f) = 12 for the last time I was pre incremented so I became 5 and hence the condition became false so the loop stopped. hope this helps
18th Jun 2018, 11:05 PM
Peter Claver
Peter Claver - avatar
+ 2
I hope that now you dont kill yourself 😒😒😒
18th Jun 2018, 11:34 PM
KrOW
KrOW - avatar
+ 2
Thanks for All <3 I Will No Kill My Self sorry :0 OK this is correct ? --First Loop i (in the while) =2 , original i = 3 because ++i; f = 3 because 1*3 = 3; --Second Loop i (in the while) = 3, original i = 4 because ++i; f = 12 because 3*4 = 12; --third Loop i (in the while) = 4, original i = 5 because ++i; f = 60 because 12*5 = 60;
19th Jun 2018, 12:01 AM
Yousef Eabitan
Yousef Eabitan - avatar
0
Yousef10 that would be wrong because when i turns 5 the loop breaks
19th Jun 2018, 12:03 AM
Peter Claver
Peter Claver - avatar