The if-else statement in Java is a fundamental control flow construct that allows your program to make decisions based on whether a condition is true or false. It enables you to execute different blocks of code depending on the outcome of a boolean expression.

Basic Structure:

Java

if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}

Explanation:

  1. if (condition): The if keyword is followed by a condition enclosed in parentheses. This condition must be a boolean expression, meaning it evaluates to either true or false.

  2. { ... } (if block): The code within the curly braces immediately following the if condition is called the “if block.” This block of code is executed only if the condition evaluates to true.

  3. else { ... } (else block): The else keyword is optional. If present, it’s followed by another block of code enclosed in curly braces, called the “else block.” This block of code is executed only if the condition evaluates to false.

How it Works:

The if-else statement works as follows:

  1. The condition is evaluated.
  2. If the condition is true, the code in the if block is executed, and the code in the else block (if present) is skipped.
  3. If the condition is false, the code in the if block is skipped, and the code in the else block (if present) is executed.

Examples:

1. Simple if statement:

Java

int age = 16;

if (age >= 18) {
    System.out.println("You are an adult.");
}

In this example, the message “You are an adult” will only be printed if the value of the age variable is greater than or equal to 18. If the age is less than 18, nothing will be printed.

2. if-else statement:

Java

int temperature = 25;

if (temperature > 30) {
    System.out.println("It's hot!");
} else {
    System.out.println("It's a pleasant temperature.");
}

Here, if the temperature is greater than 30, “It’s hot!” is printed. Otherwise (if the temperature is 30 or less), “It’s a pleasant temperature.” is printed.

3. Nested if-else statements:

You can nest if-else statements within each other to create more complex decision-making logic:

Java

int score = 85;

if (score >= 70) {
    System.out.println("You passed.");
    if (score >= 90) {
        System.out.println("Excellent!");
    } else if (score >= 80) {
        System.out.println("Good job!");
    }
} else {
    System.out.println("You did not pass.");
}

In this nested example, the outer if checks if the score is passing (70 or above). If it is, then the inner if-else if checks for specific grade ranges (80-89 and 90+).

4. Using boolean variables:

You can directly use boolean variables in the condition:

Java

boolean isLoggedIn = true;

if (isLoggedIn) {
    System.out.println("Welcome!");
} else {
    System.out.println("Please log in.");
}

Important Considerations:

  • Curly Braces: While curly braces are technically optional for single-line if and else blocks, it’s highly recommended to always use them. This improves code readability and prevents errors, especially when adding more statements later.

  • Conditions: The condition in the if statement must be a boolean expression. You can use comparison operators (e.g., ==, !=, >, <, >=, <=) and logical operators (e.g., && (AND), || (OR), ! (NOT)) to create complex conditions.

  • else if: For checking multiple conditions in sequence, use the else if construct (as shown in the nested example) to avoid deeply nested if statements.

Related Posts