Skip to content
DevMeme
4508 of 7663
Languages Post #4947 · source on Telegram

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

55
Anonymous ★ Top Pick 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
  1. Anonymous ★ Top Pick

    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

  2. Anonymous

    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

  3. Anonymous

    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

  4. Anonymous

    1 & 2 = 0: bitwise AND's polite way of saying 'loop over, no println invited'

  5. Anonymous

    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

  6. Anonymous

    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

  7. @sylfn 4y

    1 2 3 4 5 ?

  8. @cthulhu_dev 4y

    1 2 3 4 3

  9. @pointan 4y

    1 2 7 4 3

  10. @affirvega 4y

    Nothing

  11. @grandpa_the_kid 4y

    5 is assigned to e and then it is compared to 7, so how can it go into the while body?

    1. @CCZeroOne 4y

      there's OR

  12. Vitalik 4y

    1 2 7 4 3

  13. @trainzman 4y

    Should it even show anything

    1. @callofvoid0 4y

      doesn't that continue to after or ?

      1. @trainzman 4y

        https://en.wikipedia.org/wiki/Short-circuit_evaluation

        1. @trainzman 4y

          Then, If we account both this

    2. @M4lenov 4y

      No this part makes the program not execute the rest of monominal (c=3) and skips to the d=4 part

      1. @trainzman 4y

        I talked about it lower

    3. @scout_ca11sign 4y

      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

  14. @Similacrest 4y

    1 2 3 4 3

  15. @mold_in 4y

    6 9 1 4 8 8

  16. @lab0rat 4y

    1 2 7 4 3

  17. @grandpa_the_kid 4y

    oh, jeez, realy)

  18. @CCZeroOne 4y

    )

  19. @realVitShadyTV 4y

    1 2 7 4 3

  20. @callofvoid0 4y

    12743

  21. @CCZeroOne 4y

    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

  22. @trainzman 4y

    I guess Java uses it

  23. @trainzman 4y

    It won't print anything anyway

  24. @trainzman 4y

    Because && has less priority than || iirc

    1. @sylfn 4y

      AND has greater priority than OR

      1. @trainzman 4y

        And this

    2. @callofvoid0 4y

      I thought it reads from left to right without propority

      1. @trainzman 4y

        It doesn't

  25. @Samurhai 4y

    12743?

  26. @trainzman 4y

    It should give 12743

  27. @trainzman 4y

    They always have different priorities

  28. @CCZeroOne 4y

    alright it is 12743

  29. @CCZeroOne 4y

    I checked

  30. Егор 4y

    it’s 1 2 7 4 3 obviously, but RIGHT answer is «i don’t care»

  31. @callofvoid0 4y

    seems like whole left hand part of first || is counted as a block

    1. @beton_kruglosu_totchno 4y

      no it's not

    2. Егор 4y

      it stops processing ANDs when finds first false and stops processing ORs when it finds first true

      1. @callofvoid0 4y

        ah thanks

      2. @callofvoid0 4y

        resulted exactly as this

    3. @beton_kruglosu_totchno 4y

      AND and OR have same precedence

  32. @callofvoid0 4y

    let me change position of and,or and see what happens

  33. @DavidGarciaCat 4y

    infinite loop?

    1. @dst212 4y

      break; doesn't agree

      1. @DavidGarciaCat 4y

        Yeah, I ran a quick investigation later and I know now my both guessings were wrong

  34. @DavidGarciaCat 4y

    or maybe syntax error?

  35. dev_meme 4y

    The real answer to this is refactoring 😂👀

  36. @realbond007 4y

    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 🤓

  37. @Iliya_malecki 3y

    1 2 7 4 5 piss easy

Use J and K for navigation