Java Academy Logo

Control Flow Statements

Master the building blocks of programming logic

What are Control Flow Statements?

Control flow statements are fundamental components of programming languages that allow developers to control the order in which instructions are executed in a program. They enable execution of code based on conditions or repeatedly until a condition is met.

Control Flow Overview

Conditional Statements in Programming

Make decisions and control program flow with conditions

Understanding Conditional Logic

Conditional statements allow a program to make decisions based on specific conditions. They determine which block of code should execute depending on whether a condition is true or false. Two of the most commonly used conditional structures are if-else and switch.

1

If Statement in Programming

IfElseExample.java
public class IfElseExample {
    public static void main(String[] args) {
        
        int score = 85;

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

How it works:

  • 1If score is 90 or above, print Grade: A.
  • 2Otherwise, if score is 80–89, print Grade: B.
  • 3Otherwise, if score is 70–79, print Grade: C.
  • 4Otherwise, print Grade: D.
2

Switch Statement in Programming

SwitchCase.java
public class SwitchCase {
    public static void main(String[] args) {
        
        String role = "admin";

        switch (role) {
            case "admin":
                System.out.println("Admin Access");
                break;
            case "user":
                System.out.println("User Access");
                break;
            case "guest":
                System.out.println("Guest Access");
                break;
            default:
                System.out.println("Unknown Role");
        }
    }
}

Step-by-step explanation

String role = "admin";

  • A variable named role is created.
  • It stores the text "admin".

switch (role)

  • Java compares the value of role with each case.

case "admin":

  • If role is "admin", print "Admin Access".
  • break stops checking other cases.

case "user":

  • If role was "user", print "User Access".

case "guest":

  • If role was "guest", print "Guest Access".

default:

  • Runs only if none of the cases match.
  • Prints "Unknown Role".

Looping Statements in Programming

Master iteration and repetition in your code

What are Looping Statements?

Looping statements, also known as iteration or repetition statements, are used in programming to repeatedly execute a block of code.

Here are simple and clear Java examples for:

for loop
while loop
do-while loop

Each example includes an explanation so it's easy to understand.

1

FOR LOOP

Code Example:

ForLoopExample.java
public class ForLoopExample {
    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {
            System.out.println("Number: " + i);
        }
    }
}

Explanation:

int i = 1 → loop starts at 1
i <= 5 → loop continues while i ≤ 5
i++ → increments i

Prints:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
2

WHILE LOOP

Code Example:

WhileLoopExample.java
public class WhileLoopExample {
    public static void main(String[] args) {

        int i = 1;

        while (i <= 5) {
            System.out.println("Count: " + i);
            i++;
        }
    }
}

Explanation:

int i = 1 → starting value
while (i <= 5) → condition must be true

Inside the loop:

  • prints the value
  • i++ increases i
When i becomes 6 → loop stops.

Prints:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
3

DO-WHILE LOOP

Code Example:

DoWhileExample.java
public class DoWhileExample {
    public static void main(String[] args) {

        int i = 1;

        do {
            System.out.println("Value: " + i);
            i++;
        } while (i <= 5);
    }
}

Explanation:

The do block always runs once.
After that, it checks (i <= 5).
If true → loop continues.

Prints:

Value: 1
Value: 2
Value: 3
Value: 4
Value: 5

Jump Statements in Programming

Master the control flow of your programs

Jump statements in programming are used to change the flow of control within a program. They allow the programmer to transfer program control to different parts of the code based on certain conditions or requirements. Here are common types of jump statements:

1

break Statement

The break statement immediately stops a loop (for, while, do-while).

⭐ Example (Stopping a loop early):

BreakExample.java
public class BreakExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i == 5) {
                break;   // loop stops when i becomes 5
            }
            System.out.println(i);
        }
    }
}

✔ What happens? Output:

1
2
3
4

As soon as i == 5, the break statement runs and stops the whole loop — it does NOT print 5, 6, 7, 8, 9, 10.

📌 When to use break?

When you found what you're looking for
When you want to exit a loop early
When a certain condition is met
2

continue Statement

The continue statement skips one loop iteration, but the loop continues running.

⭐ Example (Skip number 5):

ContinueExample.java
public class ContinueExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 7; i++) {
            if (i == 5) {
                continue;  // skip printing 5
            }
            System.out.println(i);
        }
    }
}

✔ What happens? Output:

1
2
3
4
6
7

When i == 5, the loop skips it but keeps running normally afterward.

📌 When to use continue?

Skip unwanted values
Skip invalid input
Skip specific conditions while still looping
3

return Statement

The return statement exits a method completely and optionally returns a value.

⭐ Example 1: return without value (void method):

ReturnExample.java
public class ReturnExample {
    public static void main(String[] args) {
        checkAge(15);
        checkAge(20);
    }
    
    static void checkAge(int age) {
        if (age < 18) {
            System.out.println("You are not allowed.");
            return;   // method stops here
        }
        System.out.println("You are allowed!");
    }
}

✔ Output:

You are not allowed.
You are allowed!

When age is < 18, the method stops immediately — nothing below return runs.

Example 2: return with a value:

ReturnValue.java
public class ReturnValue {
    public static void main(String[] args) {
        int result = addNumbers(5, 7);
        System.out.println("Sum = " + result);
    }
    
    static int addNumbers(int a, int b) {
        return a + b;   // sends back the result
    }
}

✔ Output:

Sum = 12

📌 When to use return?

To stop a method early
To send a result back
To end execution of a method immediately