Outpost Universe Forums

Projects & Development => Outpost 2 Programming & Development => Topic started by: Hooman on September 15, 2015, 11:03:08 PM

Title: The "goes to" operator
Post by: Hooman 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? ;)
Title: Re: The "goes to" operator
Post by: Leviathan on September 16, 2015, 02:32:21 PM
Where is it from ?
Title: Re: The "goes to" operator
Post by: 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: [Select]
while (x-- > 0)
Title: Re: The "goes to" operator
Post by: Arklon 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.
Title: Re: The "goes to" operator
Post by: Hooman 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? (http://stackoverflow.com/questions/1642028/what-is-the-name-of-the-operator)


Very to the point leeor_net. Glad to see you and Arklon got it.
Title: Re: The "goes to" operator
Post by: Eddy-B 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.
Title: Re: The "goes to" operator
Post by: Hooman on September 19, 2015, 05:44:30 AM
Really? Java has a "goes to" operator as well? Awesome!

 ;)

Nice to see you Eddy-B.  :)
Title: Re: The "goes to" operator
Post by: Eddy-B on September 19, 2015, 12:29:50 PM
You might enjoy looking at FALSE (http://strlen.com/false-language) and BF (https://en.wikipedia.org/wiki/Brainfuck) (i can't write the full word out, since i'd have to ban myself for profanity)

Have fun!