Java if-else statement with examples

Conditional statements, which let you control the flow of your program based on specific conditions, are one of the most basic programming concepts.

In Java, there are three types of conditional statements, which are

These statements serve as the foundation for the majority of decision-making logic in Java, allowing you to execute different blocks of code based on various conditions. They are required for developing robust and efficient programs capable of handling a wide range of inputs and scenarios.

One more thing is that the "else if" and "else" statements are always used with the "if" statement.

We'll look at these conditional statements in Java and how you can use them in your own programs in this article. Writing effective Java code requires a solid understanding of conditional statements, regardless of your level of programming experience. So let's get started!

The "if" statement in Java

The "if" statement determines whether a given condition is true and, if so, executes a block of code. The syntax of an "if" statement is as follows:

if (condition) {
    // block of code to execute if "condition" is true
}

For example:

Java Code
public class Example {
  public static void main(String[] args) {
    int x = 10;

    if (x > 5) {
      System.out.println("The value of 'x' is greater than 5.");
    }
  }
}
Output
The value of 'x' is greater than 5.

Since the condition "x > 5" (10 > 5) evaluates to true, the program flow enters the body of the "if" statement and executes the "System.out.println()" statement, which prints the text: "The value of 'x' is greater than 5."

The "else if" statement in Java

You can use the "else if" statement to test for multiple conditions. If the first condition is not met, it checks the next condition and, if met, executes the block of code associated with it. The syntax of an "else if" statement is as follows:

if (condition1) {
    // block of code to execute if "condition1" is true
    
} else if (condition2) {
    // block of code to execute if "condition2" is true
    
} else if (condition3) {
    // block of code to execute if "condition3" is true
    
} else if (condition4) {
    // block of code to execute if "condition4" is true
    
}
:
else {
    // block of code to execute if none of the above conditions are true
}

For example:

Java Code
public class Example {
   public static void main(String[] args) {
      int x = 10;

      if (x < 0) {
         System.out.println("The value of 'x' is negative");
      } else if (x == 0) {
         System.out.println("The value of 'x' is zero");
      } else {
         System.out.println("The value of 'x' is positive");
      }
   }
}
Output
The value of 'x' is positive

Since the conditions "x < 0" and "x == 0" both evaluate to false, the block of code associated with "else" was executed.

The "else" statement in Java

The "else" statement is used to execute a block of code if none of the conditions in the "if" or "else if" statements are true. An "else" statement looks like this:

if (condition) {
    // block of code to execute if "condition" is true
} else {
    // block of code to execute if "condition" is false
}

For example:

Java Code
public class Example {
  public static void main(String[] args) {
    int x = 10;

    if (x < 5) {
      System.out.println("The value of 'x' is less than 5.");
    } else {
      System.out.println("The value of 'x' is greater than 5.");
    }
  }
}
Output
The value of 'x' is greater than 5.

Java if-else statement example

Java Code
public class Example {
  public static void main(String[] args) {
    int x = 10;

    if (x < 0) {
      System.out.println("The value of 'x' is negative.");
    } else if (x == 0) {
      System.out.println("The value of 'x' is zero.");
    } else if (x > 0 && x < 10) {
      System.out.println("The value of 'x' is a single-digit positive number.");
    } else if (x >= 10 && x < 100) {
      System.out.println("The value of 'x' is a two-digit positive number.");
    } else {
      System.out.println("The value of 'x' is a positive number with three or more digits.");
    }
  }
}
Output
The value of 'x' is a two-digit positive number.

Java Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!