Switch / Case#
Switching on Values#
Sometimes, you might have logic using conditional statements that
ends up looking a bit cluttered, like the following first example. But, using a
switch
statement, you can make it much cleaner.
This is a little cluttered, and requires reading the x
variable several times.
if(x == 0)
do_thing_a();
else if(x > 0 && x < 5)
do_thing_b(x);
else if(x == 5)
do_thing_c();
else if(x == 6)
do_thing_d();
else do_thing_e();
This is cleaner, and only reads the x
variable once.
switch(x)
{
case 0:
do_thing_a();
break;
case 0..5: // Value Range
do_thing_b(x);
break;
case 5:
do_thing_c();
break;
case 6:
do_thing_d();
break;
default:
do_thing_e();
break;
}
Picking an outcome based on percentage chances easily using value ranges
switch(RandGen->Rand(99)) //0-99 inclusive
{
case 0=..10:
//10% chance
break;
case 10=..30:
//20% chance
break;
case 30=..60:
//30% chance
break;
case 60=..100:
//40% chance
break;
}
In a standard switch statement, each case :
provided must include either a single
compile-time constant value, which must match exactly, or a Value Range, in
which case any value in the range will match.
If more than one case :
would match, the first one from the top will match, and
the rest won’t be checked. If NO case
matches, the default:
block will run (if
provided).
break
/ fallthrough
#
The break statements are used to end the switch
at the end of
each case. If you do not include a break;
at the end of a case, the code
will fallthrough
into the case below it. This can be desirable, but can
also cause unexpected bugs if you aren’t looking out for it, as shown below:
switch(4)
{
case 4:
print("Read 4!\n");
case 5:
print("Read 5!\n");
}
/* Outputs:
Read 4!
Read 5!
*/
Switching on Strings#
Additionally, switch statements can be used to compare entire strings!
To do this, simply use string literals as the case :
values!
char32[] str = "Test";
switch(str)
{
case "Example":
printf("Some Example Text\n");
break;
case "Test":
printf("Testing Text!\n");
break;
}
// Outputs: 'Testing Text!'
The STRING_SWITCH_CASE_INSENSITIVE
option, if on
, will
make any of these switch
statements it affects compare their values
case-insensitively.
Not Yet Implemented
STRING_SWITCH_CASE_INSENSITIVE
may become deprecated in favor
of an annotation on switch statements instead.