Confused with symbols. ("?", ":")
#1

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
Reply
#2

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.
}
Reply
#3

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.
Reply
#4

Edited the post above.
Reply
#5

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
Reply
#6

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.
Reply
#7

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

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)