开发者

When should I use a 'while loop'?

I just stumbled upon this question Are "while(true)" loops so bad?

They made me think what do I normally do.And to my surprise I realised that I have almost never used a while loop in my professional code(or work code) .

Front end frameworks e.g faces etc do not count.

So When should I use a 'while loop'? an开发者_如何学God How often do you use while loop?

It's is a real question please do not close as being subjective I really am after a concrete example.where it can not be replaced with a better alternate.


One place where I might use it is where you need to treat the first element of a sequence differently to the rest. That makes a foreach loop awkward, but a while loop works well:

Iterator<String> iterator = foo.iterator();

// Handle the first item specially
if (!iterator.hasNext()) {
    throw new SomeException("foo shouldn't be empty");
}
SomeResult result = new SomeResult(iterator.next());

// Now deal with the rest
while (iterator.hasNext())
{
    String item = iterator.next();
    result.addItem(item);
}

Also I use a while loop as one of the few places where I'll also include an assignment in a condition:

String line;
while ((line = reader.readLine()) != null)
{
    // Handle the line
}

or with an InputStream:

int bytesRead;
while ((bytesRead = input.read(buffer)) != -1)
{
    // Handle the buffer
}


java.util.Scanner scanner = //...

while(scanner.hasNextLine()) {
  String line = scanner.nextLine();
  //..do sth with the line
}

In fact every while loop can be replaced with for. But e.g. in the code above it would be less readable - and that's the point: use while when it fits better to the nature of the problem.


You should use it to loop while some condition holds true.


Simple never-stopping backend logic:

while (true) {
  consumeMessage();
}

Or also

for (;;) {
  consumeMessage();
}


You should use it when you dont know how many iterations will be needed. You only know that you want to do something while your condition is met. It could be itereated 2, 100, 0... times.


Of course you can always rewrite a while loop into a for loop, but often it is uglier, meaning that parts of the for (..;..;..) are left blank - mainly the initialization. Findbugs also gives a warning in this case: similar to "simple for loop detected, rewrite it as a while loop".

The main application of the while loop is that you do not need an initialization, or want to treat the first loop iteration (e.g. first element of an enumeration) specially, in which case you do the initialization beforehand, too.


Use it when you have a main loop in your code which you want to run until something changes.

When you dont need a counter, and when you dont need to iterate over a collection (because then you need a counter).

Using a for(;whatever;) is ugly code, thats where you have to use a while.

Also the variation, do ... while allows you to do something at least once and then possibly many times.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜