A block is a group of expressions that can stand in for a single expression. Blocks are surrounded by curly braces:
{ print("Hello there."); print("Glad to meet you."); print("So long!"); }
For example, the if statement has this form:
if (condition) exp1
exp1 can be a single expression:
if ($x > 5) print("It's more than 5!");
if ($x > 5) { print("It's more than 5!"); $x = 0; $y++; }
Blocks will become important when you start to use conditional and looping statements.
In MEL, unlike most languages, all statements inside a block (surrounded by curly braces) must end in semicolons, even if it is the only statement in the block.
if ($s > 10) {print("Glonk!")} // Syntax error. if ($s > 10) {print("Glunk!");} // Notice the semicolon.
Blocks can also be useful to limit the scope of a variable, since any local variable declared in a block is only visible inside that block:
int $test = 10; { int $test = 15; print($test+"\n"); } print($test+"\n"); // Result: 15 10
Except where otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License