Test loops at the top or bottom? (while vs. do whi

2020-02-16 03:12发布

When I was taking CS in college (mid 80's), one of the ideas that was constantly repeated was to always write loops which test at the top (while...) rather than at the bottom (do ... while) of the loop. These notions were often backed up with references to studies which showed that loops which tested at the top were statistically much more likely to be correct than their bottom-testing counterparts.

As a result, I almost always write loops which test at the top. I don't do it if it introduces extra complexity in the code, but that case seems rare. I notice that some programmers tend to almost exclusively write loops that test at the bottom. When I see constructs like:

if (condition)
{
    do
    {
       ...
    } while (same condition);
}

or the inverse (if inside the while), it makes me wonder if they actually wrote it that way or if they added the if statement when they realized the loop didn't handle the null case.

I've done some googling, but haven't been able to find any literature on this subject. How do you guys (and gals) write your loops?

30条回答
太酷不给撩
2楼-- · 2020-02-16 04:03

You should first think of the test as part of the loop code. If the test logically belongs at the start of the loop processing, then it's a top-of-the-loop test. If the test logically belongs at the end of the loop (i.e. it decides if the loop should continue to run), then it's probably a bottom-of-the-loop test.

You will have to do something fancy if the test logically belongs in them middle. :-)

查看更多
Emotional °昔
3楼-- · 2020-02-16 04:04

Difference is that the do loop executes "do something" once and then checks the condition to see if it should repeat the "do something" while the while loop checks the condition before doing anything

查看更多
时光不老,我们不散
4楼-- · 2020-02-16 04:05

Yes, just like using for instead of while, or foreach instead of for improves readability. That said some circumstances need do while and I agree you would be silly to force those situations into a while loop.

查看更多
三岁会撩人
5楼-- · 2020-02-16 04:05

In a typical Discrete Structures class in computer science, it's an easy proof that there is an equivalence mapping between the two.

Stylistically, I prefer while (easy-expr) { } when easy-expr is known up front and ready to go, and the loop doesn't have a lot of repeated overhead/initialization. I prefer do { } while (somewhat-less-easy-expr); when there is more repeated overhead and the condition may not be quite so simple to set up ahead of time. If I write an infinite loop, I always use while (true) { }. I can't explain why, but I just don't like writing for (;;) { }.

查看更多
太酷不给撩
6楼-- · 2020-02-16 04:06

Use while loops when you want to test a condition before the first iteration of the loop.

Use do-while loops when you want to test a condition after running the first iteration of the loop.

For example, if you find yourself doing something like either of these snippets:

func();
while (condition) {
   func();
}

//or:

while (true){
    func();
    if (!condition) break;
}

You should rewrite it as:

do{
    func();
} while(condition);
查看更多
萌系小妹纸
7楼-- · 2020-02-16 04:06

It really depends there are situations when you want to test at the top, others when you want to test at the bottom, and still others when you want to test in the middle.

However the example given seems absurd. If you are going to test at the top, don't use an if statement and test at the bottom, just use a while statement, that's what it is made for.

查看更多
登录 后发表回答