Develop programs to demonstrate use of if statements and its different forms in Java

If statements are one of the most basic and important control flow statements in Java. They allow us to execute different code blocks based on the evaluation of a condition.

Code:

public class IfStatementDemo {
    public static void main(String[] args) {
        int x = 10;
        int y = 20;

        // Example 1: Simple if statement
        if (x < y) {
            System.out.println("x is less than y");
        }

        // Example 2: if-else statement
        if (x > y) {
            System.out.println("x is greater than y");
        } else {
            System.out.println("x is not greater than y");
        }

        // Example 3: if-else if-else statement
        if (x > y) {
            System.out.println("x is greater than y");
        } else if (x < y) {
            System.out.println("x is less than y");
        } else {
            System.out.println("x is equal to y");
        }
    }
}

Output: 

x is less than y
x is not greater than y
x is less than y


Comments