• 0 Posts
  • 7 Comments
Joined 9 months ago
cake
Cake day: March 12th, 2025

help-circle




  • The compiler should be able to optimize all of them to the same machine code.

    1. This is already good.
    2. Easily optimized by constant folding.
    3. This one depends on the semantics of signed underflow, so it may not do what you want.
    4. The loop can only exit if x==10, so as long as the nextInt() method doesn’t have side effects, the loop should be eliminated. But, again, language semantics can affect this.

    Edit: Very wrong for 3 & 4, see replies.