+ 1
Can someone please explain this code!?
"What is the output of the following code? int f = 1, i = 2; while (++ i < 5) { f* = i System.out.println(f); " I have copied this code from one of the quiz questions, and guessed the answer. The correct answer is 12. (I understand I have left out the closing curly brackets/braces ( } ), as i dont remember where they were in the question. 😜 So if anyone can help i would really appreciate it. Thanks in advance!! 👍🏻
20 Respuestas
+ 11
int f = 1, i = 2;
while (++i < 5) { //increments i, then checks if it it lower than 5
f *= i; //multiplies f by i
}
System.out.println(f); //prints f
The output of this code is 3*4 = 12.
+ 11
@Ahmad: ++i increments i by 1, so when we do the first loop, i is already equal to 3. So f is multiplied by i and becomes 1*3=3.
f *= i is a shorter synthax for f = f * i by the way.
+ 7
int f = 1, i = 2;
while (++i < 5) { //increments i, then checks if it it lower than 5
, so i=3
f *= i; //multiplies f by i
(f=f*i) now f =1*3, therefore f=3
//then loop again executes as i=4,therefore f=3*4
}
System.out.println(f); //prints f
+ 4
3*4 =12
the last value for i is 4 as 5 is not less thann 5
+ 3
12
+ 2
Thanks so much!! That totally makes sense now!! 😀👍🏻
+ 1
thanks all you are heros in C++
0
Wait. How the f became three?
0
thanks!
0
int f = 1, i = 2;
while (++ i < 5) {
f = f*i;
System.out.println(f); "
0
12
0
output is 12
0
Why i execute to 4
0
Why we do 3*4 again
0
youre welcome
0
What is the output of the following code?
int f=1, i=2;
while(++i<5) {
f*=i;
}
cout<<f;
answer :
12
0
3 I--> 4 => 12 !!!
0
12
0
12
0
12