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 } System.out.println(word); count++; // Increment count } } }
- As you can see, we have written some Java code that demonstrates how to use a while loop.
- 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.
- Then, we create a class called WhileLoopExample. We have our main method in this class, which is where our Java programme starts.
- 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.
- We then begin a while loop with the condition count = 10, which causes the loop to keep running until count is greater than 10.
- 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.
- 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).
- We give the word variable the value "talkerscode" if count is only divisible by 3.
- We give the word variable the value "talkerstech" if count is only divisible by 5.
- 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.
- Using the System.out.println() command, we print the value of word to the console after finding its value.
- 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.
- 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.