If else in Java

If-else statement is a conditional statement in Java that allows you to execute a block of code based on a specified condition. It enables your program to make decisions and choose different paths of execution based on whether a condition is true or false.

Syntax

The syntax of the if-else statement in Java is as follows:

    
if (condition) {
    // code to be executed if the condition is true
} else {
    // code to be executed if the condition is false
}

Example

Let's consider a simple example to demonstrate the usage of the if-else statement. Suppose we want to check if a given number is positive or negative. Here's how we can do it using an if-else statement.

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

        if (number > 0) {
            System.out.println("The number is positive.");
        } else {
            System.out.println("The number is negative.");
        }
    }
}

In the above example, we first declare and initialize a variable named "number" with a value of 10. We then use the if-else statement to check if the number is greater than 0. If the condition is true, the program will print "The number is positive." Otherwise, if the condition is false, it will print "The number is negative."

if-else-if Ladder

The if-else-if ladder allows you to test multiple conditions in a sequential manner. It starts with an initial if statement, followed by one or more else-if statements, and ends with an optional else statement. Here's an example:

    
public class IfElseIfLadder {
    public static void main(String[] args) {
        int marks = 85;

        if (marks >= 90) {
            System.out.println("Grade: A");
        } else if (marks >= 80) {
            System.out.println("Grade: B");
        } else if (marks >= 70) {
            System.out.println("Grade: C");
        } else if (marks >= 60) {
            System.out.println("Grade: D");
        } else {
            System.out.println("Grade: F");
        }
    }
}

In the above example, we assign a value to the variable "marks" and use an if-else-if ladder to determine the corresponding grade based on the marks. The program outputs the appropriate grade depending on the value of "marks."

Nested if-else Statements

Nested if-else statements allow you to have if-else statements inside other if-else statements. This construct is useful when you need to evaluate multiple conditions within each other. Here's an example:

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

        if (number > 0) {
            if (number % 2 == 0) {
                System.out.println("The number is positive and even.");
            } else {
                System.out.println("The number is positive and odd.");
            }
        } else {
            System.out.println("The number is either zero or negative.");
        }
    }
}

In the above example, we check if the number is positive or negative using the outer if-else statement. If it is positive, we further check if it is even or odd using the nested if-else statement. The program then prints the appropriate message based on the conditions.