What Does Continue Function in Java Mean

What is a continue statement in Java and how to use it?


This article will help you understand what the continue statement in Java Programming language is.

Continue Statement

The statement continue, is just the opposite of Break statement. As soon as the continue statement is executed in a loop, the control skips rest of the statements for that value and resumes for the next iteration.

Syntax

          while                              (          condition          ){                              Statement                              1          ;                      _________________          ;                      _________________          ;                              if                              (          another condition                    is                              true          ){                              continue          ;                              }                              Statement                              2          ;                      _________________          ;                      _________________          ;                              }        

Given below are some examples where continue statement is used.

Example 1 − Continue Statement with Loop

          

public class ContinueExample1 { public static void main ( String [ ] args) { int i; for (i = 0 ; i <= 5 ; i++ ) { if ( i == 2 ) { continue ; } System .out. println (i) ; } } }

Output

0 1 3 4 5        

In this code, the goal is to print from 0 to 5 without the number 2. The for loop is given so that the value of variable i is increased by 1 every loop, i.e. i = 0 in first loop, i = 1 in 2nd loop and so on. Now we want to avoid 2 from printing. For that an if statement is added which checks whether the value of variable i has reached 2 or not. After reaching, the continue statement is executed, which skips the remaining statements (here the System.out.println line) of the for loop and continues to the next loop. Now value of variable i is 3 and so the printing continues. So now the output becomes 0 1 3 4 5.

Example 2 − Continue Statement with Inner Loop

          

public class ContinueExample2 { public static void main ( String [ ] args) { int i,j; for (i = 0 ; i <= 5 ; i++ ) { for (j = 0 ; j <= 5 ; j++ ) { if ( i == 2 && j == 2 ) { continue ; } System .out. println (i + " " + j) ; } } } }

Output

0 1 0 2 0 3 0 4 0 5  1 0 1 2 1 3 1 4 1 5  2 0 2 1 2 3 2 4 2 5  3 0 3 1 3 2 3 4 3 5  4 0 4 1 4 2 4 3 4 5  5 0 5 1 5 2 5 3 5 4        

In this code, the goal is to display the value of two for loop variables side by side excluding times when both variables are equal, i.e this code will display {1, 2}, {2, 3}, {2,4}, etc. but not print {1, 1}, {2, 2}, {3, 3}, etc. In order to achieve this, a continue statement has been added inside the inner loop. Why is it added to the inner loop, because for every single iteration of the outer loop, the inner loop iterates 6 times in this code. So if we had put the continue statement outside the inner loop, then only {5,5} would have been excluded. Now when both the for loop variables are same, like when both i and j are 2, the continue statement skips the print line and moves to the next inner loop iteration.

Example 3 − Continue Statement with Labelled For Loop

          

public class ContinueExample3 { public static void main ( String [ ] args) { Label1 : for ( int i = 0 ; i <= 5 ; i++ ) { Label2 : for ( int j = 0 ; j <= 5 ; j++ ) { if (i == 3 && j == 3 ) { continue Label1 ; } System .out. println (i + " " + j) ; } System .out. println ( ) ; } } }

Output

0 0 0 1 0 2 0 3 0 4 0 5  1 0 1 1 1 2 1 3 1 4 1 5  2 0 2 1 2 2 2 3 2 4 2 5  3 0 3 1 3 2  4 0 4 1 4 2 4 3 4 4 4 5  5 0 5 1 5 2 5 3 5 4 5 5        

This code of a simple demonstration of what happens when there exists a huge number of nested for loops in a code, and you want to skip a particular for loop during some condition checking. For that, we add label to every for loop, i.e. naming them individually. Now if one needs to skip the statements of a particular for loop, just write continue <for loop label name>.

Example 4 − Continue Statement in While Loop

          

public class ContinueExample4 { public static void main ( String [ ] args) { int i = 0 ; while (i <= 10 ) { if (i == 5 ) { i++ ; continue ; } System .out. println (i) ; i++ ; } } }

Output

          0  1  2  3  4  6  7  8  9 10        

In this code, we are using while loop instead of for loop to skip a certain number and print the remaining numbers.

Example 5 − Continue Statement in Do-while Loop

          

public class ContinueExample5 { public static void main ( String [ ] args) { int i = 0 ; do { if (i == 5 ) { i++ ; continue ; } System .out. println (i) ; i++ ; } while (i <= 10 ) ; } }

Output

          0  1  2  3  4  6  7  8  9 10        

In this code, we are using do-while loop instead of while loop to skip a certain number and print the remaining numbers.

raja

Updated on 05-Sep-2022 09:08:05

  • Related Questions & Answers
  • What is a break statement in Java and how to use it?
  • What is a switch case statement in Java and how to use it?
  • What is continue statement in JavaScript?
  • How to use continue statement in Python loop?
  • What is the 'if' statement in Java and how to use it?
  • What is the 'if else' statement in Java and how to use it?
  • What is the 'nested if' statement in Java and how to use it?
  • Java continue statement with Loop
  • How to use break and continue statements in Java?
  • How can I use a label with continue statement in JavaScript?
  • What is the 'if...else if...else' statement in Java and how to use it?
  • What is method hiding in Java and how to use it?
  • PHP continue Statement
  • Can we use continue statement in a Python if clause?
  • How do we use continue statement in a while loop in C#?

jonespaided.blogspot.com

Source: https://www.tutorialspoint.com/What-is-a-continue-statement-in-Java-and-how-to-use-it

0 Response to "What Does Continue Function in Java Mean"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel