Java if Statement

The if statement in Java is one of the most fundamental control flow structures used to make decisions in a program. It allows a program to execute a block of code only when a specified condition is true. This makes your application dynamic and responsive to different inputs or situations.

The condition inside an if statement is always a boolean expression—an expression that evaluates to either true or false. If the condition evaluates to true, the block of code inside the if statement runs; if it evaluates to false, the program skips that block and continues execution with the next statement.

Syntax of the if Statement

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

Here, condition is a boolean expression (for example, x > 0, age >= 18, or isValid == true).

If the condition evaluates to true, the statements inside the curly braces {} are executed.

If the condition is false, Java skips the block and moves on to the next line of code.

Example 1: Checking a Positive Number

public class PositiveCheck {
  public static void main(String[] args) {
    int number = 10;

    if (number > 0) {
      System.out.println(“The number is positive.”);
    }
  }
}

Output:
The number is positive.

Explanation:
Here, the condition number > 0 evaluates to true, so the message inside the if block is printed. If the number were negative or zero, the block would not execute.

Example 2: Checking User Eligibility

public class VotingEligibility {
  public static void main(String[] args) {
    int age = 18;

    if (age >= 18) {
      System.out.println(“You are eligible to vote.”);
    }
  }
}

Output:
You are eligible to vote.

Explanation:
The if statement checks if the person’s age is greater than or equal to 18. Since the condition is true, the program prints the eligibility message.

Example 3: Using Logical Operators in if

public class LoginCheck {
  public static void main(String[] args) {
    String username = “admin”;
    String password = “12345”;

    if (username.equals(“admin”) && password.equals(“12345”)) {
      System.out.println(“Login successful!”);
    }
  }
}

Output:
Login successful!

Explanation:
Here, the if condition uses the logical AND (&&) operator. The code inside the block executes only if both conditions — username and password — match the expected values.

Single-Line if Statement

When there is only one statement to execute, you can omit the curly braces {} for simplicity:

if (5 > 2) System.out.println(“5 is greater than 2”);

Output:
5 is greater than 2

However, for readability and best practices, especially in production-level code, it’s recommended to always use braces, even for single statements.

Key Points to Remember

  • The condition in an if statement must always return a boolean value (true or false).
  • Curly braces {} are optional for single-line statements but recommended for clarity.
  • You can combine multiple conditions using logical operators (&&, ||, !).
  • If you need multiple conditions, consider using if-else or if-else if-else structures.

Nested if Statement

Sometimes, you might want to check another condition inside an existing if block. This is called nested if.

Example:

public class NestedIfExample {
  public static void main(String[] args) {
    int number = 25;

    if (number > 0) {
      if (number % 5 == 0) {
        System.out.println(“The number is positive and divisible by 5.”);
      }
    }
  }
}

Output:
The number is positive and divisible by 5.

Explanation:
Both conditions must be true for the message to print. The first if checks whether the number is positive, and the nested if checks divisibility by 5.

Conclusion

The if statement is the cornerstone of decision-making in Java. It gives programs the ability to react dynamically to changing data and user input. Whether you’re checking user credentials, validating input, or controlling program flow, mastering the if statement is essential for writing effective and logical Java code.

Scroll to Top