Javascript required
Skip to content Skip to sidebar Skip to footer

Break and Continue in Nested Loops Java

Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

How to Use Break, Continue, and Label in Loop in Java? Example

break and continue in Java are two essential keyword beginners needs to familiar while using loops ( for loop, while loop and do while loop). break statement in java is used to break the loop and transfers control to the line immediately outside of the loop while continue is used to escape current execution and transfers control back to the start of the loop. Both break and continue allows programmers to create sophisticated algorithms and looping constructs.

In this java tutorial, we will see examples of break and continue statements in Java and some important points related to breaking the loop using label and break statements. break keyword can also be used inside switch statements to break current choice and if not used it can cause fall-through on switch.

Well break is not alone on breaking switch case you can also sued throw keyword on switch case.


break and continue statement in Java - example.

break and continue example in java  for loop In this example we are calculating sum of odd numbers until number 5 appear, where we break the loop by using break statement. after calculating sum we also call continue which cause last line of loop to not execute. remember call to continue is inside if block which checks for odd numbers, so that line will appear in case of even number but not in case of odd number.

This example of break and continue shows that upon calling break , loop terminates and control goes to first line outside the loop while upon calling continue , loop doesn't terminate but rest of execution doesn't happen and loop continues from next iteration.



/**
* Simple Java program which demonstrate use of break and continue statements in Java
* In this example break is used to break loop and continue is used to escape current  *iteration of loop once a condition is true.
* @author
*/

public class BreakContinueExample {

public static void main( String args[]) {

int [] numbers= new int []{ 1,2,3,4,5,6,7,8,9,10 } ;

//calculate sum of all numbers till 5 appears
int sum = 0 ;
for ( int i=0 ; i< numbers.length ; i++){
System.out.println ( "Executing for loop with iteration number: " + i) ;
if (i == 5 ){
System.out.println ( "calling break statement to break for loop" ) ;
break ;
}
if (i%2 != 0 ){
sum = sum + i;
System.out.println ( "calling continue statement to start new iteration" ) ;
continue ;
}
System.out.println ( "Last line of loop, not executing for odd numbers due
to continue statement i: "
+ i) ;
}
System.out.println ( "Outside of for loop, sum: " + sum) ;
}
}

Output:
Executing for loop with iteration number: 0
Last line of loop, not executing for odd numbers due to continue statement i: 0
Executing for loop with iteration number: 1
calling continue statement to start new iteration
Executing for loop with iteration number: 2
Last line of loop, not executing for odd numbers due to continue statement i: 2
Executing for loop with iteration number: 3
calling continue statement to start new iteration
Executing for loop with iteration number: 4
Last line of loop, not executing for odd numbers due to continue statement i: 4
Executing for loop with iteration number: 5
calling break statement to break for loop
Outside of for loop, sum: 4

continue and break examples with label in Java

You can use labels with break and continue . labels are where you can shift control from break and continue . by default break goes outside of loop but if you more than one loop you can use the break statement to break a specific loop by providing labels.

The same is true for continue . if you are working on nested loops labels gives more control to break and continue . In the following the example of break and continue with labels. we have a nested loop and we have created two labels OUTER and INNER.

OUTER label is for outer loop and INNER label is for inner loop. we are iterating through array and print value of odd number from outer loop and then use continue statement, which ignores execution of inner loop. if its even number than inner loop executes and breaks as soon as it prints the number.

/**
* Simple Java program which demonstrate use of break and continue statements in Java
* with lables, break and continue can be used alongside label and loop.
*
* @author Javin
*/

public class BreakContinueWithLabel {

public static void main( String args[]) {

int [] numbers= new int []{ 100,18,21,30 } ;

//Outer loop checks if number is multiple of 2
OUTER: //outer label
for ( int i = 0 ; i<numbers.length ; i++){
if (i % 2 == 0 ){
System.out.println ( "Odd number: " + i + ", continue from OUTER label" ) ;
continue OUTER;
}

            INNER:
for ( int j = 0 ; j<numbers.length ; j++){
System.out.println ( "Even number: " + i + ", break  from INNER label" ) ;
break INNER;
}
}

}
}

Output:
Odd number: 0, continue from OUTER label
Even number: 1, break from INNER label
Odd number: 2, continue from OUTER label
Even number: 3, break from INNER label

As shown in the above example of break and continue with labels, It provides a lot of conveniences to break and continue in case of a nested loop.

Important point about the break and continue in Java

1. break keyword transfers control to next statement outside loop while continue keyword transfers control to the beginning of loop, ignoring rest of lines in loop.

2. break can also be used inside CASE statement of switch construct.

3. an optional label can be provided after break and continue which cause to apply break and continue on specified loop.

That's all on break and continue statement in Java. break and continue allows programmer to control flow of execution in loops. We have also seen example of break and continue with and without labels , which allows you to write more sophisticated looping construct in java.

Other Java beginners tutorial you may like

coplandcestruch.blogspot.com

Source: https://javarevisited.blogspot.com/2012/05/break-continue-and-lablel-in-loop-java.html