Java Control Flow Statements

Introduction

Control flow statements are fundamental constructs in Java that determine the order in which statements and instructions are executed. These statements enable you to implement decision-making, looping, and branching in your programs, allowing your code to respond dynamically to different conditions and inputs. In this chapter, we will provide an overview of control flow statements in Java, setting the stage for more detailed discussions in subsequent chapters.

Types of Control Flow Statements

Java provides several control flow statements that can be broadly categorized into three types:

  1. Decision-Making Statements
  2. Looping Statements
  3. Branching Statements

1. Decision-Making Statements

Decision-making statements allow your program to choose between different paths of execution based on certain conditions. These include:

if Statement

The if statement executes a block of code if a specified 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

You will learn more about the if statement in the next chapter.

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.

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

You will learn more about the if-else statement in the next chapter.

if-else-if Ladder

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

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 {
            System.out.println("Grade C");
        }
    }
}

Output:

Grade B

You will learn more about the if-else-if ladder in the next chapter.

Nested if Statement

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

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

You will learn more about nested if statements in the next chapter.

switch Statement

The switch statement allows selection among multiple options based on the value of an expression.

Example:

public class SwitchExample {
    public static void main(String[] args) {
        int day = 3;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            default:
                System.out.println("Invalid day");
                break;
        }
    }
}

Output:

Wednesday

You will learn more about the switch statement in the next chapter.

2. Looping Statements

Looping statements enable repetitive execution of a block of code as long as a specified condition remains true. These include:

while Loop

The while loop repeats a block of code while a condition is true.

Example:

public class WhileExample {
    public static void main(String[] args) {
        int count = 0;
        while (count < 3) {
            System.out.println("Count: " + count);
            count++;
        }
    }
}

Output:

Count: 0
Count: 1
Count: 2

You will learn more about the while loop in the next chapter.

do-while Loop

The do-while loop ensures the block of code is executed at least once before checking the condition.

Example:

public class DoWhileExample {
    public static void main(String[] args) {
        int count = 0;
        do {
            System.out.println("Count: " + count);
            count++;
        } while (count < 3);
    }
}

Output:

Count: 0
Count: 1
Count: 2

You will learn more about the do-while loop in the next chapter.

for Loop

The for loop repeats a block of code a specified number of times, with initialization, condition, and increment/decrement all in one place.

Example:

public class ForExample {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println("Iteration: " + i);
        }
    }
}

Output:

Iteration: 0
Iteration: 1
Iteration: 2

You will learn more about the for loop in the next chapter.

for-each Loop

The for-each loop is specifically designed to iterate over arrays and collections, simplifying the syntax for iterating over elements.

Example:

public class ForEachExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3};
        for (int num : numbers) {
            System.out.println("Number: " + num);
        }
    }
}

Output:

Number: 1
Number: 2
Number: 3

You will learn more about the for-each loop in the next chapter.

3. Branching Statements

Branching statements alter the flow of execution by transferring control to another part of the program. These include:

break Statement

The break statement exits a loop or switch statement prematurely.

Example:

public class BreakExample {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 3) {
                break;
            }
            System.out.println("Iteration: " + i);
        }
    }
}

Output:

Iteration: 0
Iteration: 1
Iteration: 2

You will learn more about the break statement in the next chapter.

continue Statement

The continue statement skips the current iteration of a loop and proceeds with the next iteration.

Example:

public class ContinueExample {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 3) {
                continue;
            }
            System.out.println("Iteration: " + i);
        }
    }
}

Output:

Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 4

You will learn more about the continue statement in the next chapter.

return Statement

The return statement exits from the current method and optionally returns a value.

Example:

public class ReturnExample {
    public static void main(String[] args) {
        System.out.println("Sum: " + add(10, 20));
    }

    public static int add(int a, int b) {
        return a + b;
    }
}

Output:

Sum: 30

You will learn more about the return statement in the next chapter.

Conclusion

Control flow statements are the building blocks for writing dynamic and responsive Java programs. They enable you to control the execution flow based on conditions, repeat actions, and manage complex logic. In the subsequent chapters, we will explore each type of control flow statement in detail, providing 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