Switch

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "= Switch...Case...Default..EndSwitch = === Description === Conditionally run statements. <pre> Switch Case <expression> statement1 ... [Case <expressio...")
 
Line 1: Line 1:
= Switch...Case...Default..EndSwitch =
+
= Select...Case...Default =
  
 
=== Description ===
 
=== Description ===
Line 6: Line 6:
  
 
<pre>
 
<pre>
Switch
+
Switch ( <expression> )
 +
{
 
     Case <expression>
 
     Case <expression>
 +
    {
 
         statement1
 
         statement1
 
         ...
 
         ...
     [Case <expression>
+
     }
 +
    Case <expression>
 +
    {
 
         statement2
 
         statement2
         ...]
+
         ...
     [Default
+
     }
 +
    Default
 +
    {
 
         statementN
 
         statementN
         ...]
+
         ...
EndSwitch
+
    }
 +
}
 
</pre>
 
</pre>
  
 
=== Parameters ===
 
=== Parameters ===
 +
 +
==== Switch <expression>  ====
 +
 +
Any vaid expression to used and compared with the Case <expressions>
  
 
==== Case <expression>  ====
 
==== Case <expression>  ====
  
If the expression is true the following statements up to the next Case or EndSwitch statement are executed. If more than one of the Case statements are true, only the first one is executed.
+
If the expression matches the Switch <expression> the following statements up to the next Case or Default are executed. If more than one of the Case statements are true, only the first one is executed.
  
 
Note - If a Case is lacking a Break then the code will fall through into the next case and execute that and fall through again if theres no break.
 
Note - If a Case is lacking a Break then the code will fall through into the next case and execute that and fall through again if theres no break.
Line 31: Line 42:
 
Switch statements may be nested.
 
Switch statements may be nested.
  
The expression can contain the boolean operators of AND, &&, OR, ||, NOT, ! as well as the logical operators <, <=, >, >=, ==, ==, and <> as needed.
+
Strings are case sensitive when used in a case.
  
 
=== Example ===
 
=== Example ===
Line 38: Line 49:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$var = "test"
+
$var = 1;
 
+
Switch ( $var )
Switch
+
{
     Case $var == 1
+
     Case 1:
     Case $var == 2
+
     Case 2:
         println("Value is 1 or 2")
+
{
break
+
         println("Value is 1 or 2");
     Case $var == 3
+
}
         println("Value is 3")
+
break;
break
+
     Case 3:
     Case $var == "test"
+
{
         println("Value is \"test\"")
+
         println("Value is 3");
break
+
}
     Default
+
break;
         println("No preceding case was true!")
+
     Case "test":
EndSwitch
+
{
 +
         println("Value is \"test\"");
 +
}
 +
break;
 +
     Default:
 +
{
 +
         println("No preceding case was true!");
 +
}
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 16:39, 18 November 2011

Contents

Select...Case...Default

Description

Conditionally run statements.

Switch ( <expression> )
{
    Case <expression>
    {
        statement1
        ...
    }
    Case <expression>
    {
        statement2
        ...
    }
    Default
    {
        statementN
        ...
    }
}

Parameters

Switch <expression>

Any vaid expression to used and compared with the Case <expressions>

Case <expression>

If the expression matches the Switch <expression> the following statements up to the next Case or Default are executed. If more than one of the Case statements are true, only the first one is executed.

Note - If a Case is lacking a Break then the code will fall through into the next case and execute that and fall through again if theres no break.

Remarks

Switch statements may be nested.

Strings are case sensitive when used in a case.

Example

Heres an example with all breaks in proper place (Break statement is needed to tell the code to stop or else it wil fall through into the next case that may be what you want though)

$var = 1;
Switch ( $var )
{
    Case 1:
    Case 2:
	{
        println("Value is 1 or 2");
	}
	break;
    Case 3:
	{
        println("Value is 3");
	}
	break;
    Case "test":
	{
        println("Value is \"test\"");
	}
	break;
    Default:
	{
        println("No preceding case was true!");
	}
}

In the above example: if $var is 1 it will go into Case 1 then fall through into case 2 run the code and break if $var is 2 it will go into Case 2 run the code and break if $var is 3 it will go into Case 3 run the code and break if $var is "test" it will go into Case "test" run the code and break if $var does not match any case at all the code inside Default will be run

This means you can in theory place loads of empty Case statements that you want to all fall through.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox