0

On a few occasions I have seen an arrow like symbol combination being used in a for loop like such:

for(int i = 100; i --> 0;) {
    System.out.println(i);
}

What's happening here?

MP1
  • 35
  • 1
  • 6

2 Answers2

7

This is not an arrow, but a decrement operator followed by a greater than sign.

What the compiler sees is a less clear version of (i--) > 0

Joe C
  • 15,324
  • 8
  • 38
  • 50
  • Ahh. Gotcha. It was confusing at first because I've seen more than one person do it in their code. Thanks for the clarification! – MP1 Jan 01 '19 at 00:26
  • 4
    *"I've seen more than one person do it in their code."* - Tell them to stop it! – Stephen C Jan 01 '19 at 00:39
  • 1
    @MP1 If you found this helpful, don't forget to upvote and accept this answer. (And I agree with Stephen, as you have shown that is unclear.) – Joe C Jan 01 '19 at 13:38
1

It's the post-decrement operator along with the greater than symbol combined which is confusing and unreadable.

it's the same as writing:

for(int i = 100; (i--) > 0;) { ... }

i.e. i --> 0 is essentially (i--) > 0.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 2
    Actually, because it's post decrement, it's more like: is `(i > 0)` followed by `i = i - 1;` – dave Jan 01 '19 at 00:28