What is the "-->" operator in C/C++?

ID : 119867

viewed : 10

Tags : coperatorscode-formattingstandards-compliancec

Top 5 Answer for What is the "-->" operator in C/C++?

vote vote

100

--> is not an operator. It is in fact two separate operators, -- and >.

The conditional's code decrements x, while returning x's original (not decremented) value, and then compares the original value with 0 using the > operator.

To better understand, the statement could be written as follows:

while( (x--) > 0 ) 
vote vote

80

Or for something completely different... x slides to 0.

while (x --\             \              \               \                > 0)      printf("%d ", x); 

Not so mathematical, but... every picture paints a thousand words...

vote vote

76

That's a very complicated operator, so even ISO/IEC JTC1 (Joint Technical Committee 1) placed its description in two different parts of the C++ Standard.

Joking aside, they are two different operators: -- and > described respectively in §5.2.6/2 and §5.9 of the C++03 Standard.

vote vote

65

x can go to zero even faster in the opposite direction:

int x = 10;  while( 0 <---- x ) {    printf("%d ", x); } 

8 6 4 2

You can control speed with an arrow!

int x = 100;  while( 0 <-------------------- x ) {    printf("%d ", x); } 

90 80 70 60 50 40 30 20 10

;)

vote vote

53

It's equivalent to

while (x-- > 0) 

x-- (post decrement) is equivalent to x = x-1 so, the code transforms to:

while(x > 0) {     x = x-1;     // logic } x--;   // The post decrement done when x <= 0 

Top 3 video Explaining What is the "-->" operator in C/C++?

Related QUESTION?