allthingsare๐Ÿ…ฟ๏ธ.com Books โฌ…๏ธ Back allthingsare

โ€œThe Lord is my shepherd; I shall not want.โ€
[Psalm 23:1]


loop flow control


5.2.1 Using break in Loops

The break statement is a tool that gives you immediate control over when a loop should stop running.
Instead of waiting for the loopโ€™s condition to naturally become false, you can decide to exit early if a certain situation happens.

For example, imagine you are searching for a specific item in a list.
As soon as you find it, you donโ€™t need to keep looping โ€” you use break to stop the loop right there.
This saves time and resources.

Another example is input validation: if youโ€™re checking a batch of user entries and detect something invalid, you can break the loop to handle the error immediately.

The break statement works in for, while, and do-while loops, and even inside switch-case structures.

When using break, always be clear about why you are stopping early.
If you find yourself using break too often, double-check your loop design โ€” sometimes a better condition or logic flow might make break unnecessary.

Mini Lab

java
public class BreakInLoop { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i == 5) { System.out.println("Breaking at i = " + i); break; } System.out.println("i = " + i); } } }

5.2.2 Using continue in Loops

The continue statement lets you skip the rest of the loop body for the current iteration and immediately move to the next cycle.

This is useful when you want to ignore certain cases but keep the loop running normally.
For example, you might skip over empty input fields, negative numbers, or unwanted items in a list.

Without continue, you might write extra if-else blocks that make your code harder to read.
With continue, you can put your skip logic up front and keep your main logic clear and focused.

A common pattern: place a condition at the top of your loop. If that condition matches, run continue to skip the current iteration.

Just like break, use continue carefully โ€” overusing it can sometimes hide deeper logic problems.

Mini Lab

java
public class ContinueInLoop { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skip when i is 3 } System.out.println("i = " + i); } } }

5.2.3 Combining break and continue

In more advanced loops, you often use both break and continue together for maximum control.
For example, you might skip invalid entries but stop completely once you hit a certain threshold or match.

This is very common in data processing: scanning a file line by line, ignoring blank lines (continue) but stopping the scan when you reach a special marker line (break).

Combining these tools lets you build flexible and efficient loops that respond well to real-world, messy data.

The golden rule: keep your loop conditions clear and keep break and continue statements visible and justified.
Never hide them in deeply nested blocks โ€” that makes your code harder to understand and debug.

Mini Lab

java
public class BreakContinueCombined { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; // Skip even numbers } if (i > 7) { break; // Stop loop after 7 } System.out.println("i = " + i); } } }