Javascript required
Skip to content Skip to sidebar Skip to footer

Continue Methods Until Value Reached Java

Continue Statement in Java

Introduction to Continue Statement in Java

Continue Statement in Java comes under the category of branching statements. The other branching statements are break and return statements. Continue is one of the 51 keywords in java. Keywords in java are also known as reserved words that have a specific purpose. These keywords are not supposed to be used as variable names, method names, class names. The purpose of writing a continue statement in java code is to skip the current iteration of a loop, say for, while and do-while. The control is handled mostly to the same loop (if not broken) or passed to the next statement of the code (if the current loop is broken).

The continue statement serves the same purpose in case other programming languages such as C and C++. It is also a keyword in C and C++. The continue statement is just opposite to the break statement; if a break statement is encountered, it automatically breaks the loop. Whereas the return statement completely exits from the program. Both return and break are reserved keywords in the case of C, C++, and Java. Neither of them should be used for naming a variable, method or class.

Syntax:

            for (i =0; i<max; i++) // for loop is a sample loop, max is the maximum count at which the loop breaks { <Statements> //code statements If (done with this iteration) // if this condition validates to true the continue statement is executed { Continue; // statement itself } <Statements> // code statements }          

Examples of Continue Statement in Java

Below are some examples of the statement in java:

Example #1

Usage of Continue Statement with for loop.

Code:

            public class DemoContinueUsingFor { public static void main(String[] args){ for(int p=0;p<6;p++){ if(p==3){ continue; } System.out.print(p+" "); } } }          

Output :

continue statement 1

Explanation:

  • Here in the loop, 'p' runs from 0 to 5. All the values of p are printed except 3 because as soon as p becomes 3, the if condition becomes true, and the continue statement is executed, which skips the print statement. Thus 3 is not seen in the output.
  • Here instead of system.out.print, if one uses the system.out.println then the output can be seen in the vertical form as shown below.

Output:

continue statement 2

Example #2

Usage of Continue Statement with the while loop.

Code:

            public class DemoContinueUsingWhile { public static void main(String[] args){ int max = 0; while(max <= 10){ if(max == 6){ max++; continue; } System.out.print(max+" "); max++; } } }          

Output :

continue statement 3

Explanation:

  • Here in the above code, the max value is initialized with 0. In the while loop, the max value is initially checked, and if the condition is met, only then the further code is executed, and at the end of the code, the corresponding max value is printed. Here in this example, all the max values are printed except for 6 because as soon as the max becomes 6, the "if" condition validates to true, and the corresponding statements are executed. As soon as the continue statement is executed, it skips the execution of further statements like print and max++. Thus completely, it ignores printing the value of 6.
  • Here instead of system.out.print, if one uses the system.out.println then the output can be seen in the vertical form.
  • In the above example, what happens if max++ is written after the continue statement?

Code:

            public class DemoContinueUsingWhile { public static void main(String[] args){ int max = 0; while(max <= 10){ if(max == 6){ continue; max++; // Here the max ++ is written after continue statement } System.out.println(max+" "); } } }          

Explanation:

  • The piece of code written next after the "continue" statement is considered unreachable code by the java compiler because the whole purpose of a continue statement itself is to ignore/skip the lines followed by it. The above code proves to be a classic example that explains the mere existence of the continue statement.
  • The following error is thrown by the java compiler as soon as one writes code immediately after the continue statement.

Output:

output 4

Example #3

Usage of Continue Statement with a do-while loop.

Code:

            public class DemoContinueUsingDoWhile { public static void main(String[] args) { int k=10; do { if (k==6) { k--; continue; } System.out.print(k+ " "); k--; } while(k>0); } }          

Output :

output 5

Explanation:

  • Here in the above, code k is initialized to 10; as mentioned earlier, in the case of the do-while loop initially, the code is executed at least once before the condition is tested. Just following the above theory, the code starts executing and if the condition is checked, whether k is equal to 6 or not.
  • If the condition is met, then the code in the "if" block gets executed, here first the k is decrement as soon as the continue statement is encountered, it skips the rest of the code, and the control flows back to the do-while loop but before that the condition in a while is checked. This is the reason why 6 is not printed in the output.

Conclusion

The above article explains the purpose of the continue statement; the three examples provided clearly depict the usage in a real-time scenario. For, while and do-while are considered examples, and the usage of the continue statement is explained on their basis. Just like continue, there are 2 more statements called break and return, which has their own purpose and applications in java enterprise applications.

Recommended Articles

This is a guide to the Continue Statement in Java. Here we discuss an introduction to continue statement in java and top examples of continue statement in Java. You can also go through our other related articles to learn more-

  1. Conditional Statements in JavaScript
  2. Case Statement in JavaScript
  3. Switch Statement in JavaScript
  4. Python Break Statement

beckethestray.blogspot.com

Source: https://www.educba.com/continue-statement-in-java/