SA-MP Forums Archive
[Tutorial] Using the ternary operator :? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Using the ternary operator :? (/showthread.php?tid=663153)



Using the ternary operator :? - faxxe - 23.01.2019

Ternary operator

Ahoy everyone.
This time i'll show you guys how to use the ternay operator given by many languages like C or PAWN.
This time we will only talk about the condititonal operator to replace if-statements. There are also ways to perform other actions using nested ternary operator statements.

It is commonly referred as the conditional operator. An expression x ? y : a evaluates to y if the value of x is true, and otherwise to a.

Brief example, comparing 2 numbers using if-statement.
Code:
new result;
new a=1;
new b=0;

if (a > b) 
{
    result = x;
}
else 
{
    result = y;
}
Using our beloved ternary operator makes it much simplier and shorter, and that's it..

Code:
result = a > b ? x : y;
result = a < b ? printf("a is less") : printf("a is greater");
... but not in any case.

Now some examples from my gamemodes, in this case my spectate system to spectate a random player.
Using if-statement.
Code:
    if(!tmp) return INVALID_PLAYER_ID;
    if(idx>tmp) idx=0;
    if(idx<0) idx=tmp-1;
    return randoms[idx];
Using ternary operator it looks like that.
Unfortunately not any statements are appropriate to be written using ternary operators like this one.
It might look quite weird.

Code:
return (!tmp) ? INVALID_PLAYER_ID : randoms[(idx>tmp)?0:((idx<0)?(tmp-1):idx)];
In conclusion that's a brief tutorial how you can use ternary operators.

Unfortunately we tend to get crappy using ternary operators excessively.
It will be complicated to maintain code using ternaries especially for beginners.
You have to consider when to use it and not.
It might be useful to increase the readability of your code, but in other cases it might do the contrary.

So just use it when really necessary.

For now that's it, but during the next days i'll introduce some more advanced examples using the ternary operator in PAWN.


Re: Using the ternary operator :? - serqq - 23.01.2019

I was looking for some tutorial about this and had no idea how was it called, thanks!


Re: Using the ternary operator :? - Pottus - 24.01.2019

Taken from Texture Studio.

Code:
format(optline, sizeof(optline), "{FFFF00}Text: %s\n{FFFF00}Object Note: %s\n{FFFF00}Model Info: %s\n{FFFF00}Group ID: %s\n{FFFF00}Grouped Text: %s\n{FFFF00}Always Show New Objects: %s\n",
                (TextOption[tShowText] ? ("{00AA00}Enabled") : ("{FF3000}Disabled")),
                (TextOption[tShowNote] ? ("{00AA00}Enabled") : ("{FF3000}Disabled")),
                (TextOption[tShowModel] ? ("{00AA00}Enabled") : ("{FF3000}Disabled")),
                (TextOption[tShowGroup] ? ("{00AA00}Enabled") : ("{FF3000}Disabled")),
                (TextOption[tShowGrouped] ? ("{00AA00}Enabled") : ("{FF3000}Disabled")),
                (TextOption[tAlwaysShowNew] ? ("{00AA00}Enabled") : ("{FF3000}Disabled"))
I found it useful in function parameters maybe Y_Less has a better way to do this? At any rate this is important and your tutorial should cover it.