Prev Main Next

Statements

In Java, the term statement refers to something that performs an action for you. It is really more like a command, but it is called a statement.

For example, statement

  x = 1;
says to put the value 1 into variable x.


Grouping and sequencing

Often, you want to perform several statements in a row. For example, you might say

  x = 1;
  y = 0;
to first make x hold 1 and then make y hold 0. You can do this in Java, but you need to put the statements in braces {....}. For example, you write
  {
    x = 1;
    y = 0;
  }
You can put as many statements as you like in the braces. If you want to perform three statements, you might write
  {
    x = 1;
    y = 0;
    z = 10;
  }
When you make a group of statements like this, you call the entire group (from the left brace to the matching right brace) a compound statement.

Question. Do you think that you can put a compound statement inside another compound statement? Answer[4]


Indentation

When you write a compound statement, indent the statements that are inside the braces. Line up the left and right brace, so that the left brace is directly above the right brace. There should not be any characters between the braces. If you start at the left brace and draw a straight line to the matching right brace, that line should not pass through any letters of symbols that are parts of other statements.

Question. How should you write the following using a better style of indentation?

  {x = 1; y
  = 2;
  z = 3;}
Answer[5]


Prev Main Next