Continue Vs Break While Loop Java

Java provides a concept of branch/branching statements that allows us to change the flow of execution based on some condition. In java there are three types of branching statements i.e. break, continue and return. The break and continue can be applied to control the flow of execution by jumping from one statement to other. It can be used within the switch statement and looping structures however the continue statement can be used in the loops only.

This article will provide a comprehensive overview of the following concepts regarding the break and continue statements:

  • What are break and Continue statements in Java
  • How to use break and continue statements in Loops
  • How to use break Statements in Switch cases

So, let's start!

Break Statement in Java

In Java, the "break" can be used within the loops and switches to jump out of the iteration. The syntax of the break statement is shown in the below-given snippet:

It will be written in small letters, as Java is case sensitive language therefore if someone writes it in uppercase it wouldn't work.

How to Use Break Statement in Java Loops

This section will provide a detailed guide for how to use break statement in loops. For better understanding consider the below given example:

Example

In the below-given code snippet, the loop starts with i=0 and it specifies the consition as i<=30. After each iteration, the value of "i" will be incremented 5 times. An if statement is specified within the loop, to terminate the loop if value of "i" becomes equal to 15.

public static void main( String [ ] args) {
int i= 0 ;
while (i<= 30 ) {
System.out.println ( "Number : " + i) ;
i+= 5 ;
if (i== 15 ) {
break ;
}
}
}

The above snippet will provide the following output:

In the same way the break statement can be used within for loop and do-while loop.

Continue Statement in Java

In Java, a control statement used for jumping out of a loop is known as the continue statement. Using continue statement we can skip any iteration of a loop and it can be used in any type of loop such as for, while loop, etc.

The continue statement is dependent on a condition when it meets the specified condition, the continue statement breaks the current iteration and moves to the next iteration.

Syntax

The below-given snippet presents the basic syntax of the continue statement in Java:

How to Use Continue Statement in Java

Let's understand the working of the continue statement with the help of an example:

Example

The below-given code determines how continue statement works within the for loop:

public static void main( String [ ] args) {
for ( int i= 0 ; i<= 30 ; i+= 5 ) {

if (i== 15 )
{ continue ;
}
System.out.println ( "Number : " + i) ;
}

}

In the above code snippet, we specified an if statement that determines if the value of i is equal to 15 then skip that iteration and move on to the next iteration. The code along with the output is shown in the following snippet:

From the output, we can observe that "15" is missing which verifies that the "continue" statement is working properly.

Continue statement in while and do-while loop

The behavior of the continue statement will be different for the (while and do-while loop) as compared to the for loop. In for loop if a specified condition meets then the control will be shifted to the next iteration however in the while and do-while loop, if continue statement occurs then the control will be shifted to the condition/boolean expression that is specified within the while loop.

Example

This example will provide the profound understanding of how continue statement works in while loop:

public static void main( String [ ] args) {
int i= 0 ;
while (i<= 30 ) {
if (i== 15 ) {
continue ;
}
System.out.println ( "Number : " + i) ;
i+= 5 ;
}
}

The above snippet shows the following output:

From the output, we observed that the while loop printed only those values that come before the condition, i.e. if(i==15). This is because when a "continue" statement occurs then the value of i is staying equal to 15 and does not get incremented. Therefore we have to increment the value of i="i+5" in the if statement if we want to print all other values.

The modified code along with output will look something like this:

Now the output verifies that this time continue statement skipped only specified value and printed all other values.

Conclusion

When a loop encounters a break statement then it terminates the entire loop however when a loop encounters a continue statement then it terminates/skips the current iteration and moves to the next iteration. This article presents a thorough overview of break and continue statement in Java, moreover, it provides multiple examples for the profound understanding of the concepts.

About the author

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.

howardforgue.blogspot.com

Source: https://linuxhint.com/break-continue-statements-java/

0 Response to "Continue Vs Break While Loop Java"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel