6/10/2021 Control Flow in Java - Javatpoint
Control Flow in Java
Java compiler executes the java code from top to bottom. The statements 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 control flow statements.
Java provides three types of control flow statements.
1. Decision Making statements
2. Loop statements
3. Jump statements
Decision-Making statements:
Decision-making statements evaluate the Boolean expression and control the program flow
depending upon the condition result. There are two types of decision-making statements in java,
I.e., If statement and switch statement.
If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted
depending upon the condition result that is a Boolean value, either true or false. In java, there are
four types of if-statements given below.
1. if statement
2. if-else statement
3. else-if statement
4. Nested if-statement
Let's understand the if-statements one by one.
1. if statement:
⇧
https://www.javatpoint.com/control-flow-in-java 2/20
,6/10/2021 Control Flow in Java - Javatpoint
Triggers in SQL (Hindi)
This is the most basic statement among all control flow statements in java. It evaluates a Boolean
expression and enables the program to enter a block of code if the expression evaluates to true.
Syntax of if statement is given below.
if(<condition>) {
//block of code
}
Consider the following example in which we have used the if statement in the java code.
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20 ⇧
https://www.javatpoint.com/control-flow-in-java 3/20
, 6/10/2021 Control Flow in Java - Javatpoint
2. if-else statement
The if-else statement is an extension to the if-statement, which uses another block of code, I.e.,
else block. The else block is executed if the condition of the if-block is evaluated as false.
Consider the following example.
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
System.out.println("x + y is less than 10");
} else {
System.out.println("x + y is greater than 20");
}
}
Output:
x + y is greater than 20
3. lse-if statement
The 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 any block of code. We can also define an else statement at the end of the
chain.
Consider the following example.
⇧
https://www.javatpoint.com/control-flow-in-java 4/20
Control Flow in Java
Java compiler executes the java code from top to bottom. The statements 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 control flow statements.
Java provides three types of control flow statements.
1. Decision Making statements
2. Loop statements
3. Jump statements
Decision-Making statements:
Decision-making statements evaluate the Boolean expression and control the program flow
depending upon the condition result. There are two types of decision-making statements in java,
I.e., If statement and switch statement.
If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted
depending upon the condition result that is a Boolean value, either true or false. In java, there are
four types of if-statements given below.
1. if statement
2. if-else statement
3. else-if statement
4. Nested if-statement
Let's understand the if-statements one by one.
1. if statement:
⇧
https://www.javatpoint.com/control-flow-in-java 2/20
,6/10/2021 Control Flow in Java - Javatpoint
Triggers in SQL (Hindi)
This is the most basic statement among all control flow statements in java. It evaluates a Boolean
expression and enables the program to enter a block of code if the expression evaluates to true.
Syntax of if statement is given below.
if(<condition>) {
//block of code
}
Consider the following example in which we have used the if statement in the java code.
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20 ⇧
https://www.javatpoint.com/control-flow-in-java 3/20
, 6/10/2021 Control Flow in Java - Javatpoint
2. if-else statement
The if-else statement is an extension to the if-statement, which uses another block of code, I.e.,
else block. The else block is executed if the condition of the if-block is evaluated as false.
Consider the following example.
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
System.out.println("x + y is less than 10");
} else {
System.out.println("x + y is greater than 20");
}
}
Output:
x + y is greater than 20
3. lse-if statement
The 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 any block of code. We can also define an else statement at the end of the
chain.
Consider the following example.
⇧
https://www.javatpoint.com/control-flow-in-java 4/20