[Tutorial] Using the ternary operator :?

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

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

FrankJScott

In response to the lady talking about antihistamine ebastine, tideglusib price, ap1903, pharmacophore structure, aspartic acid meaning, bafilomycin a1 concentration, e1 enzyme, s1p inhibitor, protease inhibitor sigma, mtor and rapamycin,  I highly suggest this top rated amino acid url or pmsf use, nlrp3 inhibitor, dhea side effects women, mapk inflammation, fragment based, ferroptosis cell death, mtor akt, mass molar calculator, alk5 inhibitor ii, ped5 inhibitors, not to mention this do you agree for amino acid advice not forgetting sites such as histone modification gene expression, molarity conversion, sglt2 transporter mechanism, azelaic acid solubility in ethanol, epigenetic cancer drugs, anti pd1 and anti pdl1, fragment compound, cl264, tyrosine kinase pathway, xl413, which is worth considering with this more helpful hints on amino acid url which is also great. Also, have a look at this description for amino acid site alongside all natural kinase inhibitors, vasoactive intestinal, navoximod, mass molar calculator, mapk signaling pathway, parp 1 inhibitors, amp kinase activators, dhea 1, gamma secretase notch, necessary amino acids for body, not forgetting sites such as this see on amino acid tips and don't forget oligomycin inhibits atp synthase, potassium acetate solution, 78 dihydroxyflavone, amino acid in, mice to human dose conversion,  his response on and don't forget delta lactone structure, sybr green price, fibroblast growth factor receptor 1, ppar alpha and gamma, heneicosanoic acid,  for good measure. Check more @ Best Natural Pet Supplements Guide 571905b

FrankJScott

In response to the lady inquiring about best treatment for aging face, botox face fillers, skin care by, treatment microneedling, priapus shot, medical skin care treatments, wrinkle skin treatment, med spa rejuvenate, aging facial treatments, botox treatment products,  I highly suggest this his explanation about erectile dysfunction treatment sarasota details or radiesse forehead, botox slimming, botox cosmetic procedure, microneedling for skin rejuvenation, botox cosmetic treatment, lip filler wrinkles, laser skin resurfacing procedure, juvederm fillers, cosmetic skin treatments, name for botox, as well as this go here on erectile dysfunction treatment sarasota forum alongside all prp microneedling treatment, botox what is it used for, botox on skin, dermaplane skin treatment, dermal fillers, skin care laser treatment, skin wrinkle, cosmetic dermal fillers, best facial therapy, botox before and after lip lines, together with this updated male enhancement sarasota tips which is also great. Also, have a look at this top non surgical male enhancement sarasota details not to mention the body treatment, facial botox, most effective wrinkle treatment, facial for rejuvenation, platelet rich plasma with microneedling, skin dermal fillers, rejuvenate injections, skin care led light therapy, platelet rich plasma therapy face rejuvenation, shockwave therapy for skin tightening, not forgetting sites such as this excellent male regenerative aesthetics sarasota site not forgetting sites such as skin injections for wrinkles, dermal fillers for forehead wrinkles, filler for face lines, botox and frown lines, led right light therapy,  more about the author about alongside all red light therapy and wrinkles, skin tightening botox, med spa sarasota fl, vampire facial injections, best laser treatment,  for good measure. Check more @ Top Rated Penile Treatment Sarasota Guide 68456a0

SA:MP

[Tutorial] Using the ternary operator :?

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.

Source: [Tutorial] Using the ternary operator :?