←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Control Statements: Branching, Looping, Jumping & Labeled Statements

Study material and explanations for Unit 1

Control Statements: Branching, Looping, Jumping & Labeled Statements

1. What are Control Statements?

By default, the JVM executes statements sequentially from top to bottom. Control statements allow you to alter this normal flow of execution using decision-making, repetition, and unconditional jumps.

In Java, control statements are classified into 3 main categories:

Figure 1.8: Classification of Control Statements in Java
1. Branching (Decision Making)
if, if-else, if-else-if ladder, nested if, switch-case
2. Looping (Iteration)
for loop, enhanced for-each loop, while loop, do-while loop
3. Jumping (Transfer of Control)
break, continue, return, and Labeled break / continue

2. Branching (Decision-Making) Statements

Branching statements evaluate a condition and choose which block of code to execute.

A. The if-else Family

  1. Simple if: Executes a block only if the boolean condition is true.
  2. if-else: Executes the if block when true, otherwise executes the else block.
  3. if-else-if Ladder: Tests multiple conditions sequentially from top to bottom (ideal for grading systems).
  4. Nested if: An if statement inside another if statement.
int marks = 78;
if (marks >= 75) {
    System.out.println("Distinction");
} else if (marks >= 60) {
    System.out.println("First Division");
} else if (marks >= 40) {
    System.out.println("Pass");
} else {
    System.out.println("Supplementary");
}

B. The switch Statement

A switch statement is a cleaner multi-way branch statement that compares the value of a single expression against multiple case constants.

  • Data types allowed in Java switch: byte, short, int, char, String (added in Java 7), and enum. (Note: float, double, long, and boolean are NOT allowed in switch).
  • Why use break inside switch? Without break, execution “falls through” to the next case.
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");
}

3. Looping (Iterative) Statements

Loops allow a block of code to execute repeatedly as long as a specified boolean condition remains true.

1. for Loop (Entry-Controlled)

Used when the exact number of iterations is known in advance. It combines initialization, condition testing, and increment/decrement in a single line.

// Prints numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
    System.out.print(i + " ");
}

2. Enhanced for Loop (for-each Loop)

Introduced in Java 5 specifically for traversing Arrays and Collections without needing an index variable.

int[] marks = {85, 90, 78, 92};
for (int m : marks) {
    System.out.println("Score: " + m);
}

3. while Loop (Entry-Controlled)

Used when the number of iterations is not known beforehand. The condition is checked before entering the loop body. If the condition is initially false, the body executes 0 times.

int n = 123, reverse = 0;
while (n > 0) {
    int digit = n % 10;
    reverse = (reverse * 10) + digit;
    n = n / 10;
}
System.out.println("Reversed Number = " + reverse); // Output: 321

4. do-while Loop (Exit-Controlled)

The loop body executes first, and the condition is checked at the end of each iteration. Therefore, a do-while loop is guaranteed to execute at least once, even if the condition is false.

int count = 10;
do {
    System.out.println("Executes at least once! count = " + count);
    count++;
} while (count < 5); // Condition is false, yet printed once!

Comparison: while vs do-while Loop

Basis while Loop do-while Loop
Type of Loop Entry-Controlled Loop (condition checked at the top). Exit-Controlled Loop (condition checked at the bottom).
Minimum Executions 0 times (if condition is initially false). At least 1 time (even if condition is initially false).
Semicolon at End No semicolon after while(condition). Ends with a mandatory semicolon: while(condition);.
Best Use Case Reading a file or processing digits of a number. Displaying a menu-driven program to the user at least once.

4. Jumping Statements (break, continue, return)

Jumping statements transfer control unconditionally from one part of the program to another.

  1. break Statement: Immediately terminates the innermost switch or loop (for, while, do-while) and transfers control to the statement immediately following the loop.
  2. continue Statement: Skips the remaining statements in the current iteration of the loop and jumps directly to the next iteration (increment step in for or condition check in while).
  3. return Statement: Explicitly exits from the current method and returns control (along with an optional return value) back to the caller of the method.
for (int i = 1; i <= 6; i++) {
    if (i == 3) continue; // Skips printing 3
    if (i == 5) break;    // Stops the loop completely when i reaches 5
    System.out.print(i + " ");
}
// Output: 1 2 4

5. Labeled Statements in Java (Java’s Safe Alternative to goto)

C and C++ have the goto statement, which can jump anywhere in a program and create unreadable “spaghetti code”. Java banned goto and replaced it with Labeled Statements (labeled break and labeled continue).

Why Do We Need Labeled Statements?

Normally, an unlabeled break or continue only affects the innermost loop in which it is written. If you have nested loops (a loop inside another loop) and want to break out of the outer loop directly from inside the inner loop, you attach a Label (labelName:) to the outer loop!

Syntax & Complete Example of Labeled break and continue

public class LabeledLoopDemo {
    public static void main(String[] args) {

        // 'outerLoop' is a Label attached to the outer for-loop
        outerLoop:
        for (int row = 1; row <= 3; row++) {
            for (int col = 1; col <= 3; col++) {

                if (row == 2 && col == 2) {
                    System.out.println("\nBreaking out of BOTH loops at row=2, col=2!");
                    break outerLoop; // Exits the outerLoop completely!
                }
                System.out.print("(" + row + "," + col + ") ");
            }
            System.out.println();
        }
        System.out.println("Program continues after outerLoop.");
    }
}

Output:

(1,1) (1,2) (1,3) 
(2,1) 
Breaking out of BOTH loops at row=2, col=2!
Program continues after outerLoop.

Notice the difference:

  • If we had written normal break;, it would only exit the col loop and continue with row = 3.
  • Because we wrote break outerLoop;, Java immediately terminated both the inner and outer loops!
  • Similarly, continue outerLoop; skips the rest of the inner loop and jumps directly to the next iteration of outerLoop.

💡 Exam Preparation Tips for DAVV BCA

  • “What are Labeled Statements in Java? Explain with an example” is explicitly mentioned in your DAVV Unit 1 syllabus and is frequently asked as a 5-mark question. Make sure to write the nested outerLoop: code snippet above!
  • Always include the comparison table between while and do-while and between break and continue.