if...else if...else

 
 
 

You’ll often want your program to make decisions and change its behavior based on testing some condition. For example, only print a value if it is greater than 10. Like most languages, MEL has an if control structure:

if ($x > 10) {
	print("It's greater than 10!\n");
	print("Run!!!\n");
}

You can also specify code to run when the condition is not true with the else keyword:

if ($x > 10) {
	print("It's greater than 10!\n");
	print("Run!!!\n");
} else {
	print("It's not above 10.\n");
	print("It's safe... for now.\n");
}

You can specify several alternatives with the else if statement:

if ( $color == "blue" )
	print("Sky\n"); 
else if ( $color == "red" )
	print("Fire\n");
else if ($color == "yellow" )
	print("Sun\n");
else
	print("I give up!\n");

Creative Commons License Except where otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License