What Condition Does While(True) Test? When Is It True and False?

I do no understand why anybody would use

while(true) {
   //do something
}

instead of

boolean condition = true;
while(condition == true) {
   //do something
}

The latter is super easy to understand, whilst the former is not.

So, what condition does while(true) check? When is while(true) true, and when is it false?

4

9 Answers

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.

6

Though we never know when we encounter a situation where we need it. We can also have infinite for loop.

for(;;) {//Code here}

Here's a real world example. The while loop will iterate while the user input can't be parsed to an int. When the user input is parsed to int the loop exits by returning the number entered by the user.

private int getNumber(String message) {
    while (true) {  
        System.out.print(message);
        String userInput = scanner.nextLine();
        try {
            return Integer.parseInt(userInput);
        } catch (Exception ignored) {
            System.out.printf("%s isn't a number!%n", userInput);
        }
    }
}

condition == true is also going to return a boolean which is 'true'.So using that directly instead of all that.

while(true) loop will of course always iterate. You've to manually break out of it using break, or System.exit(), or may be return.

while(condition == true) will be true while condition is true. You can make that false by setting condition = false.

I would never ever use while(condition == true) at least. Instead just use while (condition). That would suffice.

while(true) is always true. Loop statements are executed all the time. If you have to break the loop, we have to use break statement.

while(true) is used to for infinite loops. They will loop forever because true is ALWAYS true They are generally used when you have to do something until a certain condition is met. You then exit with the break statement

while(true) {
  //attempt to satisfy some condition

  if (satisfied) {
    break;
  }
}

I use While(true) when i want to run a loop that does not have a specific case to stop. Of course you can declare some boolean condition, but in my case I want to make my code shorter.

For example: I create a Id number, and I want to check that Id in my database first to see whether it is existing or not. If yes, i create a new id and check again, we may create the existing Id again and again. I use this loop to make sure that the Id number is absolutely unique. If no, I break the loop.

    int myNewId = createNewId();

    while(true) {
       if (existingId(newId)) {
         myNewId = createNewId();
       }
       else break;
    }
1
  1. List item

while(True): statement if(condition): break This is only work in python In java you can write like that for same purpose

while(1==1) { Ststements... }

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest