[Tutorial] Ternary operation
#1

TERNARY OPERATION
In this tutorial I will tell you about ternary operator.

In computer science, a ternary operator is an operator that takes three arguments. The arguments and result can be of different types. Many programming languages that use C-like syntax feature a ternary operator, ?:, which defines a conditional expression. Since this operator is often the only existing ternary operator in the language, it is sometimes simply referred to as "the ternary operator". In some languages, this operator is referred to as "the conditional operator" [...]

These operations are written as:
Code:
<condition> ? <first expression> : <second expression>
If the condition is true, returns value of the first expression, differently - the second returns.

For example:
PHP Code:
new 1;
new 
= (0) ? 3
It is possible to make a similar code with use of a condition of if:
PHP Code:
new j,
    
1;
if (
0)
{
    
2;
}
else
{
    
3;

Also you can use ternary operations with a strings:
PHP Code:
new 1;
print((
!= 0) ? ("Cat") : ("Dog")); 
Fixed: in modified copy of the Pawn compiler by Zeex [...] fixed bug. If you are using the modified compiler' Zeex, you can write as
PHP Code:
print((random(3) > "QWE" "ASD")); 
.. and if not
PHP Code:
print((random(3) > ? ("QWE") : ("ASD"))); 
WHERE TO USE?
Ternary operations should be used if the condition has two operands. Is if/else.
PHP Code:
SendClientMessage(playerid, -1, (random(20) == 7) ? (!"You are just a lucky!") : (!"You are a loser :c")); 
I will give an example of a simple command for establishment of level of health to the player.
PHP Code:
CMD:health(playeridparams[])
{
    new 
targetidFloat:health;
    if (
sscanf(params, !"uf"targetidFloat:health))
        return 
SendClientMessage(playerid, -1, !"Command: /health [player] [health]");
    new 
Float:current_value;
    
GetPlayerHealth(targetidcurrent_value);
    if (
SetPlayerHealth(targetid, ((health current_value) ? health current_value 10.0)) == 0)
        return 
SendClientMessage(playerid, -1, !"Player isn't connected.");
    return 
SendClientMessage(playerid, -1, !"You're changed health level to the player.");

... or to send a message to the chat.
PHP Code:
CMD:chat(playeridparams[])
{
    return ((
isnull(params)) ?
        
SendClientMessage(playerid, -1, !"Command: /chat [text]") :
            
SendClientMessageToAll(-1params)
    );

You can use ternary operation in ternary operation!
PHP Code:
printf("%d", (random(100) > (random(5) == 200 0) ? 2)); 
Thanks for reading my small tutorial.
English is not my native language
therefore can be are errors
Reply


Messages In This Thread
Ternary operation - by Untonyst - 13.07.2016, 22:14
Re: Ternary operation - by OstGot - 13.07.2016, 22:30
Re: Ternary operation - by PrO.GameR - 14.07.2016, 19:47
Re: Ternary operation - by Pottus - 14.07.2016, 20:00
Re: Ternary operation - by Vince - 14.07.2016, 20:42
Re: Ternary operation - by Untonyst - 14.07.2016, 23:00
Re: Ternary operation - by PrO.GameR - 14.07.2016, 23:25
Re: Ternary operation - by Dayrion - 15.07.2016, 01:33
Re: Ternary operation - by Diman777 - 11.08.2016, 20:53
Re: Ternary operation - by Untonyst - 11.08.2016, 21:21

Forum Jump:


Users browsing this thread: 2 Guest(s)