Sep 11, 2025
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.
break
Keywordbreak
keyword is used inside loops to exit the loop immediately, no matter what the loop’s condition is.break
is executed, the program jumps to the first statement after the loop.How break
works:
break
in any loop (for
, while
, or do-while
).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
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
}
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);
continue
Keywordcontinue
keyword skips the rest of the current loop iteration and moves to the next iteration.for
loop, after continue
is executed, the update statement runs, then the loop condition is checked.while
or do-while
loops, continue
causes the loop condition to be checked again immediately.How continue
works:
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
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
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
}
continue
for
loop from 1 to 10 prints only odd numbers by skipping even numbers using continue
.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:
i = 1
. If i
is odd, print it.i
is even, skip printing and move to the next number.i
is greater than 10.break
and continue
while (true)
loop keeps asking the user to enter a number between 1 and 10.continue
restarts the loop to ask again.break
exits the loop.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:
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)
Tip:
for
, while
, or do-while
loops.break
and continue
with different types of loops in Java.for
, while
, do-while
) and see how break
and continue
affect their execution.break
and continue
break
when you want to exit a loop completely, even if the loop condition is still true.continue
when you want to skip the rest of the current iteration and move to the next one.break
, continue
, and variable names to help them stand out.