Goes To Just a brief tutorial to tell people about the "goes to" operator: -->. The arrow design is to show it goes towards the number, thus it decrements a variable until it reaches zero, and can be used in loops to..."> Goes To Just a brief tutorial to tell people about the "goes to" operator: -->. The arrow design is to show it goes towards the number, thus it decrements a variable until it reaches zero, and can be used in loops to...">

[Tutorial] "Goes To" operator: -->

Started by SA:MP, May 05, 2023, 05:48 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

SA:MP

[Tutorial] "Goes To" operator: -->

Goes To

Just a brief tutorial to tell people about the "goes to" operator: -->.  The arrow design is to show it goes towards the number, thus it decrements a variable until it reaches zero, and can be used in loops to loop a number of times.  Mainly the while loop:




Code:

new i = 10;
while (i --> 0)
{
    printf("%d", i);
}

Will print:



9

8

7

6

5

4

3

2

1

0





Note that 10 is not included, while 0 is.  This makes it like normal loops, but backwards.



You can loop over players backwards with:




Code:

for (new i = MAX_PLAYERS; i --> 0; )
{
}

Technically you can go down to any value:




Code:

new j = 10;
while (j --> 5) {}

And yes, if you haven't figured it out yet, this isn't actually a new operator.  It is two operators (-- and >) with odd spacing.  That last example would be more normally written as:




Code:

new j = 10;
while (j-- > 5) {}

I first saw this here: https://stackoverflow.com/questions/...-operator-in-c so though I'd write it here.  It is stupid, don't use it...

Source: [Tutorial] "Goes To" operator: -->