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

if Keyword in Java

The if keyword in Java is a conditional statement that allows you to execute a block of code only if a specified condition evaluates to true. It is one of the fundamental control flow statements in Java, enabling decision-making in your programs.

Usage

The if statement is used to test a condition. If the condition is true, the code block inside the if statement is executed. If the condition is false, the code block is skipped.

Syntax

if (condition) {
    // code to be executed if condition is true
}
  • condition: An expression that evaluates to a boolean value (true or false).

Examples

Example 1: Basic if Statement

public class IfExample {
    public static void main(String[] args) {
        int number = 10;
        if (number > 5) {
            System.out.println("The number is greater than 5.");
        }
    }
}

In this example, the condition number > 5 evaluates to true because number is 10. Therefore, the message "The number is greater than 5." is printed to the console.

Example 2: if-else Statement

public class IfElseExample {
    public static void main(String[] args) {
        int number = 3;
        if (number > 5) {
            System.out.println("The number is greater than 5.");
        } else {
            System.out.println("The number is not greater than 5.");
        }
    }
}

Here, the condition number > 5 evaluates to false because number is 3. Therefore, the else block is executed, and the message "The number is not greater than 5." is printed.

Example 3: if-else if-else Statement

public class IfElseIfElseExample {
    public static void main(String[] args) {
        int number = 7;
        if (number > 10) {
            System.out.println("The number is greater than 10.");
        } else if (number > 5) {
            System.out.println("The number is greater than 5 but less than or equal to 10.");
        } else {
            System.out.println("The number is 5 or less.");
        }
    }
}

In this example, the if condition number > 10 is false, so it checks the else if condition number > 5, which is true. Therefore, the message "The number is greater than 5 but less than or equal to 10." is printed.

Tips and Best Practices

  • Readable Conditions: Ensure that the conditions within if statements are easy to understand and maintain.
    if (userAge >= 18) {
        // Clear and understandable condition
    }
  • Avoid Deep Nesting: Deeply nested if statements can make code hard to read and maintain. Consider using else if or switch statements where appropriate.
    if (condition1) {
        // code
    } else if (condition2) {
        // code
    } else {
        // code
    }
  • Boolean Variables: Use boolean variables to make conditions more readable.
    boolean isAdult = userAge >= 18;
    if (isAdult) {
        // code
    }
  • Short-Circuit Evaluation: Leverage short-circuit evaluation to combine multiple conditions efficiently.
    if (condition1 && condition2) {
        // code
    }
  • Consistent Bracing: Always use braces {} even for single-line statements to avoid errors and improve readability.
    if (condition) {
        // code
    }
  • Avoid Complex Conditions: Break down complex conditions into simpler ones for better readability.
    boolean isEligible = (age > 18) && (hasLicense) && (isHealthy);
    if (isEligible) {
        // code
    }
  • Using Ternary Operator: For simple conditional assignments, consider using the ternary operator.
    int max = (a > b) ? a : b;
  • Nested if Statements: Use nested if statements judiciously, ensuring readability by keeping the depth minimal.
    if (condition1) {
        if (condition2) {
            // code
        }
    }