Loop Control
Loop control statements give you the power to influence how loops behave as they run. In Java, break and continue are two key statements that help you control the flow of your loops.
- break lets you stop a loop early, jumping out as soon as a certain condition is met;
- continue skips the rest of the current loop cycle and moves straight to the next one.
Using break and continue helps you write cleaner, more efficient code by avoiding unnecessary steps and focusing on what matters most in your logic.
break
The break statement is used inside loops to immediately exit the loop, regardless of the loop's condition. When the program encounters a break statement, it stops the current loop and continues executing the code that follows the loop.
Key points:
breakonly exits the innermost loop orswitchstatement where it appears;- Code after the
breakstatement within the same block will not execute; - It can be used in
for,while, anddo-whileloops, as well as inswitchstatements; - Overusing
breakcan make code harder to read, so use it thoughtfully.
Example:
Imagine you are searching for the number 5 in a sequence from 1 to 10. Instead of checking every number, you want the program to stop immediately once the number is found.
Main.java
123456789101112131415package com.example; public class Main { public static void main(String[] args) { System.out.println("Searching for the number 5..."); for (int i = 1; i <= 10; i++) { if (i == 5) { System.out.println("Found 5! Exiting loop."); break; } System.out.println("Checked: " + i); } System.out.println("Loop ended."); } }
- The
forloop iterates from 1 to 10; - Inside the loop, we check if the current number
iequals 5; - When
i == 5, thebreakstatement immediately exits the loop, skipping any remaining iterations; - If
iis not 5, the program prints"Checked: i"; - After the loop ends (either normally or due to
break), the program prints"Loop ended.".
continue
The continue statement is used inside loops to skip the current iteration and move directly to the next one. When you use continue, the loop does not execute any remaining code in the current iteration; instead, it jumps straight to the loop's condition check for the next cycle.
When to use continue:
- When you need to skip processing for specific values;
- When you want to avoid executing part of the loop body under some conditions;
- When filtering out unwanted cases during iteration.
Example:
You want to print all numbers from 1 to 5 except 3:
Main.java
123456789101112package com.example; public class Main { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; } System.out.println("Number: " + i); } } }
- The
forloop iterates from 1 to 5; - Inside the loop, we check if the current number
iequals 3; - When
i == 3, thecontinuestatement is executed, which immediately skips the rest of the loop body and moves to the next iteration; - For all other numbers, the program prints
"Number: i"; - As a result, the number 3 is skipped.
Both statements help control the flow of loops, making code more efficient and readable.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Awesome!
Completion rate improved to 9.09
Loop Control
Swipe um das Menü anzuzeigen
Loop control statements give you the power to influence how loops behave as they run. In Java, break and continue are two key statements that help you control the flow of your loops.
- break lets you stop a loop early, jumping out as soon as a certain condition is met;
- continue skips the rest of the current loop cycle and moves straight to the next one.
Using break and continue helps you write cleaner, more efficient code by avoiding unnecessary steps and focusing on what matters most in your logic.
break
The break statement is used inside loops to immediately exit the loop, regardless of the loop's condition. When the program encounters a break statement, it stops the current loop and continues executing the code that follows the loop.
Key points:
breakonly exits the innermost loop orswitchstatement where it appears;- Code after the
breakstatement within the same block will not execute; - It can be used in
for,while, anddo-whileloops, as well as inswitchstatements; - Overusing
breakcan make code harder to read, so use it thoughtfully.
Example:
Imagine you are searching for the number 5 in a sequence from 1 to 10. Instead of checking every number, you want the program to stop immediately once the number is found.
Main.java
123456789101112131415package com.example; public class Main { public static void main(String[] args) { System.out.println("Searching for the number 5..."); for (int i = 1; i <= 10; i++) { if (i == 5) { System.out.println("Found 5! Exiting loop."); break; } System.out.println("Checked: " + i); } System.out.println("Loop ended."); } }
- The
forloop iterates from 1 to 10; - Inside the loop, we check if the current number
iequals 5; - When
i == 5, thebreakstatement immediately exits the loop, skipping any remaining iterations; - If
iis not 5, the program prints"Checked: i"; - After the loop ends (either normally or due to
break), the program prints"Loop ended.".
continue
The continue statement is used inside loops to skip the current iteration and move directly to the next one. When you use continue, the loop does not execute any remaining code in the current iteration; instead, it jumps straight to the loop's condition check for the next cycle.
When to use continue:
- When you need to skip processing for specific values;
- When you want to avoid executing part of the loop body under some conditions;
- When filtering out unwanted cases during iteration.
Example:
You want to print all numbers from 1 to 5 except 3:
Main.java
123456789101112package com.example; public class Main { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; } System.out.println("Number: " + i); } } }
- The
forloop iterates from 1 to 5; - Inside the loop, we check if the current number
iequals 3; - When
i == 3, thecontinuestatement is executed, which immediately skips the rest of the loop body and moves to the next iteration; - For all other numbers, the program prints
"Number: i"; - As a result, the number 3 is skipped.
Both statements help control the flow of loops, making code more efficient and readable.
Danke für Ihr Feedback!