All TalkersCode Topics

Follow TalkersCode On Social Media

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

Continue In While Loop Java

Last Updated : Mar 11, 2024

Continue In While Loop Java

In this article we will show you the solution of continue in while loop java, loops are essential in programming because they govern the flow of execution. These enable a series of instructions to be repeated until particular conditions are met.

Java, a popular and capable programming language, has a variety of loop structures to meet a variety of needs.

In this article, we'll look at the "continue" command within a "while" loop in Java. We will learn how to efficiently use this statement to skip certain iterations and simplify our code execution.

The purpose of this tutorial is to fully understand the Java while loop continue command.

We'll look at its syntax, meaning, and practical applications. By the end of this session, you will have the knowledge and skills to use the "continue" statement to optimize and improve the efficiency of your code.

Step By Step Guide On Continue In While Loop Java :-

Let's examine a practical application of the "Continue" statement. We will create a program that sums all even numbers between 1 and 100, omitting multiples of 10.

We can improve the program's efficiency by using the "continue" command to skip iterations that fulfill our defined criterion.

public class ContinueWhileLoopExample {
    public static void main(String[] args) {
        int sum = 0;
        int num = 1;
        while (num <= 100) {
            if (num % 2 != 0) {
                num++;
                continue;
            }
            if (num % 10 == 0) {
                num++;
                continue;
            }
            sum += num;
            num++;
        }
        System.out.println("The sum of even numbers between 1 and 100, excluding multiples of 10, is: " + sum);
    }
}
  1. We set the initial values of two variables, 'sum' and 'num', to 0 and 1, respectively.
  2. To iterate through the integers from 1 to 100, we start a "while" loop with the condition 'num = 100'.
  3. Within the loop, we first determine whether 'num' is an odd number ('num% 2!= 0'). In that scenario, increase num by one and use the continue expression to skip the rest of the code in this iteration.
  4. We then determine whether 'num' is a multiple of 10 ('num% 10 == 0'). If it is, we increment 'num' by 1 again and use "continue" to skip the remaining code in the current iteration.
  5. If neither of the preceding requirements is met, we add the current value of 'num' to the variable 'sum' and increase 'num' by 1.
  6. The loop will continue until 'num' reaches 101, at which point it will end.
  7. Finally, we print the total of the even numbers ranging from 1 to 100 (excluding multiples of 10).

Conclusion :-

The "continue" statement within a "while" loop is a powerful Java tool for skipping some iterations based on defined conditions.

You become familiar with the "Continue" statement through this session.

We were able to calculate the sum of even numbers between 1 and 100, excluding multiples of 10, with increased code performance by successfully utilizing this expression.

Understanding and understanding the "continue" statement enables developers to design shorter, more efficient code.

By carefully adding "continue" statements, programmers can eliminate superfluous computations, resulting in improved program efficiency and readability.

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

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪