Java if...else Statement

In this tutorial, you will learn about control flow statements using Java if and if...else statements with the help of examples.

In programming, we use the if..else statement to run a block of code among more than one alternatives.

For example, assigning grades (A, B, C) based on the percentage obtained by a student.

  • if the percentage is above 90, assign grade A
  • if the percentage is above 75, assign grade B
  • if the percentage is above 65, assign grade C

1. Java if (if-then) Statement

The syntax of an if-then statement is:

if (condition) {
  // statements
}

Here, condition is a boolean expression such as age >= 18.

  • if condition evaluates to true , statements are executed
  • if condition evaluates to false , statements are skipped

Example 1: Java if Statement

class IfStatement {
  public static void main(String[] args) {

    int number = 10;

    // checks if number is less than 0
    if (number < 0) {
      System.out.println("The number is negative.");
    }

    System.out.println("Statement outside if block");
  }
}

Output:

Statement outside if block

2. Java if...else (if-then-else) Statement

The if statement executes a certain section of code if the test expression is evaluated to true. However, if the test expression is evaluated to false, it does nothing.

In this case, we can use an optional else block. Statements inside the body of else block are executed if the test expression is evaluated to false.

if (condition) {
  // codes in if block
}
else {
  // codes in else block
}

Example 2: Java if...else Statement

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

    // checks if number is greater than 0
    if (number > 0) {
      System.out.println("The number is positive.");
    }
    else {
      System.out.println("The number is not positive.");
    }

    System.out.println("Statement outside if...else block");
  }
}

Output:

The number is positive.
Statement outside if...else block

3. Java if...else...if Statement

In Java, we have an if...else...if ladder, that can be used to execute one block of code among multiple other blocks.

if (condition1) {
  // codes
}
else if(condition2) {
  // codes
}
else if (condition3) {
  // codes
}
else {
  // codes
}

Example 3: Java if...else...if Statement

class Main {
  public static void main(String[] args) {

    int number = 0;

    // checks if number is greater than 0
    if (number > 0) {
      System.out.println("The number is positive.");
    }
    // checks if number is less than 0
    else if (number < 0) {
      System.out.println("The number is negative.");
    }
    // if both condition is false
    else {
      System.out.println("The number is 0.");
    }
  }
}

4. Java Nested if..else Statement

In Java, it is also possible to use if..else statements inside an if...else statement. It's called the nested if...else statement.

if (condition1) {
    if (condition2) {
        // code
    }
}

5. Ternary Operator (Shorthand if...else)

There is also a shorthand called the ternary operator, which is known as the ? : operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements:

variable = (condition) ? expressionTrue :  expressionFalse;

Example:

int time = 20;
String result = (time < 18) ? "Good day." : "Good evening.";
System.out.println(result);

Key Takeaways

  • if: Executes code only if the condition is true.
  • else: Executes code if the if condition is false.
  • else if: Checks a new condition if the previous one was false.
  • Ternary Operator: A shorthand for if...else.

Common Pitfalls

[!WARNING] Assignment vs Comparison: if (a = 5) assigns 5 to a. if (a == 5) checks if a equals 5.

[!WARNING] Missing Braces: While single-line if statements work without { }, it's safer to always use them to avoid bugs when adding more lines later.

Challenge

Challenge

Task:

Write a program that checks if a number (use 10) is positive. If so, print 'Positive'. Else, print 'Not Positive'.