SA-MP Forums Archive
Ternary operator problem - 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)
+--- Thread: Ternary operator problem (/showthread.php?tid=426313)



Ternary operator problem - Speed++ - 28.03.2013

syntax

Code:
condition ? true : false
Normal if - else

pawn Code:
if(Day < 21)
{
    SetWeather(WinterWeather[winterrand]);
}
else
{
    SetWeather(SpringWeather[springrand]);
}
with ternary operator

pawn Code:
(Day < 21) ? SetWeather(WinterWeather[winterrand]) : SetWeather(SpringWeather[springrand]);
i get this warning

Code:
warning 215: expression has no effect
what i wrong ?


Re: Ternary operator problem - Jeffry - 28.03.2013

pawn Code:
if((Day < 21) ? SetWeather(WinterWeather[winterrand]) : SetWeather(SpringWeather[springrand])){}



Re: Ternary operator problem - Speed++ - 28.03.2013

Quote:
Originally Posted by Jeffry
View Post
pawn Code:
if((Day < 21) ? SetWeather(WinterWeather[winterrand]) : SetWeather(SpringWeather[springrand])){}
i get a warning with empty statement


Re: Ternary operator problem - Finn - 28.03.2013

pawn Code:
SetWeather((Day < 21) ? (WinterWeather[winterrand]) : (SpringWeather[springrand]));



Re: Ternary operator problem - Jeffry - 28.03.2013

Quote:
Originally Posted by Speed++
View Post
i get a warning with empty statement
I compiled it, it worked without warnings.


Re: Ternary operator problem - Pottus - 28.03.2013

Why bother using it? It will still compile the same as using if/else and is harder to read.