All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Do A While Loop In Java

Last Updated : Mar 11, 2024

How To Do A While Loop In Java

In this article we will show you the solution of how to do a while loop in java, while loops are control flow statements in Java that let you continually run a piece of code as long as a specific condition holds true.

The while loop is made up of the word "while" in parentheses, a Boolean expression, and a block of code enclosed in curly brackets.

Start by analysing the condition before using a while loop in Java.

The while loop's corresponding code block is run if the condition is satisfied.

Then, if the condition is still true, it is reevaluated, and the code block is run once more.

The while loop is repeated until the condition is satisfied, at which point the programme goes on to the following statement. Now we'll discuss the idea of a while loop in Java.

Step By Step Guide On How To Do A While Loop In Java :-

While loops are very handy when you need to repeatedly perform a task until a specified condition is met or when you need to iterate over a set of data without knowing the precise number of iterations in advance.

import java.lang.String;
public class WhileLoopExample {
    public static void main(String[] args) {
        int count = 1; // Starting count
        String word = ""; // Placeholder for the words
        while (count <= 10) {
            if (count % 3 == 0 && count % 5 == 0) {
                word = "talkersmoney";
            } else if (count % 3 == 0) {
                word = "talkerscode";
            } else if (count % 5 == 0) {
                word = "talkerstech";
            } else {
                word = String.valueOf(count); // Convert count to string
            count++; // Increment count
  1. As you can see, we have written some Java code that demonstrates how to use a while loop.
  2. The java.lang.String package is first imported in the code, albeit this package is already implicitly imported by default so we don't need to manually import it.
  3. Then, we create a class called WhileLoopExample. We have our main method in this class, which is where our Java programme starts.
  4. We declare and set the initial values of the variables count and word inside the main procedure. We set count's initial value to 1, which serves as the loop's starting count. We will utilise the word variable, which is an empty string, to store the words that will be printed.
  5. We then begin a while loop with the condition count = 10, which causes the loop to keep running until count is greater than 10.
  6. The word variable's value is depending on the count's current value in a number of conditional (if-else) statements found within the loop.
  7. We give the word variable the value "talkersmoney" if count is divisible by both 3 and 5 (i.e., if the remainder of count divided by 3 is 0 and the remaining of count divided by 5 is 0).
  8. We give the word variable the value "talkerscode" if count is only divisible by 3.
  9. We give the word variable the value "talkerstech" if count is only divisible by 5.
  10. We use String.valueOf(count) to give a string representation for the count value to the word variable if none of the aforementioned conditions are true.
  11. Using the System.out.println() command, we print the value of word to the console after finding its value.
  12. In order to ensure that the loop moves closer to the termination condition, we finish by using the count++ statement to increase the value of count by 1.
  13. The loop continues to run until count exceeds 10, at which point a string of text is written based on the if-else statements' circumstances.

Conclusion :-

As a result, we have successfully learnt how to use a while loop in Java.

We also learnt how to use if-else expressions inside of a loop to get a variable's value and print it appropriately. Until the condition was false, the loop kept running.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪