Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Loop Control | Exploring Loop Variations
Java Loops

bookLoop 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:

  • break only exits the innermost loop or switch statement where it appears;
  • Code after the break statement within the same block will not execute;
  • It can be used in for, while, and do-while loops, as well as in switch statements;
  • Overusing break can 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

Main.java

copy
123456789101112131415
package 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."); } }
  1. The for loop iterates from 1 to 10;
  2. Inside the loop, we check if the current number i equals 5;
  3. When i == 5, the break statement immediately exits the loop, skipping any remaining iterations;
  4. If i is not 5, the program prints "Checked: i";
  5. 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

Main.java

copy
123456789101112
package 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); } } }
  1. The for loop iterates from 1 to 5;
  2. Inside the loop, we check if the current number i equals 3;
  3. When i == 3, the continue statement is executed, which immediately skips the rest of the loop body and moves to the next iteration;
  4. For all other numbers, the program prints "Number: i";
  5. As a result, the number 3 is skipped.

Both statements help control the flow of loops, making code more efficient and readable.

question mark

Which statement is used to immediately exit a loop in Java?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 9.09

bookLoop 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:

  • break only exits the innermost loop or switch statement where it appears;
  • Code after the break statement within the same block will not execute;
  • It can be used in for, while, and do-while loops, as well as in switch statements;
  • Overusing break can 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

Main.java

copy
123456789101112131415
package 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."); } }
  1. The for loop iterates from 1 to 10;
  2. Inside the loop, we check if the current number i equals 5;
  3. When i == 5, the break statement immediately exits the loop, skipping any remaining iterations;
  4. If i is not 5, the program prints "Checked: i";
  5. 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

Main.java

copy
123456789101112
package 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); } } }
  1. The for loop iterates from 1 to 5;
  2. Inside the loop, we check if the current number i equals 3;
  3. When i == 3, the continue statement is executed, which immediately skips the rest of the loop body and moves to the next iteration;
  4. For all other numbers, the program prints "Number: i";
  5. As a result, the number 3 is skipped.

Both statements help control the flow of loops, making code more efficient and readable.

question mark

Which statement is used to immediately exit a loop in Java?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
some-alt