Only 5 of 1000 can trace this Java assignment-precedence brain-twister
Description
The image has a black background with white text. The top caption reads, "Uni stats says only 5 students out of 1k able to answer right:". Below that, a white code panel shows the exact Java snippet: public static void main(String[] args) { int a=9,b=4,c=7,d=1,e=3; while ((a=1)==1 && (b=2)==3 && (c=3)==3 || (d=4)==4 || (e=5)==7) { System.out.println(a + " " + b + " " + c + " " + d + " " + e); break; } } The joke relies on hidden assignments inside the while-condition and Java's operator-precedence/short-circuit rules, causing the loop to print "1 2 7 4 3" despite the initial values. It pokes fun at how few students (or interviewees) remember that `=` returns a value, `&&` binds tighter than `||`, and evaluation stops once an `&&` chain hits false
Comments
55Comment deleted
Seeing while((a=1)==1 && (b=2)==3 …) in a code review is that special moment you realize operator precedence isn’t the bug - someone just packed side-effects, job security, and a subtle “I quit” into one boolean
After 20 years of code reviews, I've learned that the difference between = and == in conditionals is what separates those who debug production incidents at 3am from those who cause them. Though honestly, any codebase that makes it past review with assignments in conditionals probably has bigger architectural problems than operator confusion
Ah yes, the classic 'assignment masquerading as comparison' interview hazing ritual. This is the kind of code that makes you appreciate why modern linters scream at you for assignments in conditionals. The real answer? Only 5 out of 1000 students got it right because the other 995 had already learned to write maintainable code and refused to engage with this production-ready nightmare on principle. If you unironically write conditions like this, your PR reviews must be absolutely *chef's kiss* brutal
1 & 2 = 0: bitwise AND's polite way of saying 'loop over, no println invited'
Prints 1 2 7 4 3 exactly once - also known as the mandatory code‑review where we enable a linter and ban assignment in conditionals forever
It prints “1 2 7 4 3” once - short-circuit assignments set a,b,d and skip c,e; academia calls it a clever exam question, production calls it a postmortem
1 2 3 4 5 ? Comment deleted
1 2 3 4 3 Comment deleted
1 2 7 4 3 Comment deleted
Nothing Comment deleted
5 is assigned to e and then it is compared to 7, so how can it go into the while body? Comment deleted
there's OR Comment deleted
1 2 7 4 3 Comment deleted
Should it even show anything Comment deleted
doesn't that continue to after or ? Comment deleted
https://en.wikipedia.org/wiki/Short-circuit_evaluation Comment deleted
Then, If we account both this Comment deleted
No this part makes the program not execute the rest of monominal (c=3) and skips to the d=4 part Comment deleted
I talked about it lower Comment deleted
Yes && Has bigger priority than || Think of it as multiplying first and summing later a*b*c + d + e _ a*b*c = 0 d = 1 e = 0 _ 0 + 1 +0 = 1 Comment deleted
1 2 3 4 3 Comment deleted
6 9 1 4 8 8 Comment deleted
1 2 7 4 3 Comment deleted
oh, jeez, realy) Comment deleted
) Comment deleted
1 2 7 4 3 Comment deleted
12743 Comment deleted
it's probably 12743, but I can also see it being 12343, if it's a trick question, and the third one gets evaluated, cuz there's something after it Comment deleted
I guess Java uses it Comment deleted
It won't print anything anyway Comment deleted
Because && has less priority than || iirc Comment deleted
AND has greater priority than OR Comment deleted
And this Comment deleted
I thought it reads from left to right without propority Comment deleted
It doesn't Comment deleted
12743? Comment deleted
It should give 12743 Comment deleted
They always have different priorities Comment deleted
alright it is 12743 Comment deleted
I checked Comment deleted
it’s 1 2 7 4 3 obviously, but RIGHT answer is «i don’t care» Comment deleted
seems like whole left hand part of first || is counted as a block Comment deleted
no it's not Comment deleted
it stops processing ANDs when finds first false and stops processing ORs when it finds first true Comment deleted
ah thanks Comment deleted
resulted exactly as this Comment deleted
AND and OR have same precedence Comment deleted
let me change position of and,or and see what happens Comment deleted
infinite loop? Comment deleted
break; doesn't agree Comment deleted
Yeah, I ran a quick investigation later and I know now my both guessings were wrong Comment deleted
or maybe syntax error? Comment deleted
The real answer to this is refactoring 😂👀 Comment deleted
a=1 (first assignment) | 1==1 : true, continue condition evaluation b=2 (second assignment) | 2==3 : false, short circuit break (AND FALSE operation), therefore all following && will be ignored c=7 (initial value remains - see comment above) d=4 (third assignment) | 4==4 : true, short circuit break (OR TRUE operation), therefore all following || will be ignored e=3 (initial value remains - see comment above) therefore 1 2 7 4 3 🤓 Comment deleted
1 2 7 4 5 piss easy Comment deleted