Switch

From Sputnik Wiki
Revision as of 19:59, 22 September 2013 by UberFoX (Talk | contribs)
Jump to: navigation, search

Contents

Select...Case...Default

Description

Conditionally run statements.

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

Parameters

Switch <expression>

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

Case <expressions>

Optional; You can use none, one or more.

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.

(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.

Remarks

Switch statements may be nested.

Strings are case sensitive when used in a case.

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).

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 4, 7, 9:
	{
            println("Value is 4 or 7 or 9");
	}
	break;
    Case array(100, 200, 300, "moo"):
	{
            println("Value is either 100 or 200 or 300 or \"moo\"");
	}
	break;
    Case 7..10:
	{
            println("Value is between 7 and 10");
	}
	break;
    Case 0x10..0x20:
	{
            println("Value is a hex number between 0x10 and 0x20");
	}
	break;
    Case 'A'..'F':
	{
            println("Value is a char between A and F");
	}
	break;
    Case "test":
	{
            println("Value is \"test\"");
	}
	break;
    Case "test", "dog":
	{
            println("Value is \"test\" or \"dog\"");
	}
	break;
    Default:
	{
            println("No preceding case was true!");
	}
}

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