Java if else Statements

Programs often need to make decisions — do one thing if a condition is true, and something different if it is false. Java uses if-else statements to handle this kind of conditional logic. These are the building blocks of decision-making in any program.

The if Statement

The simplest form of conditional logic is the if statement. It executes a block of code only if the given condition evaluates to true.

Syntax

if (condition) {
    // code to run if condition is true
}

Example

int temperature = 35;

if (temperature > 30) {
    System.out.println("It is hot outside.");
}

Output:

It is hot outside.

If the temperature were 25, the condition would be false and nothing would be printed.

The if-else Statement

The else block runs when the if condition is false. This provides a two-way decision.

Syntax

if (condition) {
    // runs if condition is true
} else {
    // runs if condition is false
}

Example

int marks = 45;

if (marks >= 50) {
    System.out.println("Result: Pass");
} else {
    System.out.println("Result: Fail");
}

Output:

Result: Fail

The if-else if-else Statement

When there are more than two possible outcomes, use else if to chain multiple conditions. Java evaluates each condition from top to bottom and executes the first block where the condition is true.

Syntax

if (condition1) {
    // runs if condition1 is true
} else if (condition2) {
    // runs if condition2 is true
} else if (condition3) {
    // runs if condition3 is true
} else {
    // runs if none of the above conditions are true
}

Example – Grade Calculator

int score = 72;

if (score >= 90) {
    System.out.println("Grade: A");
} else if (score >= 80) {
    System.out.println("Grade: B");
} else if (score >= 70) {
    System.out.println("Grade: C");
} else if (score >= 60) {
    System.out.println("Grade: D");
} else {
    System.out.println("Grade: F");
}

Output:

Grade: C

Nested if Statements

An if statement can be placed inside another if statement. This is called a nested if. It is used when a decision depends on another decision first being true.

Example – Loan Eligibility Check

int age = 25;
int income = 40000;

if (age >= 18) {
    if (income >= 30000) {
        System.out.println("Eligible for loan.");
    } else {
        System.out.println("Income too low for a loan.");
    }
} else {
    System.out.println("Must be 18 or older to apply.");
}

Output:

Eligible for loan.

Short if Without Braces

If only one statement needs to execute inside an if block, the curly braces can be omitted. However, this is generally not recommended for readability and safety.

int x = 10;

if (x > 5)
    System.out.println("x is greater than 5");   // one statement, braces optional

Always use braces to avoid confusion and bugs, especially when adding more statements later.

Using Logical Operators in Conditions

Conditions can be combined using logical operators && (AND), || (OR), and ! (NOT).

Example – Combined Conditions

int age = 20;
boolean hasTicket = true;

if (age >= 18 && hasTicket) {
    System.out.println("Entry allowed.");
} else {
    System.out.println("Entry not allowed.");
}

Output:

Entry allowed.

Example – Using OR

int day = 6;   // 6 = Saturday, 7 = Sunday

if (day == 6 || day == 7) {
    System.out.println("It's the weekend!");
} else {
    System.out.println("It's a weekday.");
}

Output:

It's the weekend!

Comparing Strings in Conditions

When checking string equality in conditions, always use .equals() — not ==.

String role = "admin";

if (role.equals("admin")) {
    System.out.println("Access granted to admin panel.");
} else {
    System.out.println("Access denied.");
}

Real-World Example – ATM Simulation

import java.util.Scanner;

public class ATM {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double balance = 1000.00;

        System.out.print("Enter amount to withdraw: ");
        double amount = sc.nextDouble();

        if (amount <= 0) {
            System.out.println("Invalid amount entered.");
        } else if (amount > balance) {
            System.out.println("Insufficient balance.");
        } else {
            balance -= amount;
            System.out.printf("Withdrawal successful. Remaining balance: %.2f%n", balance);
        }

        sc.close();
    }
}

Sample Interaction:

Enter amount to withdraw: 200
Withdrawal successful. Remaining balance: 800.00

Summary

  • The if statement runs code only when a condition is true.
  • The else block handles the false case.
  • Use else if for multiple branching conditions.
  • Nested if statements allow checking a condition inside another condition.
  • Use &&, ||, and ! to combine multiple conditions.
  • Always use .equals() to compare String values in conditions.

Leave a Comment

Your email address will not be published. Required fields are marked *