+ 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
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
+ 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
+ 2
dunnu wut u guys r tuking abuwt
+ 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
+ 1
@Tashi @NimWing @Joe
0
@Yaroslav So why is the answer so large, if it has empty condition's shouldn't it loop forever?
0
break stops loop
0
thanks for clarifying @Shamima
the person who wrote this as a challenge question got it wrong.