Java Flow Control
By: Aljohn S. Marilag
Java Flow Control Statements
Java compiler executes the code from top to
bottom. The statements in the code are
executed according to the order in which they
appear. However, Java provides statements
that can be used to control the flow of Java
code. Such statements are called Flow Control
Statements. It is one of the fundamental
features of Java, which provides a smooth flow
of program.
Three types of Control Flow Statements
I. Decision-Making statements III. Jump statements
1. if statements 1. break statement
2. continue statement
2. switch statement
II. Loop statements
1. do while loop
2. while loop
3. for loop
4. for-each loop
Decision-Making/ Conditional
Statements
Typically, it compares two values so that our
program can decide what action should be
taken.
The syntax of an if-then statement is:
Java If-else Statement
public static void main(String[] args) {
boolean yes = true;
boolean no = false;
if (yes) {
[Link]("The number is negative");
[Link]("Statement outside if block");
}
public static void main(String[] args) {
int number = 10;
if (number < 0) {
[Link]("The number is negative");
[Link]("Statement outside the if block");
}
Java If-else Statement
In programming, we use the if-else statement
to run a block of code among more than one
alternatives.
For example, assigning grades (A, B, C) based
on the percentage obtained by a student.
- if the percentage is above 90, assign grade A
- if the percentage is above 75, assign grade B
- if the percentage is above 65, assign grade C
Syntax of If-else Statement
Java If-else-if Statement
The if-else-if statement contains the if-
statement followed by multiple else-if
statements. In other words, we can say that
it is the chain of if-else statements that
create a decision tree where the program
may enter in the block of code where the
condition is true. We can also define an else
statement at the end of the chain.
Syntax of If-else-if Statement