SA-MP Forums Archive
Confused with symbols. ("?", ":") - 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: Confused with symbols. ("?", ":") (/showthread.php?tid=516668)



Confused with symbols. ("?", ":") - Faisal_khan - 01.06.2014

Hi SA-MP Community,

Back after a brief break. Can someone explain me this line:
pawn Code:
new cost = (PlayerInfo[playerid][pDonateRank] >= 1) ? (floatround(Businesses[businessid][bItemPrices][item] * 0.8)) : (Businesses[businessid][bItemPrices][item]);
Regards


Re: Confused with symbols. ("?", ":") - Rittik - 01.06.2014

Just convert it to if/else format it would be more easier for you.

These are called ternary operators I guess.

Code:
if(PlayerInfo[playerid][pDonateRank] >= 1) 
{
  floatround(Businesses[businessid][bItemPrices][item] * 0.8 
  new const=Businesses[businessid][bItemPrices][item]; //I am not sure about this code.As far as my information it should be coded like this.
}



Re: Confused with symbols. ("?", ":") - Faisal_khan - 01.06.2014

Well the above line has been grabbed from a script which a friend of mine gave to me fix it. So I need to understand those symbols what they specifically mean. Then I can see what can I do with it.


Re: Confused with symbols. ("?", ":") - Rittik - 01.06.2014

Edited the post above.


Re: Confused with symbols. ("?", ":") - BroZeus - 01.06.2014

yes they are called ternary operator i will tell how they work so here is it

Syntax Condition ? Value1 : Value 2

How it works?-- If the condition is true then it returns Value1 and if condition is false then it returns Value2

Examples and its outputs-
taking an simple example --
pawn Code:
//Example
new a,b,c;
b=10;
c=20;
a= (b<c) ? 1 : 0;
So in the above example the value of a will finally be 1 because the condition is true if condition had been (b>c) then value of a will be 0

There is more complex way of using it by using one ternary operator inside another those are used in place of nested loop and are very difficult to understand

I recommend you to use this only when you have a simple if else statement condition. Only if you are expertise in it then use it in place of nested if-else


Re: Confused with symbols. ("?", ":") - Faisal_khan - 01.06.2014

Quote:
Originally Posted by ******
View Post
http://www.y-less.com/uploads/PAWN/pawn-lang.pdf

The ONLY stop for any questions related to the PAWN language itself. I think people have started to forget about this file, despite the fact that it used to be the answer to almost every other topic made.
I did not knew that it is called as ternary operator. This explains it well going through it.

Quote:
Originally Posted by Rittik
View Post
Edited the post above.
Hey an advice. You should not edit your posts like this. Make a new post. And yeah thanks mate.


Re: Confused with symbols. ("?", ":") - Faisal_khan - 01.06.2014

BroZeus, cheers! Thanks got it! So it's an alternative to if.


Re: Confused with symbols. ("?", ":") - Threshold - 01.06.2014

It's an alternative to 'if' and 'else'.

pawn Code:
if(cond == true)
{
    if(cond2 == true) var = 15;
    else if(cond2 == false) var = 25;
}
else if(cond == false) var = 60;
Could be written as:
pawn Code:
new var = (cond == true) ? ((cond2 == true) ? (15) : (25)) : (60);
Personally, since I discovered ternary operators, I have loved them because they reduce everything into 1 simple line and their concept is easy to grasp. They are very useful. Slice's thread gives a very good example of their usage.