Branches allow a Java program to alter the flow of execution and run one piece of code instead of another. The figure below shows how a Java program flows without branches:
If the Boolean variable
, the first statement is executed and
is set to
Otherwise the second statement is executed and
is set to
part of the
statement is optional. In other words, you can leave it off if you have only a single statement that you want to execute conditionally.
It is also possible to link together a series of
statements to get the effect of multiple branches. Variables:
Variables are locations in memory that are used to store information.
You can think of variables as storage containers designed to hold data of a certain type.
You are not limited to executing an individual line of code with the
To execute multiple lines of code you can use a compound statement
, which is a block of code surrounded by curly braces
statement interprets a compound statement as a single statement. Following is an example that demonstrates using a compound statement: