In this tutorial, you will learn about if-else in Java with syntax and practical examples. The if-else statement is one of the most fundamental decision-making statements in Java programming. It is a conditional control statement that allows your program to execute different code blocks based on specific conditions.
An if-else statement executes a block of code if the given condition evaluates to true; otherwise, it executes another block of code. The condition is a boolean expression that determines the program’s flow depending on whether it’s true or false.
In simple terms, the if-else statement helps Java programs make logical decisions, just like humans do — choosing different actions based on different situations.
Table of Contents
Real-Life Example of If Else in Java
In everyday life, we often make decisions based on conditions. For example:
- If it rains → Take an umbrella
- Else → Go without one
Here’s what happens:
- The condition is: Is it raining?
- If the condition is true (yes, it’s raining), you take an umbrella.
- If the condition is false (no, it’s not raining), you don’t take one.
This is exactly how the if-else statement works in Java.
In Programming Terms
You can represent the same logic in Java like this:
if (isRaining) {
System.out.println("Take an umbrella.");
} else {
System.out.println("Go without one.");
}
Here:
isRaining
is a boolean variable which will result either true or false.- The program checks the condition.
- If isRaining is true, it executes the first block (take an umbrella).
- Otherwise, it executes the second block (go without one).
Just like in real life, where you make choices based on situations, the if-else statement in Java helps your program decide what to do next based on the specified conditions.
Types of If-Else Statements in Java
Java provides several types of if-else statements to handle different conditions. They are:
- Simple if statement
- if-else statement
- if-else-if ladder
- Nested if statement
Let’s learn about each variation with syntax and practical examples.
Simple if Statement in Java
The simple if statement is the most basic form of decision-making statement in Java. It allows the program to execute a block of code only if a specified condition is true. If the condition is false, the program simply skips that block and continues with the next statement.
Syntax of if Statement
The general syntax of a simple if statement in Java is as follows:
if (condition) {
// Code to be executed if the condition is true
}
In the above syntax of if statement,
- if → This is a keyword that begins the conditional statement.
- condition → It must be a boolean expression, which returns either true or false value.
- {} (curly braces) → This enclose the block of code to execute if the condition is true.
If the condition evaluates to true, the code inside the if block runs. If it’s false, the code inside the block is skipped.
Let’s take some important examples based on the simple if statement in Java.
Example 1: Check Positive Number
public class IfExample {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}
System.out.println("Program ended.");
}
}
Output:
The number is positive.
Program ended.
In this example, the condition (number > 0) is true, so it prints “The number is positive” on the console. Then, it continues executing the rest of the code.
Example 2: No Output When Condition is False
public class IfExample2 {
public static void main(String[] args) {
int number = -5;
if (number > 0) {
System.out.println("The number is positive.");
}
System.out.println("Program completed.");
}
}
Output:
Program completed.
In this example, the condition (number > 0) is false. So, the statement inside if block is skipped. Only “Program completed.” is printed on the console.
Example 3: Using if Statement with Boolean Variable
public class IfBooleanExample {
public static void main(String[] args) {
boolean isJavaFun = true;
if (isJavaFun) {
System.out.println("Yes! Java is fun to learn.");
}
System.out.println("Keep coding!");
}
}
Output:
Yes! Java is fun to learn.
Keep coding!
Key Points to Remember about Simple If Statement
- The if condition must return true or false.
- If the condition is true, the if block runs.
- If the condition is false, the if block is skipped.
- Use curly brcaes {} even for single statements for better readability.
- Avoid using = (assignment) instead of == (comparison).
If-Else Statement in Java
The if-else statement in Java is a decision-making statement that executes the if block of code when a condition is true and else block of code when the condition is false. It is one of the most commonly used conditional statements in Java programming because it enables the program to take alternative actions depending on different conditions.
Syntax of if-else Statement
The general syntax of if-else statement in Java is as:
if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
Flowchart Diagram of If-Else Statement
Look at the below diagram in which you can see the flowchart of if-else in Java.

Examples of If-Else Statement in Java
Let’s take some important practical examples based on the if-else statement in Java.
Example 1: Check Whether a Number is Positive or Negative
public class IfElseExample1 {
public static void main(String[] args) {
int number = -10;
// Applying if-else statement.
if (number >= 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is negative.");
}
}
}
Output:
The number is negative.
Since the conditional expression (number >= 0) evaluates to false, the else block of code executes and prints “The number is negative.” on the console.
Example 2: Check Whether a Number is Even or Odd
public class EvenOddCheck {
public static void main(String[] args) {
int number = 9;
// Applying if-else statement to check even or odd.
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}
Output:
9 is odd.
In this example, the condition (number % 2 == 0) checks if the number is divisible by 2. Since the number 9 is not divisible by 2, the condition is false, and the else block of code executes.
Example 3: Check Eligibility for Voting
public class VotingEligibility {
public static void main(String[] args) {
int age = 17;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote yet.");
}
}
}
Output:
You are not eligible to vote yet.
Since the conditional expression (age >= 18) evaluates to false, so the program executes the code inside the else block.
Example 4: Compare Two Numbers
public class CompareNumbers {
public static void main(String[] args) {
int a = 15, b = 20;
if (a > b) {
System.out.println(a + " is greater than " + b);
} else {
System.out.println(b + " is greater than " + a);
}
}
}
Output:
20 is greater than 15
Since the condition (a > b) evaluates to false because 15 is not greater than 20. So, the code inside else block executes.
Example 5: Check Whether a Person Passes or Fails
public class ResultCheck {
public static void main(String[] args) {
int marks = 45;
if (marks >= 40) {
System.out.println("Congratulations! You passed the exam.");
} else {
System.out.println("Sorry! You failed the exam.");
}
}
}
Output:
Congratulations! You passed the exam.
Example 6: Using if-else with User Input
import java.util.Scanner;
public class IfElseUserInput {
public static void main(String[] args) {
// Create Scanner class object to take input from the user or keyboard.
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
if (age >= 18) {
System.out.println("You are eligible to get a driving license.");
} else {
System.out.println("You are not eligible yet.");
}
}
}
Output:
Enter your age: 16
You are not eligible yet.
In this example, we have used the Scanner class to take input from the user (keyboard input). This class is part of the java.util package. We have created an object sc of the Scanner class. System.in means the program will take input from the keyboard. The nextInt() function of Scanner class reads the user’s input as an integer and stores it in the variable age.
The condition (age >= 18) in the if-else statement checks whether the user’s age is 18 or above. If the condition evaluates to true, it displays “You are eligible to get a driving license.”. Otherwise, it displays “You are not eligible yet.” if the condition evaluates to false.
Key Takeaways about If Else Statement
- The decision-making if-else statement executes a specific block of code based on true or false.
- The if condition must be a boolean expression.
- The else block is optional. You can use only if if you need.
- Always use curly braces {} to avoid ambiguity.
- Logical comparison (>=) determines which block of code executes.
If-Else-If Ladder in Java
The if-else-if ladder in Java is a multiway decision-making statement used when you need to check multiple conditions sequentially. If one condition evaluates to true, its corresponding block of code executes, and the rest of the conditions are skipped.
Syntax of if-else-if Ladder
The general syntax of if-else-if ladder in Java is as:
if (condition1) {
// Code executes if condition1 is true
} else if (condition2) {
// Code executes if condition2 is true
} else if (condition3) {
// Code executes if condition3 is true
} else {
// Code executes if all above conditions are false
}
How if-else-if Ladder Works in Java?
The if-else-if ladder in Java works as follows:
- The program first checks condition1 inside the if statement.
- If condition1 evaluates to true, it executes that block and exits the ladder.
- If condition1 evaluates to false, it checks the next condition, which is condition2.
- This process continues until one condition is true.
- If no condition is true, the else block executes.
Examples of if-else-if Ladder in Java
Let’s take some important practical examples based on if-else-if ladder in Java programming.
Example 1: Age-Based Eligibility Check
import java.util.Scanner;
public class AgeEligibilityCheck {
public static void main(String[] args) {
// Create Scanner class object to take the user input from the keyboard.
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
if (age < 0) {
System.out.println("Invalid age entered!");
} else if (age < 18) {
System.out.println("You are not eligible for a driving license.");
} else if (age >= 18 && age <= 70) {
System.out.println("You are eligible to get a driving license.");
} else {
System.out.println("You are too old for a new license.");
}
}
}
Output:
Enter your age: 16
You are not eligible for a driving license.
Example 2: Grading System Using if-else-if
public class GradingSystem {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
System.out.println("Grade A+");
} else if (marks >= 75) {
System.out.println("Grade A");
} else if (marks >= 60) {
System.out.println("Grade B");
} else if (marks >= 40) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
}
}
Output:
Grade A
In if-else-if ladder, Java checks each condition from top to bottom. Since marks = 85, the condition (marks >= 75) evaluates to true, so Grade A is displayed on the console. After execution of corresponding block, the remaining conditions are skipped.
Example 3: Largest Among Three Numbers
public class LargestNumber {
public static void main(String[] args) {
int a = 10, b = 25, c = 15;
if (a > b && a > c) {
System.out.println(a + " is the largest number.");
} else if (b > a && b > c) {
System.out.println(b + " is the largest number.");
} else {
System.out.println(c + " is the largest number.");
}
}
}
Output:
25 is the largest number.
Key Points to Remember about if-else-if Ladder
- The if-else-if ladder checks multiple conditions sequentially.
- It executes only one block if the condition is true.
- The else block is optional but recommended for handling unexpected cases.
- All the conditions are evaluated from top to bottom.
- Make sure conditions don’t overlap to avoid logic errors.
FAQ on If-Else in Java
1. What is the purpose of if-else in Java?
The if-else statement controls the flow of program based on conditions. It decides which block of code should execute depending on whether a condition is true or false.
2. Can we use multiple if statements without else?
Yes, we can use multiple independent if statements when conditions are unrelated.
3. What happens if both if and else conditions are true?
Only the if block runs if its condition is true. The else block runs only when the if condition is false.
4. Is else block mandatory in Java?
No, the else block is always optional. A simple if statement is perfectly valid.
5. Can we use if-else inside a loop?
Yes, you can use if-else statement inside for, while, or do-while loops.