- 1
Can anyone please explain the logic (a+=b=b-=c=c*=d/=20) ?
public class Program { public static void main(String[] args) { int a,b,c,d; a=b=c=d=20; a+=b=b-=c=c*=d/=20; System.out.print(a+" "+b+" "+c+" "+d+" "); //Output -> 20 0 20 1 } }
5 Antworten
+ 4
Kiruthik Kumar BCA evaluate from right to left, since they are all assignment statements.
All variables are initialized to the same value of 20.
d/=20 divides d/20 and assigns 1 to d
c*=d multiplies c*1 and stores 20 in c
c=c no change
b-=c subtracts b-20 and stores 0 in b
b=b no change
a+=b adds a+0 and stores 20 in a
By this analysis you can see why it outputs
20 0 20 1
+ 3
Kiruthik Kumar BCA
d or d/20 is 20/20 = 1
c or c * 1 = 20
b or b-=20 is 20 minus 20 = 0
a or a+=0 is 20 + 0 = 20
+ 2
a=b=c=d=20;
a=20;b=20;c=20;d=20;
-----------
d=d/20=1
c=c*d=20
b=b-c=0
a=a+b=20
a+=b=b-=c=c*=d/=20;
Read from right to left ..
System.out.print(a+" "+b+" "+c+" "+d+" ");
//Output -> 20 0 20 1
+ 2
+ 1
a+= 20+0 = 20
b-= 20-20 = 0
c*1= 20
d/20 = 20/20 = 1
# 20 0 20 1 #