🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

if…else if Statement

Java If-Else If Statement Example

The if...else if statement in Java allows you to handle multiple conditions in a sequence. It's an extension of the basic if...else statement and is useful when you have more than two possible outcomes based on the evaluation of different conditions.

Example:

public class IfElseIfStatementExample {
    public static void main(String[] args) {
        int num = 0;

        if (num > 0) {
            System.out.println("Number is positive");
        } else if (num < 0) {
            System.out.println("Number is negative");
        } else {
            System.out.println("Number is zero");
        }
    }
}

In this example:

  • The if statement checks if num is greater than 0 and prints a message if true.
  • The else if statement checks if num is less than 0 and prints a different message if true.
  • The else block is executed only if both the if and else if conditions are false.

You can have multiple else if blocks to check additional conditions. The if...else if statement provides a structured way to handle multiple conditions without nesting multiple if...else statements, improving code readability and maintainability.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.