0% found this document useful (0 votes)
168 views

Example Label, Break, Continue With Java

This document contains examples of using break and continue statements with labeled blocks in Java. It shows that a break statement with a label can break out of the labeled block, while continue only continues the current iteration. The examples demonstrate breaking or continuing the outer loop by labeling both the inner and outer for loops.

Uploaded by

Sanjoy Pathak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views

Example Label, Break, Continue With Java

This document contains examples of using break and continue statements with labeled blocks in Java. It shows that a break statement with a label can break out of the labeled block, while continue only continues the current iteration. The examples demonstrate breaking or continuing the outer loop by labeling both the inner and outer for loops.

Uploaded by

Sanjoy Pathak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

// Java program to illustrate using break with label

class BreakLabelDemo {
public static void main(String args[])
{
boolean t = true;
first : {
second : {
third : {
System.out.println("Before the break statement");
// if (t) break second;
System.out.println("This won't execute.");
} //third
System.out.println("This won't execute.");
} //second
System.out.println("This is after second block.");
} //first
}
}

Output:
Before the break statement.
This is after the second block.
1. public class BreakExample3 {
2. public static void main(String[] args) {
3. aa:
4. for(int i=1;i<=3;i++){
5. bb:
6. for(int j=1;j<=3;j++){
7. if(i==2&&j==2){
8. //using break statement with label
9. break aa;
10. }
11. System.out.println(i+" "+j);
12. }
13. }
14. }
15. }

Output:

1 1
1 2
1 3
2 1
If you use break bb;, it will break inner loop only which is the
default behavior of any loop

1. public class LabeledForExample2 {


2. public static void main(String[] args) {
3. aa:
4. for(int i=1;i<=3;i++){
5. bb:
6. for(int j=1;j<=3;j++){
7. if(i==2&&j==2){
8. break bb;
9. }
10. System.out.println(i+" "+j);
11. }
12. }
13. }
14. }

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3
Continue Statement with Inner Loop

1. //Java Program to illustrate the use of continue statement


2. //inside an inner loop
3. public class ContinueExample2 {
4. public static void main(String[] args) {
5. //outer loop
6. for(int i=1;i<=3;i++){
7. //inner loop
8. for(int j=1;j<=3;j++){
9. if(i==2&&j==2){
10. //using continue statement inside inner loop
11. continue;
12. }
13. System.out.println(i+" "+j);
14. }
15. }
16. }
17. }

Output:

1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
Java Continue Statement with Labeled For Loop
1. public class ContinueExample3 {
2. public static void main(String[] args) {
3. aa:
4. for(int i=1;i<=3;i++){
5. bb:
6. for(int j=1;j<=3;j++){
7. if(i==2&&j==2){
8. //using continue statement with label
9. continue aa;
10. }
11. System.out.println(i+" "+j);
12. }
13. }
14. }
15. }

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

You might also like