A more close look at Ternary Operators -
BroZeus - 02.07.2014
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 --
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.
Re: A more close look at Ternary Operators -
Rittik - 02.07.2014
Ternary Operators: Ternary operators deal with three operands.These operators are known as
conditional operator as because the value assigned to a variable depends upon a logical expression.
Syntax:
variable= (test expression) ? Expression 1 : Expression 2;
Example:
pawn Code:
new a=5,b=3,max=0,min=0;
max=(a>b)? a:b;
// Here, the value 5 is stored in max as a>b is true.
min= (b>a)? a:b;
// Here, the value 3 is stored in min as b>a is false.
@BroZeus: You should write a proper definition so that scripters can understand easily, writing
"Basically ternary operator are used in place of "if" and "else" statements. They consists of a "?" and a ":" sign." will not do the work. As it is your first tutorial I'll suggest you to research before posting.Nice try though.
I suggest all the scripters/coders to use if-else because it can be nested easily and you can profitably make your code execute.
Re: A more close look at Ternary Operators -
BroZeus - 02.07.2014
Quote:
Originally Posted by Rittik
Ternary Operators: Ternary operators deal with three operands. It is also called conditional operators because the value assigned to a variable depends upon a logical expression.
Syntax:
variable= (test expression) ? Expression 1 : Expression 2
Example:
pawn Code:
new a=5,b=3,max=0,min=0;
max=(a>b)? a:b; // Here, the value 5 is stored in max as a>b is true.
min= (b>a)? a:b; // Here, the value 3 is stored in min as b>a is false.
@BroZeus: You should write a proper definition so that scripters can understand easily, writing "Basically ternary operator are used in place of "if" and "else" statements. They consists of a "?" and a ":" sign." will not do the work. As it is your first tutorial I'll suggest you to research before posting.Nice try though. I suggest all the scripters/coders to use if-else because it can be nested easily and you can profitably make your code execute.
|
well thanks for that
but definition doesn't matter much what matter is its usage caz u gonna use it in the script not right it its definition
[its not school]
and as far as i know i have explained its usage correctly
well i will add your definition too
and also why have u put a syntax ? i think i have put it in tut isnt it?
Re: A more close look at Ternary Operators -
Rittik - 02.07.2014
Declaration of a variable is must which your syntax missed out .
Okay, tell me which one do you prefer ? conditional operator or if-else ?
Re: A more close look at Ternary Operators -
BroZeus - 02.07.2014
Quote:
Originally Posted by Rittik
Declaration of a variable is must which your syntax missed out .
Okay, tell me which one do you prefer ? conditional operator or if-else ?
|
1. its just an example of syntax not the full code
2. it doesn't matter what i prefer, this tut is for other people to learn from it, increasing your knowledge is never a harmful thing
Re: A more close look at Ternary Operators -
Rittik - 02.07.2014
I was asking that personally,do not take it in a different track.
Re: A more close look at Ternary Operators -
BroZeus - 02.07.2014
well i prefer "if" and "else" because most of the code i make is for release or for other person who ask for my help and almost none of them know about ternary operator so i have to make it in "if and else"
i am comfortable with both of them
i use ternary ones for java language in my school
Re: A more close look at Ternary Operators -
Vince - 02.07.2014
Worth mentioning that this can also be used inline, e.g.:
pawn Code:
ShowPlayerDialog(showplayerid, DIALOG_NO_RETURN, rows ? DIALOG_STYLE_LIST : DIALOG_STYLE_MSGBOX, temp, dialogstring, "Okay", "");
Or even:
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!")
);
Re: A more close look at Ternary Operators -
BroZeus - 02.07.2014
Quote:
Originally Posted by Vince
Worth mentioning that this can also be used inline, e.g.:
pawn Code:
ShowPlayerDialog(showplayerid, DIALOG_NO_RETURN, rows ? DIALOG_STYLE_LIST : DIALOG_STYLE_MSGBOX, temp, dialogstring, "Okay", "");
Or even:
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!") );
|
thnx man i will add it as soon as i can
Re: A more close look at Ternary Operators -
Pottus - 02.07.2014
I like this use you can quickly toggle 0/1 with limited code yes it seems like a basic concept but a clever way to do it.