next up previous
Next: Scope of variables defined Up: Unit 05 Previous: The if variant

Block of statements

The syntax of if-else allows us to have only a single statement in the then-branch (or the else-branch). If we want to execute more than one statement in the then-branch (or the else-branch), we have to use a block construct. A block of statements groups several statements in a single composite statement.


Block of statements

Syntax:

{
  statement
  ...
  statement
}

Semantics:

The statements in the block are executed in sequence. The variables declared inside the block are not visible outside the block itself.

Example:

int a, b, bigger;
...
if (a > b) {
  bigger = a;
  System.out.println("smaller = " + b);
}


next up previous
Next: Scope of variables defined Up: Unit 05 Previous: The if variant