🌐
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 ...
🌐
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.
Discussions

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
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
March 29, 2016
Why don’t we see more use of the “do...while” loop?

do while is primarily used when you want to go through one iteration of the loop body. while do implies you might not execute the body at all. I wouldn't pick one over the other based on perceived efficiency. I would base it primarily on whether you know you'll do one iteration or not.

More on reddit.com
🌐 r/learnprogramming
31
5
August 1, 2018
Do while loop
The problem is not from your loops. The problem is how you combine .nextInt() and .nextLine(). See The Scanner class and its caveats from the r/javahelp wiki More on reddit.com
🌐 r/learnjava
12
2
April 1, 2022
People also ask

What are looping statements in Java
divLooping statements in Java are constructs that allow you to repeatedly execute a block of code They help in automating repetitive tasks by iterating through a set of instructions as long as a certain condition is metdiv
🌐
scholarhat.com
scholarhat.com › home
Loops in java - For, While, Do-While Loop in Java
Can you nest loops in Java
divYes you can nest loops in Java This means placing one loop inside the body of another loop It is often used for more complex iteration scenariosdiv
🌐
scholarhat.com
scholarhat.com › home
Loops in java - For, While, Do-While Loop in Java
What are the types of looping statements in Java
divbrJava supports three types of looping statementsbrbrdivullistrongforstrong looplilistrongwhilestrong looplilistrongdowhilestrong loopliul
🌐
scholarhat.com
scholarhat.com › home
Loops in java - For, While, Do-While Loop in Java
🌐
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   3 weeks ago
🌐
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
🌐
DataCamp
datacamp.com › doc › java › while
while Keyword in Java: Usage & Examples
The while keyword in Java is used to create a loop that executes a block of code as long as a specified condition is true.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-while-loop-with-examples
Java while Loop - GeeksforGeeks
Control enters the while loop. The condition is tested. If true, execute the body of the loop. If false, exit the loop. After executing the body, update the loop variable. Repeat from step-2 until the condition is false. Below are the examples of Java while loop that demonstrates repeating actions and performing calculations.
Published   3 weeks ago
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.
🌐
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:
🌐
ScholarHat
scholarhat.com › home
Loops in java - For, While, Do-While Loop in Java
It iterates over the values of i (1 and 2) and j (1 to 10), outputting the result of i * j after each iteration, using two nested do...while loops. A nested while loop and for loop in Java involves placing a while loop inside a for loop or vice versa. For each iteration of the outer loop (either while or for), the inner loop will run completely.
Published   August 30, 2025
🌐
SitePoint
sitepoint.com › blog › java › java’s while and do-while loops in five minutes
Java's While and Do-While Loops in Five Minutes — SitePoint
November 5, 2024 - A while loop first checks the condition before executing the loop body. If the condition is false at the start, the loop body will not execute even once. On the other hand, a do-while loop executes the loop body first and then checks the condition.
🌐
ScholarHat
scholarhat.com › home
do...while Loop in Java - Flowchart & Syntax (With Examples)
September 6, 2025 - Read More: Top 50 Java Interview Questions and Answers · The do...while loop in Java is also known as Exit Control Loop. It is used byJava Developerswhen they want a block of code to be executed at least once before checking the condition.
🌐
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
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.
🌐
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 ...
🌐
Reddit
reddit.com › r/learnprogramming › java: when to use a while loop?
r/learnprogramming on Reddit: Java: When to use a While Loop?
March 29, 2016 -

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 › do-while-loop-in-java-with-example
do-while loop in Java with example
do-while loop is similar to while loop, however there is a difference between them: In while loop, condition is evaluated before the execution of loop’s body but in do-while loop condition is evaluated after the execution of loop’s body.
🌐
IONOS
ionos.com › digital guide › websites › web development › while-loop java
How to use the while-loop in Java - IONOS
September 26, 2022 - The main dif­fer­ence is that the head-con­trolled while-loop checks the ter­mi­na­tion condition in advance, and the foot-con­trolled do-while-loop waits until the state­ments have been executed at least once. ... Here, the code is executed at least once and then the condition is checked af­ter­wards. You also have the option of nesting several while-loops in Java.
🌐
Jenkov
jenkov.com › tutorials › java › while.html
Java while Loops
May 9, 2024 - The main difference between the two while loop variations is exactly that the do while loop is always executed at least once before the while loop condition is tested. This is not the case with the normal while loop variation explained in the beginning of this text. The two variations of the Java ...
🌐
IONOS
ionos.com › digital guide › websites › web development › java do-while loop
How to use the Java do-while loop - IONOS
January 3, 2025 - The do-while loop is used in Java to execute and repeat an in­struc­tion until a defined ter­mi­na­tion condition is fulfilled (i.e., true). Similar loops exist in most pro­gram­ming languages and are used to execute certain blocks of code multiple times.