All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Continue Statement In Java

Last Updated : Mar 11, 2024

Continue Statement In Java

In this article we will show you the solution of continue statement in java, when you need to jump to the next iteration of the loop immediately in the loop control structure, you use the continue statement.

In either case, it can be used with the for loop or with the while loop. In Java, the loop is continued using the continue statement.

As long as the specified condition is met, the program will continue flowing and skip the remaining code.

An inner loop is only continued when it is an inner loop. The Java continue statement can be used with all types of loops, including for loops, while loops, and do-while loops.

In some cases, skipping some statements or terminating a loop might be necessary when working with loops.

A break statement and a continue statement are used in such instances.

Check out the Java break statement for more information. Let's examine the continuation statement in this section. We will now discuss the idea of how to create continue statement in java with an example.

Step By Step Guide On Continue Statement In Java :-

import java.util.Scanner;
class TC{
  public static void main(String[] args) {
    Double number, sum = 0.0;
    // create an object of Scanner
    Scanner input = new Scanner(System.in);
    for (int i = 1; i < 6; ++i) {
      System.out.print("Enter number " + i + " : ");
      // takes input from the user
      number = input.nextDouble();
      // if number is negative
      // continue statement is executed
      if (number <= 0.0) {
        continue;
      }
      sum += number;
    }
    System.out.println("Sum = " + sum);
    input.close();
  }
}
  1. In this step, the class 'TC' is created.
  2. It is defined in the TC class where the main method is located.
  3. In this case, two variables of type Double are declared - 'number' and 'sum', with 'sum' being initialized to 0.0.
  4. To assign 'input' to the variable Scanner, an object of the class is created.
  5. This is done by launching a 'for' loop with 'i' starting at 1 and ending at 5.
  6. As the loop iterates, the user is prompted to enter the 'ith' number.
  7. A variable named 'number' is assigned to the input provided by the user.
  8. By checking whether 'number' is less than or equal to 0, the 'if' statement is executed. The loop moves on to the next iteration if that is the case since the 'continue' statement is executed.
  9. In this case, 'number's' value is added to 'sum' if it is positive.
  10. After the loop completes, a console command is issued to print the sum.
  11. A resource-freeing operation is performed on the 'input' object.

Conclusion :-

As a result, we have successfully learned how to create a continue statement in java with an example.

In a loop, we can use the continue statement to stop executing it at the current iteration.

In addition to using the continue statement within loops, it can also be used within nested loops.

In case it is necessary, we can label it. In the absence of a label, the continue Java statement skips the current iteration of the loop that it is called in.

I hope this article on continue statement in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪