When is while(true) true, and when is it false?

It's always true, it's never false.

Some people use while(true) loops and then use break to exit them when a certain condition is true, but it's generally quite sloppy practice and not recommended. Without the use of break, return, System.exit(), or some other such mechanism, it will keep looping forever.

Answer from Michael Berry on Stack Overflow
🌐
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 while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.
Discussions

java - Are "while(true)" loops so bad? - Stack Overflow
And I don't think Java would be any worse for having a loop structure either. There's nothing inherently wrong with while(true) loops, but there is a tendency for teachers to discourage them. More on stackoverflow.com
🌐 stackoverflow.com
[java]How does the `while(true)` loop work?
While what's true? While true is true. Which it always is. What makes it go false? Nothing. true is always true. You need a different way to get out of this loop (break;) More on reddit.com
🌐 r/learnprogramming
8
1
October 22, 2015
while (true) - I don't get using a boolean as a condition... confused!
While you're at it, check out some resources Treehouse students have shared here. Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today. Start your free trial ... I don't get how 'true... More on teamtreehouse.com
🌐 teamtreehouse.com
6
April 2, 2020
[C#]Is while(true) considered bad practice?
Yes, while true's are a bad practice. better to set it up to a bool flag, like so: var isWaitingForCorrectNumber = true; while(isWaitingForCorrectNumber) { ... else { isWaitingForCorrectNumber = false; } } This way you explain to the reader what this code is doing. And, after all, code is meant to be interpreted by machines, but read and maintained by humans. More on reddit.com
🌐 r/learnprogramming
16
5
April 15, 2016
🌐
W3Schools
w3schools.com › java › java_while_loop.asp
Java 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 ... 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:
🌐
HWS
math.hws.edu › eck › cs124 › javanotes6 › c3 › s3.html
Javanotes 6.0, Section 3.3 -- The while and do..while Statements
It's called the break statement, which takes the form ... When the computer executes a break statement in a loop, it will immediately jump out of the loop. It then continues on to whatever follows the loop in the program. Consider for example: while (true) { // looks like it will run forever!
🌐
DataCamp
datacamp.com › doc › java › while
while Keyword in Java: Usage & Examples
If the condition is true, the loop continues; if false, the loop terminates. public class WhileExample { public static void main(String[] args) { int i = 0; while (i < 5) { System.out.println("i is: " + i); i++; } } } In this example, the variable i is initialized to 0. The while loop continues ...
🌐
DataCamp
datacamp.com › doc › java › java-while-loop
Java While Loop
This example demonstrates an infinite while loop, where the condition is always true. The break statement is used here to exit the loop, preventing it from running indefinitely. import java.util.Scanner; public class InputValidationExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number; System.out.println("Enter a number between 1 and 10:"); number = scanner.nextInt(); while (number < 1 || number > 10) { System.out.println("Invalid input.
🌐
Tutorialspoint
tutorialspoint.com › java › java_while_loop.htm
Java - while Loop
public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System.out.print("value of x : " + x ); x++; System.out.print("\n"); } } } value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 · In this example, we're showing the use of a while loop to print contents of an array.
Find elsewhere
🌐
Javatpoint
javatpoint.com › java-while-loop
Java While Loop - Javatpoint
November 20, 2017 - Java While Loop - Java while loop with java nested while loop, java while loop examples, java infinitive while loop example, difference between java for loop and while loop with concepts and examples.
🌐
Coderanch
coderanch.com › t › 592208 › java › Meaning-true
Meaning of while (true) (Beginning Java forum at Coderanch)
September 11, 2012 - It means while true is true. That is one way to code an endless loop. NB: This thread was split from this one. Please don’t ask new questions unrelated to the subject of an old thread. ... Akin Millone wrote:please i have a question about a line in chapter 2's guessgame code.
Top answer
1 of 16
231

I wouldn't say it's bad - but equally I would normally at least look for an alternative.

In situations where it's the first thing I write, I almost always at least try to refactor it into something clearer. Sometimes it can't be helped (or the alternative is to have a bool variable which does nothing meaningful except indicate the end of the loop, less clearly than a break statement) but it's worth at least trying.

As an example of where it's clearer to use break than a flag, consider:

while (true)
{
    doStuffNeededAtStartOfLoop();
    int input = getSomeInput();
    if (testCondition(input))
    {
        break;
    }
    actOnInput(input);
}

Now let's force it to use a flag:

boolean running = true;
while (running)
{
    doStuffNeededAtStartOfLoop();
    int input = getSomeInput();
    if (testCondition(input))
    {
        running = false;
    }
    else
    {
        actOnInput(input);
    }
}

I view the latter as more complicated to read: it's got an extra else block, the actOnInput is more indented, and if you're trying to work out what happens when testCondition returns true, you need to look carefully through the rest of the block to check that there isn't something after the else block which would occur whether running has been set to false or not.

The break statement communicates the intent more clearly, and lets the rest of the block get on with what it needs to do without worrying about earlier conditions.

Note that this is exactly the same sort of argument that people have about multiple return statements in a method. For example, if I can work out the result of a method within the first few lines (e.g. because some input is null, or empty, or zero) I find it clearer to return that answer directly than to have a variable to store the result, then a whole block of other code, and finally a return statement.

2 of 16
103

AFAIK nothing, really. Teachers are just allergic to goto, because they heard somewhere it's really bad. Otherwise you would just write:

bool guard = true;
do
{
   getInput();
   if (something)
     guard = false;
} while (guard)

Which is almost the same thing.

Maybe this is cleaner (because all the looping info is contained at the top of the block):

for (bool endLoop = false; !endLoop;)
{

}
🌐
Jenkov
jenkov.com › tutorials › java › while.html
Java while Loops
May 9, 2024 - The while loop enables your Java program to repeat a set of operations while a certain conditions is true. The Java while loop exist in two variations. The commonly used while loop and the less often do while version. I will cover both while loop versions in this text. Let us first look at the most commonly used variation of the Java while loop. Here is a simple Java while loop example:
🌐
Reddit
reddit.com › r/learnprogramming › [java]how does the `while(true)` loop work?
r/learnprogramming on Reddit: [java]How does the `while(true)` loop work?
October 22, 2015 -

I'm taking my first programming course. It's an accelerated Java class so we are skipping through stuff pretty quickly. Basically just getting exposed to one topic then moving to the next.

I've been keeping up fairly well, but encountered something in today's reading that has me stumped. It's this while(true) loop. I can't figure out how it is working.

while (true) {
    Socket socket = serverSocket.accept(); // Connect to a client 
    Thread thread = new ThreadClass(socket);
    thread.start();
}

We are discussing networking and the above loop is supposed to open a new thread for each connection.

How does it work? The while loop runs while true. While what's true? What makes it go false? I don't understand how this is working because every other while loop we have done has checked some condition.

...but while(true) ??? What's the condition that is true?

🌐
BeginnersBook
beginnersbook.com › 2015 › 03 › while-loop-in-java-with-examples
While loop in Java with examples
Hey, the notes were really helpful but i couldn’t understand the last example .Can anyone help me please? ... First of all….. The initialization done with i=0 Then goto while loop and check the condition i<4(i=0) It is true goto the loop body execute the looping statement i.e., args[0] Then increment the i value by 1 After incrementing again check the while loop condition …….
🌐
iO Flood
ioflood.com › blog › while-loop-java
While Loop Java: The Basics and Beyond
February 20, 2024 - This causes the loop to run indefinitely, which can cause your program to become unresponsive. Here’s an example of an infinite loop: while(true) { System.out.println("This will print forever!"); }
🌐
Team Treehouse
teamtreehouse.com › community › while-true-i-dont-get-using-a-boolean-as-a-condition-confused
while (true) - I don't get using a boolean as a condition... confused! (Example) | Treehouse Community
April 2, 2020 - Thanks in advance! ... The condition always needs to equal either true or false but usually you would do some kind of comparison to return true or false. while( "Yes" === "Yes" ) is true so it runs while( "No" === "Yes" ) is false so it doesn't run.
🌐
Programiz
programiz.com › java-programming › do-while-loop
Java while and do...while Loop
If the textExpression evaluates to true, the body of the loop inside the do statement is executed again. This process continues until the textExpression evaluates to false. Then the loop stops. ... Let's see the working of do...while loop. // Java Program to display numbers from 1 to 5 import java.util.Scanner; // Program to find the sum of natural numbers from 1 to 100.
🌐
Tutorialspoint
tutorialspoint.com › java › java_do_while_loop.htm
Java - do...while Loop
An infinite loop can also be implemented by writing "true" as the conditional statement using do do-while loop statement in Java. In this example, we're showing the infinite loop using while loop.
🌐
Study.com
study.com › programming languages › compiled languages
While Loops in Java: Example & Syntax - Lesson | Study.com
October 26, 2025 - The computer will continue to process the body of the loop until it reaches the last line. Then, it goes back to see if the condition is still true. You can have multiple conditions in a while statement. For example, you can have the loop run while one value is positive and another negative, like you can see playing out here:
🌐
Sololearn
sololearn.com › en › Discuss › 898551 › what-does-this-actually-mean-while-true-in-while-loop
What does this actually mean "while True:" in while loop? | Sololearn: Learn to code for FREE!
December 3, 2017 - If you have an infinite loop, you must (or should) have a break statement. Like so: while True: a += 1 This code will execute forever, adding 1 to a to infinity. Well, that's no good!
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-do-while-loop
Java do while loop | DigitalOcean
August 3, 2022 - We can create an infinite loop by passing boolean expression as true in the do while loop. Here is a simple do while java infinite loop example.