Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
Java keywords

else Keyword in Java

The else keyword in Java is used in conjunction with the if statement to execute a block of code when the condition specified in the if statement evaluates to false. It provides an alternative path of execution in conditional logic.

Usage

The else keyword is used to define a block of code that will be executed if the if condition is not met. It is often paired with if and can be extended with else if for multiple conditional checks.

Syntax

if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}
  • condition: A boolean expression that evaluates to true or false.

Examples

Example 1: Basic else Statement

public class ElseExample {
    public static void main(String[] args) {
        int number = 10;
        if (number > 0) {
            System.out.println("The number is positive.");
        } else {
            System.out.println("The number is not positive.");
        }
    }
}

In this example, the program checks if the variable number is greater than 0. If true, it prints "The number is positive."; otherwise, it prints "The number is not positive."

Example 2: else if Ladder

public class ElseIfExample {
    public static void main(String[] args) {
        int score = 85;
        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 {
            System.out.println("Grade: F");
        }
    }
}

This example demonstrates the use of an else if ladder. The program assigns a grade based on the value of score. If none of the if or else if conditions are met, the else block is executed.

Example 3: Nested else Statement

public class NestedElseExample {
    public static void main(String[] args) {
        int age = 20;
        if (age >= 18) {
            if (age >= 21) {
                System.out.println("You are an adult and can drink alcohol.");
            } else {
                System.out.println("You are an adult but cannot drink alcohol.");
            }
        } else {
            System.out.println("You are not an adult.");
        }
    }
}

In this example, the program uses nested if and else statements to provide more granular checks. It first checks if age is 18 or older, and then checks if age is 21 or older.

Tips and Best Practices

  • Readability: Use proper indentation and formatting to make if-else blocks easy to read and understand.
  • Complex Conditions: For complex conditions, consider breaking them into multiple if-else blocks or using else if to improve readability.
  • Avoid Deep Nesting: Deeply nested if-else statements can make code hard to read and maintain. Consider refactoring such code into methods or using switch statements where appropriate.
  • Default Case: Always provide an else block to handle unexpected or default cases, ensuring that your code can handle all possible scenarios.
    if (condition) {
        // Code to execute if condition is true
    } else {
        // Code to execute if condition is false
    }
  • Boolean Expressions: Ensure that the conditions in if and else if statements are boolean expressions that evaluate to true or false.
  • Ternary Operator: For simple if-else conditions, consider using the ternary operator for conciseness.
    int number = 10;
    String result = (number > 0) ? "Positive" : "Not Positive";
    System.out.println(result);