Compound Statements


Compound statements

In several places Java requires you to write one statement. But you might want to put several statements there instead of just one. You do that by packaging several statements up into a single statement, called a compound statement. Think of it as like putting a few things into a box and closing the box, so that it looks like just one thing, the box.

A compound statement begins with a left curly brace and ends with a right curly brace. In between, write any sequence of statements, one after another.

For example, compound statement

  {
    x = 1;
    y = 0;
  }
contains two smaller statements. The statements are done one after the other, from the beginning to the end.

You can put any number of statements inside the braces, including only one, or even none. Compound statement { } is a do-nothing statement.


Indentation

You normally indent what is inside a compound statement. Line up the left and right braces vertically, so that the right brace is directly below the left brace. Indent what is inside so that there is nothing but space directly between the left and right braces. For example,

  {
    x = x + 1;
    r = r + x;
  }
is sensibly indented, but
  {
  x = x + 1;
  r = r + x;
  }
is not sensibly indented, since there are characters (x and r) between the braces. Even
  {
 x = x + 1;
 r = r + x;
  }
is not sensibly indented. There should be nothing inside the braces that is further to the left than the braces are.

You indent to show structure. That is, you indent to show that a statement is part of a larger statement. Be sure that indentation does show structure. Do not indent without reason. For example,

  {
    x = x + 1;
      r = r + x;
  }
is not sensibly indented. If two statements are done one after the other, they should be indented the same amount.


Context

A Java program is broken into a lot of small contexts. Any variable that is created within one context only exists while the program is running in that context.

A compound statement is one example of a context. So any variable that is created inside a compound statement is destroyed as soon as the program leaves that compound statement. For example,

  {
   int x = 0;
   int w = x + 1;
  }
  int y = w;
is not allowed because w only exists while the program is inside the compound statement where w was declared. As soon as the program reaches the end of the compound statement, variables that were created (or declared) inside that compound statement are destroyed.


Problems

  1. [solve] What is a compound statement used for?

  2. [solve] What does a compound statement begin with? What does it end with?

  3. [solve] What does it mean that a compound statement is a context?


Summary

A compound statement is a sequence of zero or more statements enclosed in {...}. It is considered to be a single statement.

A compound statement is a context. Any variables that are created inside the compound statement are destroyed at the end of it.


Review

Java types include int, long, double, boolean, char and String.

To create a variable called zebra of type double, and give it an initial value of 1.0, write

  double zebra = 1.0;
Do not forget the semicolon at the end.

To change zebra to hold 2.0, write

  zebra = 2.0;
Do not write a type when you want to change an existing variable. The type is a cue to the Java compiler that you are creating a new variable.