Expressions, Statements, and Blocks

Expressions

Expressions perform the work of a Java program. Among other things, expressions are used to compute and assign values to variables and to help control the execution flow of a program. The job of an expression is two-fold: perform the computation indicated by the elements of the expression and return some value that is the result of the computation.

The data type of the value returned by an expression depends on the elements used in the expression. The expression aChar = 'S' returns a character because the assignment operator returns a value of the same data type as its operands and aChar and 'S' are characters. As you see from the other expressions, an expression can return a boolean value, a string, and so on.

The Java programming language allows you to construct compound expressions and statements from various smaller expressions as long as the data types required by one part of the expression matches the data types of the other. Here's an example of a compound expression:

x * y * z
x + y / 100
You can specify exactly how you want an expression to be evaluated by using balanced parentheses ( and ). For example to make the previous expression unambiguous, you could write (x + y)/ 100.

To make your code easier to read and maintain you should be explicit and indicate with parentheses which operators should be evaluated first.

Statements

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution.

The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).

In addition to the expression statements described above, declaration statements and control flow statements also constitute a statement. A declaration statement is a statement that declares a local variable. Control flow statements are listed in the following table:

Statement Type Keyword
looping while, do-while , for
decision making if-else, switch-case
exception handling try-catch-finally, throw
branching break, continue, label:, return

Blocks

A block is a collection of statements between balanced curly braces.
if (Character.isUpperCase(aChar)) {
    System.out.println("The character " + aChar + " is upper case.");
} else {
    System.out.println("The character " + aChar + " is lower case.");
}
The braces themselves are not considered part of the block; they just delimit it.