Difference between revisions of "The ? (conditional) Operator"

From Catglobe Wiki
Jump to: navigation, search
(Created page with "= The ? (Conditional) Operator = The conditonal operator (?) returns one of two values depending on the value of the conditional expression. It works the same as If stateme...")
 
Line 1: Line 1:
= The ? (Conditional) Operator =
+
= The ? (Conditional) Operator =
  
 
The conditonal operator (?) returns one of two values depending on the value of the conditional expression. It works the same as If statement.  
 
The conditonal operator (?) returns one of two values depending on the value of the conditional expression. It works the same as If statement.  
  
=== Syntax ===
+
=== Syntax ===
  
'''Conditional expression ? exp1 : exp2 '''
+
'''Conditional expression ? exp1 : exp2 '''  
  
 
Comparing with if statement:<br>  
 
Comparing with if statement:<br>  
  
:'''if(Conditional expression)'''<br>
+
:'''if(Conditional expression)'''<br>  
 
::'''exp1'''<br>  
 
::'''exp1'''<br>  
 
:'''else'''<br>  
 
:'''else'''<br>  
 
::'''exp2'''
 
::'''exp2'''
  
=== Arguments= ===
+
=== Arguments ===
  
 
*Conditional expression: a boolean expression  
 
*Conditional expression: a boolean expression  
*exp1: when conditional expression is true, this expression will be excuted.  
+
*exp1: when the conditional expression is true, this expression will be excuted.  
*exp2: when conditional expression is false, this expression will be excuted.
+
*exp2: when the conditional expression is false, this expression will be excuted.
  
 
=== Examples ===
 
=== Examples ===

Revision as of 05:50, 19 October 2011

The ? (Conditional) Operator

The conditonal operator (?) returns one of two values depending on the value of the conditional expression. It works the same as If statement.

Syntax

Conditional expression ? exp1 : exp2

Comparing with if statement:

if(Conditional expression)
exp1
else
exp2

Arguments

  • Conditional expression: a boolean expression
  • exp1: when the conditional expression is true, this expression will be excuted.
  • exp2: when the conditional expression is false, this expression will be excuted.

Examples

1 == 1 ? print(true) : print(false);   // return true

//Comparing with if statement
if(1 == 1)
    print(true);
else
    print(false);
// return true