The "goes to" operator

Started by Hooman, September 15, 2015, 11:03:08 PM

Hooman

I came across something rather funny today. The "goes to" operator: -->

#include <stdio.h>

int main()
{
    int x = 10;
    while (x --> 0) // x goes to 0
    {
        printf("%d ", x);
    }
}


The output is:
9 8 7 6 5 4 3 2 1 0

Anyone care to explain? ;)

Leviathan


leeor_net

That's cute but no. Anybody who's worked with C++ for any length of time will know to read this as:

Code: cpp
while (x-- > 0)


Arklon

#3
Quote from: leeor_net on September 16, 2015, 08:55:48 PM
That's cute but no. Anybody who's worked with C++ for any length of time will know to read this as:

Code: cpp
while (x-- > 0)

No, silly, it totally works. Here:

#include <stdio.h>

int main()
{
    int x = 0;
    while (x --> 10) // x goes to 10, maybe, except probably not
    {
        printf("%d ", x);
    }
}


The output is:

You could use ++< and make it work, but that just looks funny.

Hooman

#4
Yes, a corresponding "upto" operator would have been nice, but I'm afraid the comparison operator needs to be reversed, making it look funny and a bit too obvious.

I came across this on StackOverflow a few days ago.
What is the name of the "-->" operator?


Very to the point leeor_net. Glad to see you and Arklon got it.

Eddy-B

You're kidding, right?


I've used these for as long i can remember. one of the first things one learns when studying the C language.
while (x-->0)  and while (x++<10)

Their names:
" --> " and " ++< " don't not HAVE names since they're NOT single operators, it's simply the decrement/increment operator, followed by a comparison operator. So it is 2 operators, not one.


FIY: it's not just C++ but used in many languages, for example java.
Rule #1:  Eddy is always right
Rule #2: If you think he's wrong, see rule #1
--------------------

Outpost : Renegades - Eddy-B.com - Electronics Pit[/siz

Hooman

Really? Java has a "goes to" operator as well? Awesome!

;)

Nice to see you Eddy-B.  :)

Eddy-B

You might enjoy looking at FALSE and BF (i can't write the full word out, since i'd have to ban myself for profanity)

Have fun!
Rule #1:  Eddy is always right
Rule #2: If you think he's wrong, see rule #1
--------------------

Outpost : Renegades - Eddy-B.com - Electronics Pit[/siz