Java If Statement

Introduction

The if statement is one of the fundamental control flow statements in Java, allowing you to execute certain parts of your code based on specific conditions. In this chapter, we will explore different variations of the if statement, including if, if-else, if-else-if ladder, and nested if. Each type will be explained with examples and a text-based flow chart to illustrate the flow of control.

Java if Statement

The if statement executes a block of code if a specified condition is true.

Syntax

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

Example

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

Output:

Positive Number

Flow Chart

   Start
     |
  [condition]
    / \
   /   \
True  False
 /       \
Execute   Skip
Code     Code
 /         \
End        End

Java if-else Statement

The if-else statement executes one block of code if a condition is true and another block of code if it 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 number = -10;
        if (number > 0) {
            System.out.println("Positive Number");
        } else {
            System.out.println("Non-Positive Number");
        }
    }
}

Output:

Non-Positive Number

Flow Chart

   Start
     |
  [condition]
    / \
   /   \
True  False
 /       \
Execute   Execute
Code A    Code B
 /         \
End        End

Java if-else-if Ladder

The if-else-if ladder tests multiple conditions sequentially, executing the corresponding block of code for the first true condition.

Syntax

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

Example

public class IfElseIfExample {
    public static void main(String[] args) {
        int marks = 85;
        if (marks >= 90) {
            System.out.println("Grade A");
        } else if (marks >= 75) {
            System.out.println("Grade B");
        } else if (marks >= 50) {
            System.out.println("Grade C");
        } else {
            System.out.println("Grade D");
        }
    }
}

Output:

Grade B

Flow Chart

   Start
     |
  [condition1]
    / \
   /   \
True  False
 /       \
Code A  [condition2]
          / \
         /   \
       True False
       /      \
    Code B   [condition3]
              / \
             /   \
           True False
           /      \
        Code C    Code D
           \       /
            End

Java Nested if Statement

A nested if statement uses an if statement inside another if statement to test multiple conditions.

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 number = 10;
        if (number > 0) {
            if (number % 2 == 0) {
                System.out.println("Positive Even Number");
            }
        }
    }
}

Output:

Positive Even Number

Flow Chart

   Start
     |
  [condition1]
    / \
   /   \
True  False
 /       \
[condition2] Skip
  / \
 /   \
True False
 /     \
Code  Skip
 /       \
End      End

Conclusion

In this chapter, we covered the various forms of the if statement in Java, including if, if-else, if-else-if ladder, and nested if. These control flow statements are essential for making decisions in your programs based on conditions. In the next chapters, we will delve deeper into each type of control flow statement, providing more examples and best practices to help you master their use in your Java programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top