Select

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Remarks)
(Example)
Line 93: Line 93:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In the above example:
+
This one uses the goto _default; to instantly jump to the default statement
  
if $var is 1 it will go into Case 1 then fall through into case 2 run the code and break
+
<syntaxhighlight lang="sputnik">
 +
$a = "Cat";
  
if $var is 2 it will go into Case 2 run the code and break
+
Switch( $a )
 +
{
 +
case "FoX":
 +
say "FoX";
 +
break;
 +
case "Cat":
 +
say "Cat";
 +
goto _default;
 +
break;
 +
Case "Dog":
 +
say "Dog";
 +
break;
 +
default:
 +
say "Default";
 +
break;
 +
}
 +
</syntaxhighlight>
  
if $var is 3 it will go into Case 3 run the code and break
 
 
if $var is not 4 it will go into Case 4 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]]

Revision as of 08:58, 19 September 2013

Contents

Select...Case...Default

Description

Conditionally run statements.

Select
{
    Case <expression>
    {
        statement1
        ...
    }
    Case <expression>
    {
        statement2
        ...
    }
    Default
    {
        statementN
        ...
    }
}

Parameters

Case <expression>

Optional; You can use none, one or more.

If the case is true it will be 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.

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.

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.

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;

This one uses the goto _default; to instantly jump to the default statement

$a = "Cat";
 
Switch( $a )
{
	case "FoX":
		say "FoX";
		break;
	case "Cat":
		say "Cat";
		goto _default;
		break;
	Case "Dog":
		say "Dog";
		break;
	default:
		say "Default";
		break;
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox