+ 2
Java code how it work?
i found out that a++ without ++ the output still same. isn't it work like this? a = 3 , a > 1 (true) , a /= a++ a = 3 , a > 1 (true) , 3/= 3++ (3/3=1) https://code.sololearn.com/cHrIg0ivgwq9/?ref=app
5 ответов
+ 2
This code is equivalent to the following:
for(int a = 3;a > 1; /*a/=a++*/ ){
System.out.print(a);
a++; //a => 4
a/=a; //a => 1
// ? a>1=> false, break for.
}
http://www.cs.bilkent.edu.tr/~guvenir/courses/CS101/op_precedence.html
Trace:
a = 3;
? a > 1 => true, begin for
print => 3
a++ => 4
a/=a => 1
? a>1=> false, break for.
with this, this one is wrong. He is an infinity loop. Print out: 3222222......
for(int a = 3;a > 1; /*a/=a++*/ ){
System.out.print(a);
a/=a;
a++;
}
Trace:
a = 3;
? a > 1 => true, begin for
print => 3
a/=a => 1
a++ => 2
? a > 1 => true, iter for
print => 2
a/=a => 1
a++ => 2
? a > 1 => true, iter for
print => 2
a/=a => 1
a++ => 2
? a > 1 => true, iter for
infinity
+ 5
The ++ operator after variable has less priority than / so a /= a++ first do a/a then add 1 to a
+ 2
3 - it is first value of variable "a". Command print it is first command in the body of loop.
I corrected the comment just now - added the route of the current equivalent code.
+ 1
Michail Getmanskiy so output should be 3222222 ,an infinity code,but why it print out 3?
0
learn post and pre increment operators