blog posts

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?

C: \ Users \ Mr \ Desktop \ how-continue-statement-works_0.jpg

Example 1: continue command in Java

  1. class Test {
  2. public static void main (String [] args) {
  3. for (int i = 1; i <= 10; ++ i) {
  4. if (i> 4 && i <9) {
  5. continue;
  6. }
  7. System.out.println (i);
  8. }
  9. }
  10. }

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.

  1. import java.util.Scanner;
  2. class AssignmentOperator {
  3. public static void main (String [] args) {
  4. Double number, sum = 0.0;
  5. Scanner input = new Scanner (System.in);
  6. for (int i = 1; i <6; ++ i) {
  7. System.out.print (“Enter a number:”);
  8. number = input.nextDouble ();
  9. if (number <= 0.0) {
  10. continue;
  11. }
  12. sum + = number;
  13. }
  14. System.out.println (“Sum =” + sum);
  15. }
  16. }

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.

C: \ Users \ Mr \ Desktop \ nested-while-loop-continue.jpg

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?

C: \ Users \ Mr \ Desktop \ labeled-Java-continue_0.jpg

Here is the label ID.

Example 3: continue label tagged

  1. class LabeledContinue {
  2. public static void main (String [] args) {
  3. label:
  4. for (int i = 1; i <6; ++ i) {
  5. for (int j = 1; j <5; ++ j) {
  6. if (i == 3 || j == 2)
  7. continue label;
  8. System.out.println (“i =” + i + “; j =” + j);
  9. }
  10. }
  11. }
  12. }

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.