Main Index : Reference :
switch (expression)
{
case value1:
// Do something
break;
case value2:
case value3:
// Do something else
break;
case value 4:
// Do something
break;
[...]
default:
// If no value matches expression
}
switch
structure first evaluates the value of expression
and then jumps to the case:
label that has a matching value. If no match is found it jumps to the default:
label. The code below the matching label is executed down to the first break
statement. This can be used to let several case
statements lead to the same code.
Remember: Don't forget to add the break
statements! Otherwise all code in all cases below the matching case the will also be executed, since no break statement is found. (The only exception is when there already is a return
statement for each case.)
// Outputs "Three.".
switch (3)
{
case 0:
println("Zero.");
break;
case 1:
println("One.");
break;
case 2:
println("Two.");
break;
case 3:
println("Three.");
break;
default:
println("Other.");
break;
}
// Outputs "It's a number". Notice how both DT_INT
// and DT_FLOAT lead to the same output.
var x = 5.5
switch(typeof(x))
{
case DT_INT:
case DT_FLOAT:
println("It's a number.");
break;
case DT_STRING:
println("It's a string.");
break;
default:
println("I don't know what it is.");
}