You are currently viewing Java if Statement
java_logo

Java if Statement

In the realm of Java programming, the if statement stands as a pivotal tool for controlling the flow of your application. At its core, a Java statement is a complete unit of execution terminated with a semicolon, and the if statement empowers developers to introduce decision-making capabilities into their code.

1. The Anatomy of a Java if statement

In the landscape of programming, decision-making is a critical aspect that determines the course of execution based on specific conditions. The basic structure of an if statement is as follows:

if (condition) {
    // Code to be executed if the condition is true
}
  • if: Keyword indicating the beginning of the if statement.
  • (condition): Parentheses containing the Boolean expression that determines whether the code inside the block should be executed. The parentheses are required.
  • {}: Enclosed block containing the code to be executed if the condition is true. If there’s only one statement, the curly braces are optional.

examples without curly braces:

int hourOfDay = 14;
int afternoonGreetings = 0;

if (hourOfDay >= 12)
    System.out.println("Good Afternoon!"); // Greet with "Good Afternoon";
afternoonGreetings++;
System.out.println(afternoonGreetings);

In this example, without curly braces, the if statement applies only to the immediately following statement (System.out.println("Good Afternoon!");). The line afternoonGreetings++; and System.out.println(afternoonGreetings); are not part of the if block. Regardless of the condition, these lines will be executed. The output will be:

Good Afternoon!
1

In case the hourOfDay is 10 , without curly braces, the if condition (hourOfDay >= 12) is false. However, the statements following the if block (afternoonGreetings++; and System.out.println(afternoonGreetings);) are not part of the if block. Therefore, they will execute regardless of the condition. The output will be:

1

examples with curly braces:

int hourOfDay2 = 10;
int afternoonGreetings2 = 0;

if (hourOfDay2 >= 12) {
    System.out.println("Good Afternoon!"); // Greet with "Good Afternoon";
    afternoonGreetings2++;
    System.out.println(afternoonGreetings2);
}

In this case, with curly braces, the entire block of code following the if statement is part of the if block. Since the condition (hourOfDay2 >= 12) is false, the statements inside the curly braces will not execute. The output will be nothing, as the statements within the curly braces are skipped when the condition is false.

In case the hourOfDay would have been 14, the entire block of code following the if statement is part of the if block. So, if the condition is true (hourOfDay2 >= 12), all the statements within the curly braces will be executed. The output is:

Good Afternoon!
1

2. The Anatomy of a Java else statement

The else statement pairs seamlessly with the if statement, creating a duet of conditional logic. Together, they allow your code to gracefully navigate through various scenarios. Let’s dissect the structure of an if-else statement:

if (condition) {
    // Code to be executed if the condition is true
} else {
    // Code to be executed if the condition is false
}
  • if: Keyword indicating the beginning of the if statement.
  • (condition): Parentheses containing the Boolean expression that determines whether the code inside the first block should be executed.
  • {}: Enclosed block containing the code to be executed if the condition is true. If there’s only one statement, the curly braces are optional.
  • else: Keyword indicating the beginning of the else statement.
  • {}: Enclosed block containing the code to be executed if the condition in the if statement is false. If there’s only one statement, the curly braces are optional.

examples:

int hoursOfDay = 12;

if (hoursOfDay >= 12) {
    System.out.println("Good Afternoon!"); 
} else {
    System.out.println("Good Morning!"); 
}

In this example, if hoursOfDay is 12 or later, it prints “Good Afternoon!”; otherwise, it prints “Good Morning!”

We can append additional if-statements with the else if keyword:

int hoursOfDay = 18;

if (hoursOfDay >= 12 && hoursOfDay < 17) {
    System.out.println("Good Afternoon!"); 
} else if (hoursOfDay >= 17 && hoursOfDay < 20) {
    System.out.println("Good Evening!"); 
} else {
    System.out.println("Good Morning!"); 
}
  • If hoursOfDay is between 12 and 5 PM, it prints “Good Afternoon!”
  • If hoursOfDay is between 5 and 8 PM, it prints “Good Evening!”
  • Otherwise, it prints “Good Morning!”

Feel free to adjust the value of hoursOfDay to see how the program greets based on different times of the day.

watch out for unreachable code. unreachable code will compile, but the code will never be reached

    int hoursOfDay2 = 10;

        if (hoursOfDay2 < 17) {
            System.out.println("Good Afternoon!");
        } else if (hoursOfDay2 < 12) {
            System.out.println("Good Morning!"); // Unreachable statement
        }

The else if statement is unreachable because the first condition (hoursOfDay2 < 17) is always true when the second condition (hoursOfDay2 < 12) is true. Due to the order of the conditions, the second statement can never be reached. To make the code logically correct, you should reverse the order of conditions:

int hoursOfDay2 = 10;

if (hoursOfDay2 < 12) {
    System.out.println("Good Morning!"); 
} else if (hoursOfDay2 < 17) {
    System.out.println("Good Afternoon!"); 
}

Now, the else if block becomes reachable, and the output will be “Good Morning!” since the first condition is satisfied. The order of conditions in if-else if-else statements is crucial, and arranging them correctly ensures that the code behaves as intended.