Mel - Maya Embedded Language

<HOME>   <PREVIOUS>   <NEXT>

Loops and If Blocks - switch

if then else
for
for in
while
do while
switch

switch

The switch statement can be used to shorten a long list of if statements.
int $random= rand(0,10);
switch($random)
{
case 0:
     print "\n The number is 0";
     break;
case 1:
     print "\n The number is 1";
     break;
case 2:
     print "\n The number is 2";
     break;
case 3:
     print "\n The number is 3";
     break;
case 4:
     print "\n The number is 4";
     break;
default:
     print "\n I give up! Game over! The number is greater than 4";
     print ("\n Just kidding. The number is actually " + $random);
}
If you want more than one case to execute, omit the break. All cases that are true will execute. The switch will not exit until it reaches the end of the cases or encounters another break.