break and continue in Java

break and continue in Java

In Java programming, the break and continue statements are used to manage or handle the ongoing control flow of statements such as for loops, while loops, do-while loops, and switch and if-else statements. The break and continue statements modify the default behavior of a loop or switch statement in an existing program. The break and continue statements are useful for breaking the current program loop by exiting the loop completely or skipping the current iteration of the loop.

break and continue in Java

Break Statement in Java.

The break statement in Java is used to prematurely terminate a print loop or switch statement within a running program loop, even if the current program loop condition has not been met. After the break statement is executed, control moves to the next statement that comes after the loop or switch.

Break Statement in Java Loops.

The break statement in any Java program can be used to immediately exit or break from a for loop, while loop, do-while loop, and switch if-else statement. After the break statement is executed, the program breaks or terminates the loop, and the program continues with the remaining program code statements.

Break in switch statement.

Using the break statement in a Java program in a switch statement allows you to break the fall-through concept behavior in the program. This means that the break statement breaks the execution of the remaining case blocks after one case block is executed.

Syntax of the break statement.

break;

Example of the break statement in a for loop.

public class Main

{

public static void main(String[] args) {

/ Here it prints numbers from 1 to 10, but we use the break keyword when the loop reaches 7.

for (int p = 1; p <= 10; p++) {

if (p == 7) {

break; // Here the for loop breaks and exits when the loop reaches p == 7.

}

System.out.println(p);

}

}

}

Break in switch statement explanation.

  • Here, the for loop prints numbers from 1 to 6 in the program. When the value of the p variable equals 7, the break keyword statement executes, and the loop breaks and exits immediately.

Example of break in a Java switch statement.

public class Main

{

public static void main(String[] args) {

int country = 3;

switch (country) {

case 1:

System.out.println(“Singapore”);

break;

case 2:

System.out.println(“Usa”);

break;

case 3:

System.out.println(“India”);

break; // here it Exits the switch statement after the country = 3 reach

case 4:

System.out.println(“China”);

break;

case 5:

System.out.println(“UK”);

break;

case 6:

System.out.println(“Africa”);

break;

default:

System.out.println(“Wrong country selection”);

}

}

}

break in a Java switch statement explanation.

  • Here in this program, the switch statement is followed by a break statement after printing the “India” statement, which prevents any other remaining case statements from being evaluated and exits the switch statement.

Continue Statement in Java.

The continue statement in Java programs is used to skip the current iteration of a loop and proceed to the next iteration or remaining flow. The continue statement only affects the current iteration of the loop. This means that the continue statement does not terminate the current program loop; it simply proceeds to the next remaining statements and prints them.

Continue Statements in Loops.

When the continue statement is used in a Java program, it immediately skips the remaining program code within the loop for the current program loop iteration and continues with the next remaining iteration, rechecking and analysing the remaining loop conditions.

Syntax of the continue statement.

continue;

Example of continue in a for loop.

public class Main

{

public static void main(String[] args) {

/ here it prints numbers from 1 to 10, but it skips when the number is 7 and prints the remaining statement

for (int p = 1; p <= 10; p++) {

if (p == 7) {

continue; // here it skips the current iteration when p is 7

}

System.out.println(p);

}

}

}

Explaining continue in a for loop.

  • In this program, the for loop prints numbers from 1 to 10, but when the value of p reaches 7, the continue statement bypasses the print statement and moves on to the next iteration.

Example of continue in a while loop.

public class Main

{

public static void main(String[] args) {

int p = 1;

/ Here it prints numbers from 1 to 10, but skips when p reaches 6

while (p <= 10) {

if (p == 6) {

p++; // Here it increments p to prevent an infinite loop situation

continue; // Skip printing the value when p reaches 6

}

System.out.println(p);

p++; // Now it increments the p variable for all other remaining cases

}

}

}

continue in a while loop explanation.

  • In this program, the while loop prints numbers from 1 to 10, skipping printing values ​​for the p variable value 6. The p variable p++ inside the continue statement ensures that the loop doesn’t run forever.

Example of continue in a do-while loop.

public class Main

{

public static void main(String[] args) {

int p = 1;

/ Here it prints numbers from 1 to 10 but skips when the number is 6

do {

if (p == 6) {

p++; // Here it increments the p variable to prevent an infinite loop

continue; // Here it skips printing when p is 6

}

System.out.println(p);

p++; // Here it increments the p variable for all other cases

} while (p <= 10);

}

}

Explanation of a do-while loop.

  • In this example, when the p variable equals 6, the continue statement skips the program iteration process and prints the next statement.

Use cases for break and continue in Java programming.

Break keyword use cases.

  • The break statement is used to immediately exit a program loop when a particular condition is met. For example, finding a specific value in an array of data.
  • The switch statement exits the case block when a specific case or condition is met in a Java program, thus avoiding the development of fall-through behavior.

Continue Statement Use Cases.

  • To skip the processing of specific values ​​in a Java program loop, for example, skipping a negative number in a list.
  • To skip the current program iteration process and continue with the next iteration when certain special conditions are met in a Java program.

Main Differences Between break and continue java statement

AspectBreak statementContinue statement
Purpose of statementBreak statement immediate Exits the program loop or switch statement completely.Continue statement used to Skips the current iteration and moves to the next iteration of the loop process.
Impact on LoopBreak statement immediate Terminates the program loop entirely and control moves to the next statement after the loop break.Continue statement used to Skips the current loop iteration, but the loop continues to the next iteration will print.
Use in switchBreak keyword Used to exit the switch statement and prevent from fall-through concept.Continue statement Cannot be used in a switch statement.
When to applyBreak statement used When you want to exit the loop or switch early when you need.Continue statement used When you want to skip spec loop situation.

Conclusion of break and continue.

  • The break statement in a Java program helps the programmer to prematurely exit a loop or switch statement when a specific condition is met.
  • The continue statement in a Java program allows the programmer to skip the current iteration process of the loop and move on to the next statement.

Leave a Reply