C

Core Java tutorial for beginners

Clean • Professional

Core Java Break & Continue Statements – Pattern Programming Examples

4 minute

Break & Continue Statements and Pattern Programming in Java

Loops in Java are powerful, but sometimes you need to control their execution. That’s where break and continue come in. We’ll also cover pattern programming examples, which are commonly asked in interviews.


Break Statement

  • Terminates the current loop immediately.
  • Used to exit early when a condition is met.

Syntax:

break;

Example: Stop loop when number equals 5

for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break; // exit loop
    }
    System.out.println(i);
}

Output:

1
2
3
4

Continue Statement

  • Skips current iteration and moves to the next iteration.
  • Useful when you want to ignore certain conditions.

Syntax:

continue;

Example: Skip number 5

for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        continue; // skip this iteration
    }
    System.out.println(i);
}

Output:

1
2
3
4
6
7
8
9
10

Nested Loops with Break & Continue

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (i == 2 && j == 2) {
            break; // exits inner loop
        }
        System.out.println("i=" + i + ", j=" + j);
    }
}

Output:

i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1
i=3, j=1
i=3, j=2
i=3, j=3

⚠️ Tip: For breaking outer loops, use labeled break:

outerLoop:
for(int i=1;i<=3;i++){
    for(int j=1;j<=3;j++){
        if(i==2 && j==2) break outerLoop;
    }
}

Pattern Programming Examples

Pattern programming is popular in Java interviews. Let’s look at common patterns.


1. Right-Angled Triangle

int n = 5;
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
        System.out.print("* ");
    }
    System.out.println();
}

Output:

*
* *
* * *
* * * *
* * * * *

2. Inverted Triangle

int n = 5;
for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= i; j++) {
        System.out.print("* ");
    }
    System.out.println();
}

Output:

* * * * *
* * * *
* * *
* *
*

3. Pyramid Pattern

int n = 5;
for (int i = 1; i <= n; i++) {
    for (int j = n; j > i; j--) {
        System.out.print(" ");
    }
    for (int k = 1; k <= (2*i-1); k++) {
        System.out.print("*");
    }
    System.out.println();
}

Output:

    *
   ***
  *****
 *******
*********

Tips for Pattern Programming

  1. Understand row and column relationships.
  2. Use nested loops carefully.
  3. Start with simple patterns, then try complex patterns.
  4. Many interview questions are variations of triangles, pyramids, and diamonds.

🏆 Top 5 Java Interview Questions - Break, Continue & Pattern Programs

1. What is the difference between break and continue?

Answer:

Featurebreakcontinue
PurposeTerminates the entire loop immediatelySkips current iteration and continues with next iteration
UsageInside loops or switchInside loops only
EffectExits loop completelyLoop continues with next iteration
Exampleif(i==5) break;if(i==5) continue;

Example:

for(int i=1;i<=5;i++){
    if(i==3) continue; // skip 3
    System.out.println(i);
}
// Output: 1 2 4 5
for(int i=1;i<=5;i++){
    if(i==3) break; // exit loop
    System.out.println(i);
}
// Output: 1 2

2. Can break be used outside a loop?

Answer:

  • No, break is only allowed inside loops (for, while, do-while) or switch statements.
  • Using it outside these constructs causes a compile-time error.

3. What is a labeled break in Java?

Answer: A labeled break allows you to exit a specific outer loop when working with nested loops.

Syntax:

outerLoop: for(int i=1;i<=3;i++){
    for(int j=1;j<=3;j++){
        if(i==2 && j==2) break outerLoop;
        System.out.println(i + " " + j);
    }
}

Output:

1 1
1 2
1 3
2 1
  • Here, the outer loop is terminated when the condition matches.

4. How do you print a pyramid pattern using loops?

Answer: You typically use nested loops — outer loop for rows, inner loop for spaces and stars.

Example:

int rows = 5;
for(int i=1;i<=rows;i++){
    for(int j=i;j<rows;j++) System.out.print(" "); // spaces
    for(int k=1;k<=(2*i-1);k++) System.out.print("*"); // stars
    System.out.println();
}

Output:

    *
   ***
  *****
 *******
*********

5. How do you print a right-angled triangle pattern in Java?

Answer:

int n = 5;
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
        System.out.print("* ");
    }
    System.out.println();
}

Output:

*
* *
* * *
* * * *
* * * * *


Article 0 of 0