🌐
W3Schools
w3schools.com › java › java_while_loop.asp
Java While Loop
Loops can execute a block of code as long as a specified condition is true. Loops are handy because they save time, reduce errors, and they make code more readable. The while loop repeats a block of code as long as the specified condition is true:
🌐
Oracle
java.com › en › download › uninstalltool.jsp
Java Uninstall Tool
» What is Java » Remove older versions » Disable Java » Error messages » Troubleshoot Java » Other help · If you want to download Java for another computer or Operating System, click the link below. All Java Downloads
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
March 29, 2016
How do i do while loops in Java? - Stack Overflow
I'm new to Java and wanted to learn a few basics about coding. I got stuck on the while loop! It seems like my compiler is just not executing the loop and I suspect it has something to do with my More on stackoverflow.com
🌐 stackoverflow.com
java basics while loops
joseph bonsignore is having issues with: I'm struggling with every question in the looping until the value passes part of java basics. Please help! More on teamtreehouse.com
🌐 teamtreehouse.com
1
April 14, 2017
While loop condition in java - Stack Overflow
I am working my way through "Java - A beginners guide by Herbert Schildt". I hope this question is not way too ridiculous. It is about a while loop condition, where the while loop is located inside... More on stackoverflow.com
🌐 stackoverflow.com
🌐
DEV Community
dev.to › devaspires › understanding-loops-in-java-while-loop-3e3g
While loops in Java - DEV Community
May 22, 2022 - While loop is among the types of loops associated with Java, and it's been regarded as the most basic type of loop in Java, and therefore, can be utilize for several purposes within your program. Just like the for loop, a while loop can be used to perform some operations on the basis of a condition.
🌐
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   2 weeks ago
🌐
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)
See Java Language Changes for a ... and removed or deprecated options for all JDK releases. The while statement continually executes a block of statements while a particular condition is true....
🌐
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.
🌐
iO Flood
ioflood.com › blog › while-loop-java
While Loop Java: The Basics and Beyond
February 20, 2024 - The while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given condition. The condition expression must be a boolean expression. The loop executes the block of code as long as the condition is true.
Find elsewhere
🌐
Talent Battle
talentbattle.in › learn-java-for-free › whileloop-in-java
While Loop in Java | Free Java Course - Talent Battle
August 24, 2024 - In Java programming, the while loop provides a mechanism to execute a block of code repeatedly as long as a specified condition remains true. It's particularly useful when the number of iterations is unknown beforehand.
🌐
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 ...
🌐
DataCamp
datacamp.com › doc › java › while
while Keyword in Java: Usage & Examples
In this example, the variable i is initialized to 0. The while loop continues to execute as long as i is less than 5. The value of i is printed and incremented in each iteration. import java.util.Scanner; public class WhileInputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = ""; while (!input.equals("exit")) { System.out.println("Enter text (type 'exit' to quit): "); input = scanner.nextLine(); } scanner.close(); } }
🌐
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.
🌐
ScholarHat
scholarhat.com › home
Loops in java - For, While, Do-While Loop in Java
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
Top answer
1 of 2
1

In a while cycle you must put a condition. While that condition is true the cycle loop continue. An easy example should be:

int a = 0;
while(a<5){
    System.out.println(a);
    a++;
}

This cycle will be executed untill a is lower than 5. Each time that this cycle is executed the variable increase. The output will be:

0
1
2
3
4

So you should place a condition in your while loop that should not be always true (that cause an infinite loop) or always false (that will never execute your code).

2 of 2
1

First of all, don´t be discouraged because you might not understand everything at the first instance, this all comes with time and a lot of practice. I will do my best to explain the while loop for you.

A while-loop executes everything inside of the curly braces over and over again until the condition inside the parentheses evaluates to false. If your condition is always true, the loop will continue forever. Here is an example:

public static void main(String[] args) {
     int i = 0;
     while(i < 5) {
        System.out.println("Looping...");
        i = i + 1;
     }
}

This loop would print "Looping..." five times, since i isn´t smaller than 5 after 5 iterations.

You can basically put any expression inside the parentheses that evaluates to a boolean. I would love to get more specific, but for that, it would be nice to know where your variables ja and nein are defined.

I would recommend you to go and practice the basics again, here is a good example that I used a while ago:

http://openbook.rheinwerk-verlag.de/javainsel/

🌐
DigitalOcean
digitalocean.com › community › tutorials › java-do-while-loop
Java do while loop | DigitalOcean
August 3, 2022 - Java do-while loop is used to execute a block of statements continuously until the given 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.
🌐
Jenkov
jenkov.com › tutorials › java › while.html
Java while Loops
May 9, 2024 - This tutorial explains how Java's while loop works.
🌐
IONOS
ionos.com › digital guide › websites › web development › while-loop java
How to use the while-loop in Java - IONOS
September 26, 2022 - The structure of a while-loop in Java doesn’t usually change too much. While the syntax remains the same, only the state­ments and the ter­mi­na­tion condition change. First the term “while” in­tro­duces the loop, then the ter­mi­na­tion condition follows in brackets and finally one or more state­ments in curly brackets.
🌐
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