[Tutorial] A more close look at Ternary Operators
#1

Ternary Operators

--How Many Types of operator are there?

Basically there are 3 types of operator, they are -->
-- Uniary Operators
Example-- a++ , --b ... etc.
All of us knows about this.

-- Binary Operators
Example -- << , >> etc.
You can learn about Binary operators here- https://sampforum.blast.hk/showthread.php?tid=177523

-- Ternary Operators
Example -- a = b<c ? 5 : 6; etc.
You will learn about them more in this tut.

Ternary operator and its Basic usage


Basically ternary operator are used in place of "if" and "else" statements. They consists of a "?" and a ":" sign.

It's Syntax

Basic syntax -- Condition ? Value1 : Value2

It returns "Value1" if Condition is true and returns "Value2" if Condition is false.

Using it

A simple code in "if" and "else"--
pawn Code:
new a;
if(b < c)
{
a=10;
}
else
{
a=5;
}
The above code if can be written using ternary operators like this --
pawn Code:
new a = b<c ? 10 : 5;
You see how short it become using ternary operators, well that's the use of operators to make our code small

Advanced Usage


Advanced usage of ternary operators is used in place of nested "if" and "else" statements
A piece of advanced piece of ternary operator looks something like this--
pawn Code:
new a = b<c ? m<n ? 7 : 10 : 60;
Looks weird enough?
Don't worry we can make i will make it simpler for you
Look at the code now--
pawn Code:
new a = b<c ? ( m<n ? 7 : 10 ) : 60;
Now it looks a little simpler, isn't it?

Well you can use it without round brackets() but it will make you confuse so recommended usage is using with round brackets()

So a comparison of above example with an nested "if" and "else" statement, so here is the above example in "if" and "else" statements--
pawn Code:
new a;
if(b<c)
    {
       if(m<n)
          {
             a = 7;
          }
       else
          {
             a = 10;
          }
     }
else
     {
        a = 60;
     }
So look at the same code with ternary operator--
pawn Code:
new a = b<c ? m<n ? 7 : 10 : 60;
//OR
new a = b<c ? { m<n ? 7 : 10 } : 60;
Using it in Fucntions inline

A Big Thanks to Vince for showing this feature of Ternary operators
Examples-
pawn Code:
if(PlayerStats[playerid][pDrugSeeds]==true)return SendClientMessage(playerid, COLOR_RED,"You aren't growing any drug plants. Use /plant to grow one.");
else return SendClientMessage(playerid, COLOR_RED,"You aren't growing any drug plants. Get some seeds first!");
this code can be written as this also--
pawn Code:
return SendClientMessage(playerid, COLOR_RED, (PlayerStats[playerid][pDrugSeeds])
    ? ("You aren't growing any drug plants. Use /plant to grow one.")
    : ("You aren't growing any drug plants. Get some seeds first!")
);
Hope so you all liked it, I tried my best to explain as much as I can.

PS. My first tut.
Reply


Messages In This Thread
A more close look at Ternary Operators - by BroZeus - 02.07.2014, 10:11
Re: A more close look at Ternary Operators - by Rittik - 02.07.2014, 10:48
Re: A more close look at Ternary Operators - by BroZeus - 02.07.2014, 10:52
Re: A more close look at Ternary Operators - by Rittik - 02.07.2014, 11:02
Re: A more close look at Ternary Operators - by BroZeus - 02.07.2014, 11:05
Re: A more close look at Ternary Operators - by Rittik - 02.07.2014, 11:08
Re: A more close look at Ternary Operators - by BroZeus - 02.07.2014, 11:12
Re: A more close look at Ternary Operators - by Vince - 02.07.2014, 11:30
Re: A more close look at Ternary Operators - by BroZeus - 02.07.2014, 13:22
Re: A more close look at Ternary Operators - by Pottus - 02.07.2014, 13:58

Forum Jump:


Users browsing this thread: 1 Guest(s)