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

Above code works, and has the exactly same result of

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

What does the syntax "-->" originally mean in Java? Since almost reachable search engines disallow special characters, I cannot seem to find the answer.

oarfish
  • 4,116
  • 4
  • 37
  • 66
TigerHix
  • 532
  • 1
  • 5
  • 15

6 Answers6

47

--> is not a new operator.

It is just a conjunction of the operators -- and >.

You first compare, and then decrement the variable.

That is,

i --> 0

becomes effectively

i > 0; //Compare
i--; //and decrement
Community
  • 1
  • 1
shauryachats
  • 9,975
  • 4
  • 35
  • 48
10

i --> 0 means i>0 and i-- :: i is decremented first (returns non-decremented value) and then compared to 0.

This is a conjunction.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Eran
  • 387,369
  • 54
  • 702
  • 768
5

--> is not any operator. It is just the cocatenation of -- and >.

So when you write

i-->0 it means compare the value of i and then decrement it.

So for better readability it can be written as

for (int i = 99; (i--)> 0;) {
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
3

Notice here the increment/decrement place does not appear. So it decrements i by 1 but returns same non-decremented value and then compares i with 0.

The comparison checks whether i is greater than 0 after the decrement is performed(but not returned).

Mehmood Arbaz
  • 285
  • 1
  • 3
  • 11
2

i-- > 0

i-- is post decrement

> is greater than

for (initialization; boolean expression; updation){
 `//some code`
}

So you did initialization and but you checked boolean expression and updated in one step so it worked.

singhakash
  • 7,891
  • 6
  • 31
  • 65
1

There is no operator as --> it's simply i-- and i>0; first, it will do post decrement. then it will check the condition and compare it with 0 whether it's greater than or not.

Remember it's value will not be changed while comparison (i will be 1) after comparison it will decrement the value (i will now be 0) and printed.

Prashant
  • 2,556
  • 2
  • 20
  • 26