Select

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Description)
m (1 revision)
 
(16 intermediate revisions by one user not shown)
Line 6: Line 6:
  
 
<pre>
 
<pre>
Select <expression>
+
Select
     Case <expression>
+
{
 +
     Case <expressions>
 +
    {
 
         statement1
 
         statement1
 
         ...
 
         ...
     [Case <expression>
+
     }
 +
    Case <expressions>
 +
    {
 
         statement2
 
         statement2
         ...]
+
         ...
     [Default
+
     }
 +
    CaseID <Identifier> <expressions>
 +
    {
 +
        statement2
 +
        ...
 +
    }
 +
    Default
 +
    {
 
         statementN
 
         statementN
         ...]
+
         ...
EndSelect
+
    }
 +
}
 
</pre>
 
</pre>
  
Line 23: Line 35:
 
==== Case <expression>  ====
 
==== Case <expression>  ====
  
If the expression is true the following statements up to the next Case or EndSelect statement are executed. If more than one of the Case statements are true, only the first one is executed.
+
Optional; You can use none, one or more.
 +
 
 +
If the case is true it will be executed.
 +
 
 +
If you type CaseID instead of Case you can assign an ID to it like CaseID Testy: then you can jump to that case using goto _caseTesty;
 +
 
 +
(You can separate expressions to check using a , )
  
 
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.
 +
 +
==== Default  ====
 +
 +
Optional; You can use one or none.
 +
 +
If none of the cases are true the code in Default will be executed.
 +
 +
You dont need a Break in the Default.
 +
 +
You can place the default anywhere in the statement.
  
 
=== Remarks ===
 
=== Remarks ===
Line 31: Line 59:
 
Select statements may be nested.
 
Select 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.
 +
 
 +
A select acts very much like an If statement however it lets you fallthrough if thats useful to you.
 +
 
 +
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.
 +
 
 +
You can place a goto _default; inside a case to instantly jump to the default: statement if one exists (This uses a hardcoded jump and does not reply on Sputniks goto at all so it should be be a lot faster).
 +
 
 +
You can place a goto _case0; inside a case to instantly jump to the case ID you specify you change 0 to the id of the case you wish to jump to. (This uses a hardcoded jump and does not reply on Sputniks goto at all so it should be be a lot faster).
 +
 
 +
You can place a goto _caseFox; inside a case to instantly jump to the case with the name you specify you change Fox to the Name of the case you wish to jump to. (This uses a hardcoded jump and does not reply on Sputniks goto at all so it should be be a lot faster).
 +
 
 +
Note - When you use goto _case stuff it will instantly begin executing code in that case it will NOT check if the case is a match for the Select (A warning will be given if the case/default you want to jump to does not exist).
  
 
=== Example ===
 
=== Example ===
Line 38: Line 78:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$var = 1
+
$var = 1;
 
+
Select
Select $var
+
{
     Case 1
+
     Case $var == 1:
     Case 2
+
     Case $var == 2:
        println("Value is 1 or 2")
+
{
break
+
            println("Value is 1 or 2");
     Case 3
+
}
        println("Value is 3")
+
break;
break
+
     Case $var == 3:
     Case "test"
+
{
        println("Value is \"test\"")
+
            println("Value is 3");
break
+
}
     Default
+
break;
        println("No preceding case was true!")
+
     Case $var != 4:
EndSelect
+
{
 +
            println("Value is not 4");
 +
}
 +
break;
 +
    Case $var == "test":
 +
{
 +
            println("Value is \"test\"");
 +
}
 +
break;
 +
     Default:
 +
{
 +
            println("No preceding case was true!");
 +
}
 +
}
 +
return 1;
 
</syntaxhighlight>
 
</syntaxhighlight>
 
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.
 
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:38, 14 June 2015

Contents

Select...Case...Default

Description

Conditionally run statements.

Select
{
    Case <expressions>
    {
        statement1
        ...
    }
    Case <expressions>
    {
        statement2
        ...
    }
    CaseID <Identifier> <expressions>
    {
        statement2
        ...
    }
    Default
    {
        statementN
        ...
    }
}

Parameters

Case <expression>

Optional; You can use none, one or more.

If the case is true it will be executed.

If you type CaseID instead of Case you can assign an ID to it like CaseID Testy: then you can jump to that case using goto _caseTesty;

(You can separate expressions to check using a , )

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.

Default

Optional; You can use one or none.

If none of the cases are true the code in Default will be executed.

You dont need a Break in the Default.

You can place the default anywhere in the statement.

Remarks

Select statements may be nested.

Strings are case sensitive when used in a case.

A select acts very much like an If statement however it lets you fallthrough if thats useful to you.

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.

You can place a goto _default; inside a case to instantly jump to the default: statement if one exists (This uses a hardcoded jump and does not reply on Sputniks goto at all so it should be be a lot faster).

You can place a goto _case0; inside a case to instantly jump to the case ID you specify you change 0 to the id of the case you wish to jump to. (This uses a hardcoded jump and does not reply on Sputniks goto at all so it should be be a lot faster).

You can place a goto _caseFox; inside a case to instantly jump to the case with the name you specify you change Fox to the Name of the case you wish to jump to. (This uses a hardcoded jump and does not reply on Sputniks goto at all so it should be be a lot faster).

Note - When you use goto _case stuff it will instantly begin executing code in that case it will NOT check if the case is a match for the Select (A warning will be given if the case/default you want to jump to does not exist).

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;
Select
{
    Case $var == 1:
    Case $var == 2:
	{
            println("Value is 1 or 2");
	}
	break;
    Case $var == 3:
	{
            println("Value is 3");
	}
	break;
    Case $var != 4:
	{
            println("Value is not 4");
	}
	break;
    Case $var == "test":
	{
            println("Value is \"test\"");
	}
	break;
    Default:
	{
            println("No preceding case was true!");
	}
}
return 1;
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox