🌐
W3Schools
w3schools.com › java › java_while_loop.asp
Java While Loop
In the next chapter, you will learn about the do while loop, which always runs the code at least once before checking the condition. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-while-loop-with-examples
Java while Loop - GeeksforGeeks
Java while loop is a control flow statement used to execute the block of statements repeatedly until the given condition evaluates to false.
Published   1 week ago
Discussions

Java: When to use a While Loop?
Imagine you have a collection of 1,000,000 songs. You want to search for one song specifically, and return that song (or, if the song doesn't exist in the collection, tell you it wasn't found). A for or for-each loop could accomplish that, but it's going to loop 1,000,000 times even if the song is the very first record checked, which is a huge waste of time and power. If you write a while loop and then have it stop when a matching record is found, it runs exactly as much as is necessary and no more. Another example -- imagine a simple text-based console app that reads input from a user and then prints it out on the screen, but then closes when they type "quit". If you put the steps of your program - asking the user for input, assigning it to a variable, and then printing it out - in a for/for-each loop, how do you know how many times to run it? A while loop that stops running when the user types "quit" lets the user flexibly quit whenever they want, whether on 0 entries or 200. There's a lot of other applications but hopefully these are useful to conceptualize at least. More on reddit.com
🌐 r/learnprogramming
19
13
November 21, 2015
While Loops Java - Stack Overflow
I need to create an infinite loop based off a users response, however, I keep getting stuck after the while loop. To me it appears that I would need to just copy/paste my prior code since it would More on stackoverflow.com
🌐 stackoverflow.com
do while loop - Java Basic
Tanguy Muffat is having issues with: Hi guys, I would need you support. I'm stucked under challenge 2/3 where I'm being asked: "Now continually prompt the user in a do whil... More on teamtreehouse.com
🌐 teamtreehouse.com
2
November 20, 2017
When is it situational to use while-loop vs for-loop?
I’m sure there’s more to it than this but a while loop is better for when you don’t know how many times you want to perform an action and a for loop is for when you know how many times you’ll need to do something. I only just finished the Mooc course yesterday though so I’m no professional. More on reddit.com
🌐 r/learnjava
28
2
January 10, 2018
🌐
W3Schools
w3schools.com › java › java_while_loop_do.asp
Java Do/While Loop
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The do/while loop is a variant of the while loop. This loop will execute ...
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › while.html
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once, as shown in the following DoWhileDemo program: class DoWhileDemo { public static void main(String[] args){ int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-do-while-loop-with-examples
Java Do While Loop - GeeksforGeeks
The loop continues until the condition c <= 5 becomes false, after which the loop terminates. ... Note: The test_expression in a do-while loop must evaluate to a boolean value; otherwise, a compile-time error occurs.
Published   1 week ago
🌐
Programiz
programiz.com › java-programming › do-while-loop
Java while and do...while Loop
Java while loop is used to run a specific code until a certain condition is met.
🌐
Tutorialspoint
tutorialspoint.com › java › java_while_loop.htm
Java - while Loop
Java while loop statement repeatedly executes a code block as long as a given condition is true. The while loop is an entry control loop, where conditions are checked before executing the loop's body.
🌐
DataCamp
datacamp.com › doc › java › java-while-loop
Java While Loop
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming ... The while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The loop continues to execute as long as the ...
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-do-while-loop
Java do while loop | DigitalOcean
August 3, 2022 - The do-while loop in Java is similar to while loop except that the condition is checked after the statements are executed, so do while loop guarantees the loop execution at least once.
🌐
GeeksforGeeks
geeksforgeeks.org › java › loops-in-java
Java Loops - GeeksforGeeks
The do-while loop ensures that the code block executes at least once before checking the condition. Example: The below Java program demonstrates a do-while loop that prints numbers from 0 to 10 in a single line.
Published   August 10, 2025
🌐
Tutorialspoint
tutorialspoint.com › java › java_do_while_loop.htm
Java - do...while Loop
Java do while loop is similar to a while loop, except that a do while loop is guaranteed to execute at least one time. The do-while loop is an exit control loop where the condition is checked after executing the loop's body.
🌐
Reddit
reddit.com › r/learnprogramming › java: when to use a while loop?
r/learnprogramming on Reddit: Java: When to use a While Loop?
November 21, 2015 -

My Intro to programming class has covered how to use both for and while loops. As for the code I am good to go with it but I am having a little trouble understanding them.

When would I use a while loop? Why would that be the better choice for the loop over a for loop?

Though I know how to code them I do not quite have a full comprehension on determining which to use and why one would be better than the other.

Any examples and/or like laymen's break down would be much appreciated!

Top answer
1 of 5
11
Imagine you have a collection of 1,000,000 songs. You want to search for one song specifically, and return that song (or, if the song doesn't exist in the collection, tell you it wasn't found). A for or for-each loop could accomplish that, but it's going to loop 1,000,000 times even if the song is the very first record checked, which is a huge waste of time and power. If you write a while loop and then have it stop when a matching record is found, it runs exactly as much as is necessary and no more. Another example -- imagine a simple text-based console app that reads input from a user and then prints it out on the screen, but then closes when they type "quit". If you put the steps of your program - asking the user for input, assigning it to a variable, and then printing it out - in a for/for-each loop, how do you know how many times to run it? A while loop that stops running when the user types "quit" lets the user flexibly quit whenever they want, whether on 0 entries or 200. There's a lot of other applications but hopefully these are useful to conceptualize at least.
2 of 5
10
There's actually 4 types of loops in Java. A normal for loop (for(int i = 0; etc), a while loop, a do-while loop and a for-each (or enhanced for loop). Examples: For loop: for(int i = 0;i < someNumber;i++) { //Do something } While loop: while(expression) { //Do something } Do-while: do { //Do something } while(expression); Enhanced for: for(Element e : collectionOfElements) { //Do something with E. } In essence they all do the exact same thing: repeat the inner block based on a condition. When and how the condition is checked differs between them and you use each depending on what you need to do. A very rough guideline is: For: when you know beforehand you have to do something N number of times For-each: when you need to iterate over a collection, iterable or array While: when you need to loop an unknown number of times and need to check the condition to continue before the code block. Do-while: when you need to loop an unknown number of times and need to check the condition after the code block. Also keep in mind that break (break out of the loop) and continue (skip the rest of the code and go directly to the next iteration) are all control statements that work for all loops. So it's very possible to use a while as a for loop or vice versa.
🌐
BeginnersBook
beginnersbook.com › 2015 › 03 › while-loop-in-java-with-examples
While loop in Java with examples
Fourth iteration: value of i is 3, fourth element of the array represented by arr[3] is printed. After fourth iteration: value of i is 4, the condition i<4 returns false so the loop ends and the code inside body of while loop doesn’t execute. Practice the following java programs related to while loop:
🌐
IONOS
ionos.com › digital guide › websites › web development › while-loop java
How to use the while-loop in Java - IONOS
September 26, 2022 - The ter­mi­na­tion condition works after the Boolean. This means that the while-loop in Java executes the stored state­ments as long as the condition specified in the header is true. If the ter­mi­na­tion condition changes to “false” for the first time, the loop is ter­mi­nat­ed.
🌐
Jenkov
jenkov.com › tutorials › java › while.html
Java while Loops
May 9, 2024 - I mostly used the first while loop variation, but there are situations where I have used the second variation. Java contains a continue command which can be used inside Java while (and for) loops. The continue command is placed inside the body of the while loop.
🌐
Vaia
vaia.com › java while loop
Java While Loop: Examples & Iterations | Vaia
November 14, 2023 - A Java while loop repeatedly executes a block of code as long as a given condition evaluates to true, making it ideal for situations where the number of iterations isn't predetermined. This loop syntax starts with the "while" keyword, followed by the condition within parentheses and the code ...
🌐
Study.com
study.com › programming languages › compiled languages
While Loops in Java: Example & Syntax - Lesson | Study.com
October 26, 2025 - This lesson has provided the syntax for the Java while statement, including some code examples. A while loop will execute commands as long as a certain condition is true. If you do not know when the condition will be true, this type of loop ...
🌐
Stack Overflow
stackoverflow.com › questions › 48492214 › while-loops-java
While Loops Java - Stack Overflow
You are a genius!"); correctQuestions++; overallQuestions++; } else System.out.print("Sorry, looks like you'll have to try again"); while(number1 * number2 == answer ^ number1 * number2 != answer) System.out.print("Would you like to try another question ?
Top answer
1 of 2
3
Hi Tanguy, I have seen this problem come up a lot. You do not actually need a boolean to run your do while loop, and you can simplify it a lot by having the loop depend on the response directly. java // I have initialized a java.io.Console for you. It is in a variable named console. String response; do{ response = console.readLine("Do you understand do while loops?"); }while(response.equalsIgnoreCase("no")); You may also include your print statement that you included using if(response.equalsIgnoreCase("no")). HOWEVER, with all that being said, what you did actually works fine! You just made a typo in your code. You wrote invalidReponse not invalidReSponse, as declared outside your loop. You can see these errors easily if you check the compiler error (You would see a symbol not found error point to that variable) Hope that helps!
2 of 2
0
I think i'm very unfamiliar with this kind of Java syntax. Using the console.printf() instead of System.out.println() and console.readLine() instead of scanner.nextLine(). I still don't understand why Tanguy Muffat's program works after fixing the syntax error. I don't understand how having the [while(response.equalsIgnoreCase("no"));] at the bottom works. Normally i would expect it to look more like : do{ while(response.equalsIgnoreCase("no")) { console.printf("Sorry you cannot continue the training. Train harder to understand the concept. \n"); }} I would think that a while condition with no brackets under it and nothing inside of it wouldn't work. In Tanguy Muffat's program it looks like the while loop is completely outside of the do loop.
🌐
ScholarHat
scholarhat.com › home
Looping Statements in Java - For, While, Do-While Loop in ...
An infinite while loop in Java happens when the condition in your while loop is always true. For example, using while(true) creates an endless loop. You can stop it with a break statement or by fixing the condition.
Published   August 30, 2025