If
Contents |
If...Else If...Else
Description
Conditionally run statements.
If(<expression>) { statements ... } Else If(expression-n) { else if statements ... } Else { else statements ... }
Parameters
expression
If the expression is true, the first statement block is executed. If not, the first true Else If block is executed. Otherwise, the "Else" block is executed.
Both the Else If and Else blocks are optional.
Remarks
If statements may be nested.
The expression can contain the boolean operators of AND, &&, OR, ||, NOT, ! as well as the logical operators <, <=, >, >=, ==, ==, and <> as needed grouped with parentheses as needed.
Example
If($a == 100) { println( "Value is 100" ); } Else If($a == 200) { println( "Value is 200" ); } Else { println( "No Match" ); }
Example of using the logical ! operator:
If(!($a == 100)) { println( "Value is 100" ); } Else If($a == 200) { println( "Value is 200" ); } Else { println( "No Match" ); }
It is also possible to not bother with the {} example:
If(!($a == 100)) println( "Value is 100" ); Else If($a == 200) println( "Value is 200" ); Else println( "No Match" ); // Or even If(!($a == 100)) println( "Value is 100" ); Else println( "Value is NOT 100" ); // Note this will only work for the FIRST statement and not the other so the following is invalid: If(!($a == 100)) println( "Value is 100" ); println( "Value is 100" ); // INVALID this will not be considered part of the IF Else println( "Value is NOT 100" ); println( "Value is NOT 100" ); // INVALID this will not be considered part of the else
Example of using the logical ? : operator:
println( $a == 100 ? "Value is 100" : "The value is NOT 100" );
The ? : operator is much faster than a full IF statement so if you want more speed in your programs this is a rather good choice for simple Ifs.
Can be used to set variables:
$var = $a == 100 ? "Value is 100" : "The value is NOT 100";