Why is the answer 468? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why is the answer 468?

the question was: "What is the output of this code? int x = 1; int y = 2; for(;;) { System.out.print((x++)+(++y)); if(x = 3); break; } " like what does the for(;;) do? because I've never seen that on the course before

17th May 2017, 9:39 PM
ASDFG
ASDFG - avatar
8 Answers
+ 18
Shouldn't it be if(x==3) instead of if(x=3); ? In that case, the output should be 46 Iteration 1: x++ is 1, x is 2. Both ++y and y are 3 // prints 1+3= 4 // x is not 3, so break won't work Iteration 2: x++ is 2, x is 3. Both ++y and y are 4 // prints 2+4=6 // x is 3, so break works. // Loop is done and dusted 😌 https://code.sololearn.com/cUjb35huVV6S/?ref=app
17th May 2017, 10:19 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 4
Error. if(x = 3) This is not a boolean, this is assigning x to equal 3. To check if it is 3 use ==. if(x == 3) for(;;) is the same as while(true) its infinite. Although, When x is 3, the loop breaks. x = 1 y = 2 x++ + ++y = 4 x = 2, y = 3 x++ + ++y = 6. x is now 3. stop loop. Output: 46 Not 468
17th May 2017, 10:26 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
dunnu wut u guys r tuking abuwt
18th May 2017, 7:33 AM
Lucy Fifa
Lucy Fifa - avatar
+ 1
I think it is just for loop with empty conditions lfor( empty ; empty; empty){} EDIT - and without if(){ break ;} this loop will be infinite
17th May 2017, 9:42 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
+ 1
@Tashi @NimWing @Joe
17th May 2017, 9:43 PM
ASDFG
ASDFG - avatar
0
@Yaroslav So why is the answer so large, if it has empty condition's shouldn't it loop forever?
17th May 2017, 9:44 PM
ASDFG
ASDFG - avatar
0
break stops loop
17th May 2017, 9:45 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
0
thanks for clarifying @Shamima the person who wrote this as a challenge question got it wrong.
17th May 2017, 10:26 PM
ASDFG
ASDFG - avatar