Java Break/Continue

Introduction to Break and Continue

In Java, the break and continue statements are used to control the flow of loops. These statements allow you to jump out of the normal flow of execution in a loop or switch statement, making your program more flexible and efficient.

  • The break statement is used to exit a loop or a switch case entirely.
  • The continue statement is used to skip the current iteration of a loop and move to the next one.

Break Statement

The break statement is used to terminate the innermost loop or switch in which it appears. Once the break statement is executed, the control is transferred out of the loop or switch block, and the program continues from the next statement outside the block.

Syntax

break;

Example 1: Using Break in a Loop

public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i is 5
}
System.out.println("Number: " + i);
}
System.out.println("Loop terminated.");
}
}

Output:

Number: 1  
Number: 2
Number: 3
Number: 4
Loop terminated.

Example 2: Using Break in a Switch Statement

public class BreakInSwitch {
public static void main(String[] args) {
int day = 3;

switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
break;
}
}
}

Output:

Wednesday

Continue Statement

The continue statement is used to skip the current iteration of a loop and jump to the next iteration. Unlike break, it does not terminate the loop but allows the program to proceed with the next cycle.

Syntax

continue;

Example 1: Using Continue in a Loop

public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip the current iteration for even numbers
}
System.out.println("Odd Number: " + i);
}
}
}

Output:

Odd Number: 1  
Odd Number: 3
Odd Number: 5
Odd Number: 7
Odd Number: 9

Example 2: Continue in a While Loop

public class ContinueInWhile {
public static void main(String[] args) {
int i = 0;

while (i < 10) {
i++;
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println("Odd Number: " + i);
}
}
}

Output:

Odd Number: 1  
Odd Number: 3
Odd Number: 5
Odd Number: 7
Odd Number: 9

Break vs Continue

AspectBreakContinue
FunctionalityExits the loop or switch entirely.Skips the current iteration of the loop.
Control FlowTransfers control outside the loop or switch.Transfers control to the next iteration.
Usage ScenarioUsed to terminate the loop prematurely.Used to skip unwanted iterations.

Common Mistakes to Avoid

  1. Unintended Infinite Loops:
    • Using continue without proper condition handling can lead to infinite loops in while or do-while constructs.
  2. Misplaced Break/Continue Statements:
    • Ensure that break and continue are inside the loop or switch block; otherwise, they cause a compilation error.
  3. Confusion Between Break and Continue:
    • Remember, break exits the loop entirely, while continue skips only the current iteration.

Advanced Example: Combining Break and Continue

public class BreakContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 3) {
System.out.println("Skipping number 3");
continue; // Skip the iteration when i is 3
}
if (i == 7) {
System.out.println("Breaking the loop at number 7");
break; // Terminate the loop when i is 7
}
System.out.println("Number: " + i);
}
}
}

Output:

Number: 1  
Number: 2
Skipping number 3
Number: 4
Number: 5
Number: 6
Breaking the loop at number 7

Leave a Comment