blog posts

Continue Command

Understanding the continue Command in Java — Explained Simply

This tutorial teaches you to use the continue Command in Java. The continue statement causes the current iteration to continue.

Suppose you are working with circles. Sometimes, you want to skip some commands inside or end the loop immediately without checking the condition.

In such cases, the terms break and continue are used. The continue statement skips the remainder of the current iteration of a loop (for, while, or 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 a for loop, the variable update statement executes 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?

How does the continue statement work?

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 its value is greater than four and less than nine, the continue statement is executed, preventing i (System.out.println) from being executed.

When running the program, the output will be equal to:

1

2

3

4

9

10

Example 2: The continue statement in Java

The following program calculates the sum of a maximum of 5 positive numbers that the user enters. 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.

If there are nested loops, continue jumps to the beginning of the inner loop.

Continue Command Tagged

The continue command we’ve discussed 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 skip executing commands inside the outer loop.

How does the continue tagged Command work?

How does the continue tagged Command work?

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 the code difficult to understand. If you have to use continue tagging, refine your code and try to write it differently to make it more readable.

FAQ

What does the continue statement do in Java?

When executed inside a loop, continue skips the remaining statements in the current iteration and immediately proceeds to the next iteration.

In which loops can I use continue?

You can use continue inside any Java loop: for, while, do-while, or enhanced for-each loops.

What is the difference between continue and break in Java loops?

continue skips only the current iteration and moves on to the next one, whereas break exits the loop entirely.