🔄

Java Loop Control Keywords

Sep 11, 2025

Overview

This lecture covers the break and continue keywords in Java, focusing on how they control the flow of loops. The notes include clear explanations, step-by-step code examples, and important points about variable scope. These notes are designed for a college-level course and are organized for easy reference, with ADHD-friendly formatting and thorough explanations so you can learn everything without needing to read the original material.


The break Keyword

  • The break keyword is used inside loops to exit the loop immediately, no matter what the loop’s condition is.
  • It is especially useful for stopping infinite loops or exiting early when a specific condition is met.
  • When break is executed, the program jumps to the first statement after the loop.

How break works:

  • You can use break in any loop (for, while, or do-while).
  • Once break is reached, the rest of the loop body is skipped, and the program continues after the loop.

Example 1: Exiting a for loop early

for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break; // Exit the loop when i is 5
    }
    System.out.print(i + " ");
}
// Output: 1 2 3 4
  • The loop prints numbers 1 to 4. When i becomes 5, break stops the loop.

Example 2: Exiting an infinite while loop

while (true) {
    int n = scanner.nextInt();
    if (n == 0) {
        break; // Exit the loop if user enters 0
    }
    // Process n
}
  • Even though the loop is infinite (while (true)), break allows you to exit when a certain condition is met.

Example 3: Using break after valid input

Scanner scanner = new Scanner(System.in);
int n;
while (true) {
    System.out.print("Enter a number between 1 and 10: ");
    n = scanner.nextInt();
    if (n >= 1 && n <= 10) {
        break; // Exit loop if input is valid
    }
    // If not valid, loop continues
}
System.out.println("You entered: " + n);
  • The loop keeps asking until the user enters a valid number, then exits.

The continue Keyword

  • The continue keyword skips the rest of the current loop iteration and moves to the next iteration.
  • In a for loop, after continue is executed, the update statement runs, then the loop condition is checked.
  • In while or do-while loops, continue causes the loop condition to be checked again immediately.

How continue works:

  • Use continue when you want to skip certain cases in a loop but keep looping.

Example 1: Skipping even numbers in a for loop

for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0) {
        continue; // Skip even numbers
    }
    System.out.print(i + " ");
}
// Output: 1 3 5 7 9
  • If i is even, continue skips the print statement, so only odd numbers are printed.

Example 2: Using continue in a while loop

int i = 0;
while (i < 5) {
    i++;
    if (i == 3) {
        continue; // Skip printing when i is 3
    }
    System.out.print(i + " ");
}
// Output: 1 2 4 5
  • When i is 3, the print statement is skipped.

Example 3: Repeatedly asking for valid input

Scanner scanner = new Scanner(System.in);
int n;
while (true) {
    System.out.print("Enter a number between 1 and 10: ");
    n = scanner.nextInt();
    if (n < 1 || n > 10) {
        continue; // Ask again if input is invalid
    }
    System.out.println("n is between 1 and 10");
    break; // Exit loop if input is valid
}
  • The loop keeps asking until the user enters a valid number.

Example: Printing Odd Numbers with continue

  • A for loop from 1 to 10 prints only odd numbers by skipping even numbers using continue.
  • If the current number is even, continue skips the print statement.

Step-by-step code:

for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0) {
        continue; // Skip even numbers
    }
    System.out.print(i + " ");
}
// Output: 1 3 5 7 9

How it works:

  • Start with i = 1. If i is odd, print it.
  • If i is even, skip printing and move to the next number.
  • This continues until i is greater than 10.

Example: Validating User Input with break and continue

  • An infinite while (true) loop keeps asking the user to enter a number between 1 and 10.
  • If the input is outside 1 to 10, continue restarts the loop to ask again.
  • If the input is valid, break exits the loop.
  • Declaring the input variable outside the loop allows its value to be used after breaking out.

Step-by-step code:

Scanner scanner = new Scanner(System.in);
int n;
while (true) {
    System.out.print("Enter a number between 1 and 10: ");
    n = scanner.nextInt();
    if (n < 1 || n > 10) {
        continue; // Ask again if input is invalid
    }
    System.out.println("n is between 1 and 10");
    break; // Exit loop if input is valid
}
// n can be used here
System.out.println("You entered: " + n);

How it works:

  • The loop keeps asking until the user enters a valid number.
  • Only then does it print confirmation and exit.

Variable Scope in Loops

  • A variable declared inside a loop is not accessible outside the loop.
  • To use a variable after a loop (for example, after using break), declare it outside the loop.

Example 1: Variable declared outside the loop

int n; // Declared outside the loop
while (true) {
    n = scanner.nextInt();
    if (n == 5) {
        break;
    }
}
// n can be used here
System.out.println("Final value: " + n);

Example 2: Variable declared inside the loop

while (true) {
    int m = scanner.nextInt();
    if (m == 5) {
        break;
    }
}
// m cannot be used here (compilation error)
  • Declaring the variable outside the loop makes it accessible after the loop ends.

Tip:

  • If you need to use a value after the loop, always declare the variable before the loop starts.

Key Terms & Definitions

  • break — Keyword to immediately exit a loop’s execution.
  • continue — Keyword to skip the rest of the loop body and go to the next iteration.
  • loop — A programming construct for repeating code, such as for, while, or do-while loops.
  • variable scope — The region in code where a variable can be accessed.

Action Items / Next Steps

  • Practice using break and continue with different types of loops in Java.
  • Try writing loops that use both keywords to control flow.
  • Experiment with variable scope by declaring variables inside and outside loops.
  • Review loop structures (for, while, do-while) and see how break and continue affect their execution.
  • If you get stuck, break the problem into small steps and focus on one part at a time. Take breaks if you need to refocus!

Quick Reference: When to Use break and continue

  • Use break when you want to exit a loop completely, even if the loop condition is still true.
  • Use continue when you want to skip the rest of the current iteration and move to the next one.
  • Always declare variables outside the loop if you need to use them after the loop ends.

ADHD-Friendly Tips

  • Focus on one example at a time—read the code, then the explanation.
  • Use the code examples as templates for your own practice.
  • Highlight or underline key words like break, continue, and variable names to help them stand out.
  • Take short breaks between sections to help with focus and retention.
  • If you feel overwhelmed, revisit the "Key Terms & Definitions" section for a quick refresher.