Java If, If-Else, Nested If, and If-Else-If Statements


Introduction

In Java, control flow statements are used to manage the flow of execution based on certain conditions. Among these, the if, if-else, nested if, and if-else-if statements are fundamental for making decisions in code. These statements allow the program to choose different paths of execution based on the evaluation of boolean expressions.

Table of Contents

  1. What are Control Flow Statements?
  2. Types of Control Flow Statements in Java
    • If Statement
    • If-Else Statement
    • Nested If Statement
    • If-Else-If Statement
  3. Examples of Each Control Flow Statement
  4. Conclusion

What are Control Flow Statements?

Control flow statements in Java determine the order in which statements are executed. These statements enable the program to make decisions, repeat tasks, and branch into different execution paths. The if, if-else, nested if, and if-else-if statements are used to evaluate conditions and execute specific blocks of code based on whether the conditions are true or false.

Types of Control Flow Statements in Java

Java provides several types of control flow statements:

  1. If Statement
  2. If-Else Statement
  3. Nested If Statement
  4. If-Else-If Statement

1. If Statement

The if statement is used to test a condition. If the condition evaluates to true, the block of code inside the if statement is executed.

Syntax:

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

Example:

public class IfExample {
    public static void main(String[] args) {
        int num = 10;
        if (num > 0) {
            System.out.println("The number is positive.");
        }
    }
}

2. If-Else Statement

The if-else statement is used to execute one block of code if the condition is true and another block of code if the condition is false.

Syntax:

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

Example:

public class IfElseExample {
    public static void main(String[] args) {
        int num = -10;
        if (num > 0) {
            System.out.println("The number is positive.");
        } else {
            System.out.println("The number is negative.");
        }
    }
}

3. Nested If Statement

A nested if statement is an if statement inside another if statement. This allows for more complex conditions to be tested.

Syntax:

if (condition1) {
    // code to be executed if condition1 is true
    if (condition2) {
        // code to be executed if condition2 is true
    }
}

Example:

public class NestedIfExample {
    public static void main(String[] args) {
        int num = 5;
        if (num > 0) {
            if (num < 10) {
                System.out.println("The number is a single digit positive number.");
            }
        }
    }
}

4. If-Else-If Statement

The if-else-if statement is used to test multiple conditions. It executes a block of code corresponding to the first true condition. If none of the conditions is true, the else block is executed.

Syntax:

if (condition1) {
    // code to be executed if condition1 is true
} else if (condition2) {
    // code to be executed if condition2 is true
} else {
    // code to be executed if all conditions are false
}

Example:

public class IfElseIfExample {
    public static void main(String[] args) {
        int num = 0;
        if (num > 0) {
            System.out.println("The number is positive.");
        } else if (num < 0) {
            System.out.println("The number is negative.");
        } else {
            System.out.println("The number is zero.");
        }
    }
}

Examples of Each Control Flow Statement

If Statement

The if statement checks a condition and executes a block of code if the condition is true.

public class SimpleIfExample {
    public static void main(String[] args) {
        int age = 18;
        if (age >= 18) {
            System.out.println("You are eligible to vote.");
        }
    }
}

Explanation: If the variable age is 18 or greater, the message "You are eligible to vote." is printed.

If-Else Statement

The if-else statement executes one block of code if the condition is true and another block if the condition is false.

public class IfElseExample {
    public static void main(String[] args) {
        int number = -5;
        if (number > 0) {
            System.out.println("The number is positive.");
        } else {
            System.out.println("The number is negative.");
        }
    }
}

Explanation: If the variable number is greater than 0, it prints "The number is positive."; otherwise, it prints "The number is negative."

Nested If Statement

A nested if statement is an if statement inside another if statement, allowing for more complex conditions.

public class NestedIfExample {
    public static void main(String[] args) {
        int number = 25;
        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.");
            }
        }
    }
}

Explanation: If number is greater than 0, it checks if number is even or odd and prints the appropriate message.

If-Else-If Statement

The if-else-if statement tests multiple conditions and executes corresponding blocks of code for the first true condition.

public class IfElseIfExample {
    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");
        }
    }
}

Explanation: Depending on the value of marks, it prints the corresponding grade. If marks is 85, it prints "Grade B" as it meets the condition marks >= 80.

Conclusion

Control flow statements like if, if-else, nested if, and if-else-if are essential for making decisions in Java programs. They allow the program to execute different blocks of code based on various conditions, making the code more dynamic and responsive to different inputs. Understanding and using these statements effectively is crucial for writing robust and efficient Java applications.


Comments