If-Else Statements in Java | Control Flow in Java
The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java.
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
The if statement is a control flow statement in Java that allows you to execute a block of code conditionally. This means the code inside the if block will run only if the specified condition evaluates to true.
Key Characteristics
- The if statement evaluates a boolean expression (e.g., x > 10, y == 5).
- If the expression is true, the code block inside the if is executed.
- If the expression is false, the program skips the if block and moves to the next statement.
Syntax of If-Else Statement:
Syntax Example
if (condition) {
// Code block for true condition
}
Example of If Statement in Java:
In this example, the if- statement checks whether the sum of x
and y
is greater than 20.
Code Example
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 11;
if (x + y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
Output
If-Else Flowchart:
The flowchart below illustrates the control flow of an if-else statement.

The if-else statement in Java is a basic control flow statement that allows you to execute a block of code if a certain condition is true and another block of code if the condition is false. This decision-making feature provides flexibility in program logic.
Key Points on If-Else Statements:
- The if statement evaluates a condition. If the condition is true, the corresponding code block executes.
- If the if condition is false, the program checks the else block and executes it if present.
- Only one of the code blocks between if or else will execute at runtime.
- It is commonly used for scenarios requiring true or false decision-making logic.
Syntax of If-Else Statement:
Syntax Example
if (condition) {
// Code block for true condition
} else {
// Code block for false condition
}
Example of If-Else Statement in Java:
In this example, the if-else statement checks whether the sum of x
and y
is greater than 20.
Code Example
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 8;
if (x + y > 20) {
System.out.println("x + y is greater than 20");
} else {
System.out.println("x + y is less than or equal to 20");
}
}
}
Output
If-Else Flowchart:
The flowchart below illustrates the control flow of an if-else statement.

if else laddar
The if-else ladder is a control flow structure in Java used to evaluate multiple conditions in sequence. It allows you to execute a specific block of code when one of several conditions is true. If none of the conditions are true, an optional else block can handle the default action.
Key Characteristics of If-Else Ladder
- Conditions are checked one by one, starting from the top.
- As soon as a condition evaluates to true, the corresponding block of code is executed, and the rest of the ladder is skipped.
- Only one block of code in the ladder is executed, even if multiple conditions are true.
Syntax of If-Else Statement:
Syntax Example
if (condition) {
// Code to execute if condition2 is true
}
else if {
// Code to execute if condition2 is true
}
else if {
//Code to execute if condition3 is true
}
else{
// Code to execute if none of the conditions are true
}
Example of If-Else Statement in Java:
In this example, the if-else statement checks whether the sum of x
and y
is greater than 20.
Code Example
public class Main {
public static void main(String[] args) {
int marks = 75;
if (marks >= 90) {
System.out.println("Grade: A");
}
else if (marks >= 80) {
System.out.println("Grade: B");
}
else if (marks >= 70) {
System.out.println("Grade: C");
}
else if (marks >= 60) {
System.out.println("Grade: D");
}
else {
System.out.println("Grade: F");
}
}
}
Output
Detailed Explanation:
- If Condition: The program checks the condition within the if statement. If it evaluates to true, the if block executes.
- Else Condition: If the if condition evaluates to false, the program bypasses the if block and executes the else block, if provided.
- This structure is useful for binary decisions where only one outcome can occur at a time.
By mastering the if-else statement, developers can create dynamic and responsive Java programs that respond to various conditions effectively.