Code blocks

Code blocks are sequences of statements (we talk about these next) surrounded by brace characters. Code blocks are used to group statements that should be executed by the containing control statement, such as the arms of the if else statement:

If (a == 3) {
  print("a is three");
  c = 5;
} else {
  print("a is something other than 3");
  c = 6;
}

Unlike Java, code blocks can do not act as containers for the lexical scoping of variables. For example, a local variable created within the code block of an if statement will be created within the namespace where the if statement occurs:

if (a>0) {
  c = 5;
} else {
  c = 6;
}
print(c); // this works in BeanShell, but not in Java!