Learn The Continue Command In Java (In Quite Simple Language)
In this tutorial you will learn to use the continue command in Java. The continue statement prevents the current loop from repeating.
Suppose you are working with circles. Sometimes you want to skip some commands inside the loop or end the loop immediately without checking the condition.
In such cases, the terms break and continue are used.
The continue statement prevents the current iteration of a loop (for, while, and do… while).
When the continue command is executed, the program control jumps to the end of the loop. The condition that controls the loop is then evaluated. In the case of the for loop, the variable update statement is executed before the condition is evaluated.
Most often used with decision structures (if… else).
The syntax of the continue command is as follows:
continue;
How does the continue statement work?
Example 1: continue command in Java
- class Test {
- public static void main (String [] args) {
- for (int i = 1; i <= 10; ++ i) {
- if (i> 4 && i <9) {
- continue;
- }
- System.out.println (i);
- }
- }
- }
When the value of i is greater than 4 and less than 9, the continue statement is executed and prevents i (System.out.println) from being executed.
When running the program, the output will be equal to:
1
۲
3
4
9
10
Example 2: The continue statement in Java
The following program calculates the sum of a maximum of 5 positive numbers entered by the user. If the user enters a negative or zero number, it will be skipped.
The Scanner object is used to get input from the user.
- import java.util.Scanner;
- class AssignmentOperator {
- public static void main (String [] args) {
- Double number, sum = 0.0;
- Scanner input = new Scanner (System.in);
- for (int i = 1; i <6; ++ i) {
- System.out.print (“Enter a number:”);
- number = input.nextDouble ();
- if (number <= 0.0) {
- continue;
- }
- sum + = number;
- }
- System.out.println (“Sum =” + sum);
- }
- }
Output:
Enter a number: 2.2
Enter a number: 5.6
Enter a number: 0
Enter a number: -2.4
Enter a number: -3
Sum = 7.8
If there are nested loops, continue jumps to the beginning of the inner loop.
Continue command tagged
The continue command we’ve talked about so far is unlabeled, which prevents the execution of the remaining commands of the innermost loops for, while and do… while.
Another form of the continue command is the tagged form, which can be used to skip executing commands inside the outer loop.
How does the continue tagged command work?
Here is the label ID.
Example 3: continue label tagged
- class LabeledContinue {
- public static void main (String [] args) {
- label:
- for (int i = 1; i <6; ++ i) {
- for (int j = 1; j <5; ++ j) {
- if (i == 3 || j == 2)
- continue label;
- System.out.println (“i =” + i + “; j =” + j);
- }
- }
- }
- }
Output
i = 1; j = 1
i = 2; j = 1
i = 4; j = 1
i = 5; j = 1
Usually continue to use labeled continue because it makes it difficult to understand the code. If you have to use continue tagging, refine your code and try to write it differently to make it more readable.