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.
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.
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);
}
}
}
Videos
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.
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;)
{
}
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?