If

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "= If...ElseIf...Else...EndIf = === Description === Conditionally run statements. <pre> If <expression> Then statements ... [ElseIf expression-n Then [elseif state...")
 
m (1 revision)
 
(8 intermediate revisions by one user not shown)
Line 1: Line 1:
= If...ElseIf...Else...EndIf =
+
= If...Else If...Else  =
  
 
=== Description ===
 
=== Description ===
Line 6: Line 6:
  
 
<pre>
 
<pre>
If <expression> Then
+
If(<expression>)
 +
{
 
     statements
 
     statements
 
     ...
 
     ...
[ElseIf expression-n Then
+
}
     [elseif statements ... ]]
+
Else If(expression-n)
 +
{
 +
     else if statements
 
     ...
 
     ...
[Else
+
}
     [else statements]
+
Else
 +
{
 +
     else statements
 
     ...
 
     ...
EndIf
+
}
 
</pre>
 
</pre>
  
Line 22: Line 27:
 
==== expression  ====
 
==== expression  ====
  
If the expression is true, the first statement block is executed. If not, the first true ElseIf block is executed. Otherwise, the "Else" block is executed.
+
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 ===
 
=== Remarks ===
Line 28: Line 35:
 
If statements may be nested.
 
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.
+
The expression can contain the boolean operators of &&, ||, ! as well as the logical operators <, <=, >, >=, ==, !=, <>, eq, eqi, neq and neqi as needed grouped with parentheses as needed.
  
 
=== Example ===
 
=== Example ===
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
If $a == 100 Then
+
If($a == 100)
println( "Value is 100" )
+
{
ElseIf $a == 200 Then
+
println( "Value is 100" );
println( "Value is 200" )
+
}
 +
Else If($a == 200)
 +
{
 +
println( "Value is 200" );
 +
}
 
Else
 
Else
println( "No Match" )
+
{
EndIf
+
println( "No Match" );
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Example of using the logical NOT or ! operator:
+
Example of using the logical ! operator:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
If NOT $a == 100 Then
+
If(!($a == 100))
println( "Value is 100" )
+
{
ElseIf $a == 200 Then
+
println( "Value is 100" );
println( "Value is 200" )
+
}
 +
Else If($a == 200)
 +
{
 +
println( "Value is 200" );
 +
}
 
Else
 
Else
println( "No Match" )
+
{
EndIf
+
println( "No Match" );
 +
}
 +
</syntaxhighlight>
 +
 
 +
It is also possible to not bother with the {} example:
 +
 
 +
<syntaxhighlight lang="sputnik">
 +
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
 +
</syntaxhighlight>
 +
 
 +
Example of using the logical ? : operator:
 +
 
 +
<syntaxhighlight lang="sputnik">
 +
println( $a == 100 ? "Value is 100" : "The value is NOT 100" );
 +
</syntaxhighlight>
 +
 
 +
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:
 +
 
 +
<syntaxhighlight lang="sputnik">
 +
$var = $a == 100 ? "Value is 100" : "The value is NOT 100";
 +
</syntaxhighlight>
 +
 
 +
An IF can also be used by itself example
 +
 
 +
<syntaxhighlight lang="sputnik">
 +
$a = 10;
 +
if($a == 10)
 +
println("It is 10");
 +
 
 +
// Or even
 +
$a = 10;
 +
if($a == 10)
 +
println("It is 10");
 +
else
 +
println("It is NOT 10");
 +
 
 +
// Or Even
 +
$a = 10;
 +
if($a == 10)
 +
println("It is 10");
 +
else if($a == 11)
 +
println("It is 11");
 +
else
 +
println("ELSE");
 +
</syntaxhighlight>
 +
 
 +
On one line
 +
 
 +
<syntaxhighlight lang="sputnik">
 +
$a = 10;
 +
if($a == 10) println("It is 10");
 +
 
 +
// Or even
 +
$a = 10;
 +
if($a == 10) println("It is 10"); else println("It is NOT 10");
 +
 
 +
// Or even
 +
$a = 10;
 +
if($a == 10) println("It is 10"); else if($a == 11) println("It is 11"); else println("ELSE");
 +
</syntaxhighlight>
 +
 
 +
This can also be done in reverse just for the IF itself example
 +
 
 +
<syntaxhighlight lang="sputnik">
 +
println("It is 10") if($a == 10);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:38, 14 June 2015

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 &&, ||, ! as well as the logical operators <, <=, >, >=, ==, !=, <>, eq, eqi, neq and neqi 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";

An IF can also be used by itself example

$a = 10;
if($a == 10)
	println("It is 10");
 
// Or even
$a = 10;
if($a == 10)
	println("It is 10");
else
	println("It is NOT 10");
 
// Or Even
$a = 10;
if($a == 10)
	println("It is 10");
else if($a == 11)
	println("It is 11");
else
	println("ELSE");

On one line

$a = 10;
if($a == 10) println("It is 10");
 
// Or even
$a = 10;
if($a == 10) println("It is 10"); else println("It is NOT 10");
 
// Or even
$a = 10;
if($a == 10) println("It is 10"); else if($a == 11) println("It is 11"); else println("ELSE");

This can also be done in reverse just for the IF itself example

println("It is 10") if($a == 10);
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox