Author Topic: The "goes to" operator  (Read 2607 times)

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4954
The "goes to" operator
« on: September 15, 2015, 11:03:08 PM »
I came across something rather funny today. The "goes to" operator: -->

Code: [Select]
#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? ;)

Offline Leviathan

  • Hero Member
  • *****
  • Posts: 4055
Re: The "goes to" operator
« Reply #1 on: September 16, 2015, 02:32:21 PM »
Where is it from ?

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2350
  • OPHD Lead Developer
    • LairWorks Entertainment
Re: The "goes to" operator
« Reply #2 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: [Select]
while (x-- > 0)

Offline Arklon

  • Administrator
  • Hero Member
  • *****
  • Posts: 1267
Re: The "goes to" operator
« Reply #3 on: September 16, 2015, 10:28:36 PM »
That's cute but no. Anybody who's worked with C++ for any length of time will know to read this as:

Code: [Select]
while (x-- > 0)
No, silly, it totally works. Here:

Code: [Select]
#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.
« Last Edit: September 16, 2015, 10:30:34 PM by Arklon »

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4954
Re: The "goes to" operator
« Reply #4 on: September 17, 2015, 09:08:54 AM »
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.
« Last Edit: September 17, 2015, 09:11:17 AM by Hooman »

Offline Eddy-B

  • Hero Member
  • *****
  • Posts: 1186
    • http://www.eddy-b.com
Re: The "goes to" operator
« Reply #5 on: September 18, 2015, 01:31:29 PM »
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

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4954
Re: The "goes to" operator
« Reply #6 on: September 19, 2015, 05:44:30 AM »
Really? Java has a "goes to" operator as well? Awesome!

 ;)

Nice to see you Eddy-B.  :)

Offline Eddy-B

  • Hero Member
  • *****
  • Posts: 1186
    • http://www.eddy-b.com
Re: The "goes to" operator
« Reply #7 on: September 19, 2015, 12:29:50 PM »
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