90

Bjarne Stroustrup (C++ creator) once said that he avoids "do/while" loops, and prefers to write the code in terms of a "while" loop instead. [See quote below.]

Since hearing this, I have found this to be true. What are your thoughts? Is there an example where a "do/while" is much cleaner and easier to understand than if you used a "while" instead?

In response to some of the answers: yes, I understand the technical difference between "do/while" and "while". This is a deeper question about readability and structuring code involving loops.

Let me ask another way: suppose you were forbidden from using "do/while" - is there a realistic example where this would give you no choice but to write unclean code using "while"?

From "The C++ Programming Language", 6.3.3:

In my experience, the do-statement is a source of errors and confusion. The reason is that its body is always executed once before the condition is evaluated. However, for the body to work correctly, something very much like the condition must hold even the first time through. More often than I would have guessed, I have found that condition not to hold as expected either when the program was first written and tested, or later after the code preceding it has been modified. I also prefer the condition "up front where I can see it." Consequently, I tend to avoid do-statements. -Bjarne

Avoiding the do/while loop is a recommendation included in the C++ Core Guidelines as ES.75, avoid do-statements.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Dustin Boswell
  • 6,114
  • 7
  • 28
  • 26
  • 3
    @brandaemon Code Review wasn't around yet in June '09. What is your point exactly? – Mast Jul 31 '15 at 23:16
  • 1
    @brandaemon That is a really bad recommendation... – Ismael Miguel Jul 31 '15 at 23:18
  • 3
    @brandaemon StackOverflow or Programmers.SE. Code Review is **only for fully working code** and also **must include code**. This question has no code what-so-ever. Which makes it off-topic on Code Review. – Ismael Miguel Jul 31 '15 at 23:20
  • That makes sense. I'll do that. – intcreator Jul 31 '15 at 23:21
  • 5
    @brandaemon please see [a guide to Code Review for Stack Overflow users](http://meta.codereview.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users). – Mathieu Guindon Jul 31 '15 at 23:21
  • 1
    @brandaemon I would take some time to read [this meta post](http://meta.programmers.stackexchange.com/a/7183/52929) which is pretty extensive on this subject. It provides a lot of insight as to what questions are on-topic on Programmers.StackExchange. Hope that helps, too! – enderland Jul 31 '15 at 23:50

19 Answers19

106

Yes I agree that do while loops can be rewritten to a while loop, however I disagree that always using a while loop is better. do while always get run at least once and that is a very useful property (most typical example being input checking (from keyboard))

#include <stdio.h>

int main() {
    char c;

    do {
        printf("enter a number");
        scanf("%c", &c);

    } while (c < '0' ||  c > '9'); 
}

This can of course be rewritten to a while loop, but this is usually viewed as a much more elegant solution.

David Božjak
  • 16,887
  • 18
  • 67
  • 98
  • 3
    Here's the "while" version: char c = -1; while (c < '0' || c > '9') { printf(...); scanf(...); } – Dustin Boswell Jun 15 '09 at 08:26
  • 52
    @Dustin, IMO your counter example is less readable because you use a magic number (-1). So in terms of "better" code (which is subjective, of course), I think that @Rekreativc's original code is better, since it's more readable to *human* eyes. – Ron Klein Jun 15 '09 at 10:48
  • @Dustin Boswell: As I've said in my answer, I agree that do while loops can be rewritten as while loops, however I posted an example that show why using a do while loops is sometimes better - you can avoid initialisation with magic numbers. – David Božjak Jun 15 '09 at 13:42
  • 9
    I'm not sure I'd call it a "magic" number, but in any case, you could initialize it as "char c = '0' - 1;" if that's clearer. Perhaps what you're really resisting is the fact that 'c' has an initial value at all? I agree it's a little weird, but on the other hand, a 'while' is nice because it states the continuation condition up front, so the reader understands the life-cycle of the loop right away. – Dustin Boswell Jun 15 '09 at 17:55
  • 6
    My objection is yes, in principle, that 'c' shouldn't have an observable initial value at all. That's why I'd like the do-while version better. (Although I think you have a point about real-world readability.) – mqp Jun 15 '09 at 18:59
  • 9
    @Dustin: The important downside of your rewrite is that you need to reserve one element (here, -1) from the domain of `char` as a special marker. Although that doesn't affect the logic here, in general it means you're no longer able to process a stream of arbitrary characters, since it may include characters having the value -1. – j_random_hacker Jan 15 '10 at 15:56
  • What if you wanted to respond to invalid input? Yes, it's a different problem but the point is do loops only have application in trivial, contrived examples. – CurtainDog Jun 09 '10 at 06:32
  • I would write it like this instead... while(true) { printf("enter a number"); scanf("%c", &c); if (c >= '0' || c <= '9') break; } I don't think I ever use do...while. – Johan Kotlinski Jun 09 '10 at 07:47
  • 2
    The signedness of `char` is implementation-defined, so using the magic number -1 isn't wise for more than one reason, it could be a potential bug. You shouldn't attempt to store a negative numbers inside unsigned variables. – Lundin Nov 13 '13 at 10:44
  • This looks like a perfect example of why to not use a do-while loop. The code never checks the state of the stream and is risking an infinite loop. You can get a hint of the problem by hand if you type in R2D2 for the number. I/O is dangerous if not checked for error states and a while loop should check the state of I/O for every iteration, including the first. – Mitch Oct 24 '18 at 15:38
38

do-while is a loop with a post-condition. You need it in cases when the loop body is to be executed at least once. This is necessary for code which needs some action before the loop condition can be sensibly evaluated. With while loop you would have to call the initialization code from two sites, with do-while you can only call it from one site.

Another example is when you already have a valid object when the first iteration is to be started, so you don't want to execute anything (loop condition evaluation included) before the first iteration starts. An example is with FindFirstFile/FindNextFile Win32 functions: you call FindFirstFile which either returns an error or a search handle to the first file, then you call FindNextFile until it returns an error.

Pseudocode:

Handle handle;
Params params;
if( ( handle = FindFirstFile( params ) ) != Error ) {
   do {
      process( params ); //process found file
   } while( ( handle = FindNextFile( params ) ) != Error ) );
}
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 8
    a pre-condition? if the condition is checked after one execution of the body isn't it a post-condition? :) –  Jun 15 '09 at 07:54
  • 8
    Here's a rewrite that I think is more readable (and has less nesting) Handle handle = FindFirstFile(params); while(handle != Error) { process(params); handle = FindNextFile(params); } – Dustin Boswell Jun 15 '09 at 08:11
  • Yes, in case you don't need elaborate error handling for each edge case. – sharptooth Jun 15 '09 at 08:27
  • Can't you avoid the if altoghether using a while loop here? while( ( handle = FindNextFile( params ) ) != Error ) ) { process( params ); } I mean, the condition in the if clause is exactly the same as the condition in the loop. – Juan Pablo Califano Jun 15 '09 at 18:40
  • 4
    @Dustin: it could also be done like this (and probably should be) `for(handle = FindFirstFile(params); handle != Error; handle = FindNextFile(params)) {}`. – falstro Dec 15 '09 at 13:48
  • @Dustin: However, if the code is identical for first and non-first iterations (e.g. FindFirstFile and FindNextFile are unified into a single function, but more commonly an expression not amenable to an encapsulated function), then you've duplicated it, leading to increased errors and decreased clarity. That situation calls for Knuth's loop-and-a-half, of which do-while is a special case. –  Feb 05 '10 at 14:25
  • I think this answer is better than the accepted answer, as you clearly say that you would have to do initialization from two sites, once before the while loop and once inside the while loop. This is indeed solved with a do-while. I found myself writing better and more readable code using a do-while when I encounter such a situation. You just have to declare the variable before the loop, but not initialize them. So, no duplication of code. For me this is the most useful property of do-while compared to while loops. – Timmos Jul 17 '13 at 11:30
  • This is a case where obviously a `for` loop is appropriate (as in @falstro's comment), I'm really surprised by the number of up-votes. – tobi_s Jul 08 '21 at 01:11
18

do { ... } while (0) is an important construct for making macros behave well.

Even if it's unimportant in real code (with which I don't necessarily agree), it's important for for fixing some of the deficiencies of the preprocessor.

Edit: I ran into a situation where do/while was much cleaner today in my own code. I was making a cross-platform abstraction of the paired LL/SC instructions. These need to be used in a loop, like so:

do
{
  oldvalue = LL (address);
  newvalue = oldvalue + 1;
} while (!SC (address, newvalue, oldvalue));

(Experts might realize that oldvalue is unused in an SC Implementation, but it's included so that this abstraction can be emulated with CAS.)

LL and SC are an excellent example of a situation where do/while is significantly cleaner than the equivalent while form:

oldvalue = LL (address);
newvalue = oldvalue + 1;
while (!SC (address, newvalue, oldvalue))
{
  oldvalue = LL (address);
  newvalue = oldvalue + 1;
}

For this reason I'm extremely disappointed in the fact that Google Go has opted to remove the do-while construct.

Dan Olson
  • 22,849
  • 4
  • 42
  • 56
  • Why is this needed for macros? Can you give an example? – Dustin Boswell Jun 15 '09 at 08:06
  • 5
    Take a look at . Preprocessor macros don't "deal with" C syntax, so clever hacks like this are necessary to prevent macros from barfing in, say, an `if` statement without braces. – Wesley Jun 15 '09 at 08:16
  • 1
    There are a number of reasons detailed around the web, but I couldn't find a comprehensive link in a couple of minutes of googling. The easiest example is that it makes multi-line macros behave as a single line, which can be important because the user of the macro may use it in an un-bracketed single-line for loop and if it's not wrapped correctly this usage will misbehave. – Dan Olson Jun 15 '09 at 08:17
  • I find do while's inside #defines as disgusting. It's one of the reasons the preprocessor has such a bad name. – Pod Jun 15 '09 at 08:29
  • 1
    You should clarify Pod, you just don't like it aesthetically, don't like the additional safety for some reason, or disagree that it's necessary? Help me understand. – Dan Olson Jun 15 '09 at 08:31
  • I think that it's necessary shows you how dangerous the pre-processor is. – Martin Beckett Jun 15 '09 at 14:28
8

The following common idiom seems very straightforward to me:

do {
    preliminary_work();
    value = get_value();
} while (not_valid(value));

The rewrite to avoid do seems to be:

value = make_invalid_value();
while (not_valid(value)) {
    preliminary_work();
    value = get_value();
}

That first line is used to make sure that the test always evaluates to true the first time. In other words, the test is always superfluous the first time. If this superfluous test wasn't there, one could also omit the initial assignment. This code gives the impression that it fights itself.

In cases such like these, the do construct is a very useful option.

Svante
  • 50,694
  • 11
  • 78
  • 122
7

It's useful for when you want to "do" something "until" a condition is satisfied.

It can be fudged in a while loop like this:

while(true)
{

    // .... code .....

    if(condition_satisfied) 
        break;
}
hasen
  • 161,647
  • 65
  • 194
  • 231
  • 1
    Actually, this is NOT equivalent to "do { ... code ... } while (condition)", if "... code ..." contains a 'continue' statement. The rule for 'continue' is to go to the bottom for a do/while. – Dustin Boswell Jun 21 '09 at 00:44
  • good point, however, I said "fudge", which implies it's kinda wrong – hasen Jun 21 '09 at 01:44
6

This is cleanest alternative to do-while that I have seen. It is the idiom recommended for Python which does not have a do-while loop.

One caveat is that you can not have a continue in the <setup code> since it would jump the break condition, but none of the examples that show the benefits of the do-while need a continue before the condition.

while (true) {
       <setup code>
       if (!condition) break;
       <loop body>
}

Here it is applied to some of the best examples of the do-while loops above.

while (true) {
    printf("enter a number");
    scanf("%c", &c);
    if (!(c < '0' ||  c > '9')) break;
} 

This next example is a case where the structure is more readable than a do-while since the condition is kept near the top as //get data is usually short yet the //process data portion may be lengthy.

while (true) {
    // get data 
    if (data == null) break;
    // process data
    // process it some more
    // have a lot of cases etc.
    // wow, we're almost done.
    // oops, just one more thing.
} 
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
dansalmo
  • 11,506
  • 5
  • 58
  • 53
6

First of all, I do agree that do-while is less readable than while.

But I'm amazed that after so many answers, nobody has considered why do-while even exists in the language. The reason is efficiency.

Lets say we have a do-while loop with N condition checks, where the outcome of the condition depends on the loop body. Then if we replace it with a while loop, we get N+1 condition checks instead, where the extra check is pointless. That's no big deal if the loop condition only contains a check of an integer value, but lets say that we have

something_t* x = NULL;

while( very_slowly_check_if_something_is_done(x) )
{
  set_something(x);
}

Then the function call in first lap of the loop is redundant: we already know that x isn't set to anything yet. So why execute some pointless overhead code?

I often use do-while for this very purpose when coding realtime embedded systems, where the code inside the condition is relatively slow (checking the response from some slow hardware peripheral).

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 2
    adding a check to the while `while (x == null || check(x)) . . .` addresses this issue – D.B. Fred Aug 03 '18 at 17:47
  • 3
    @D.B.Fred That still gives worse performance though, as you potentially add an additional branch to each lap in the loop. – Lundin Aug 06 '18 at 08:54
6

(Assuming you know the difference between the both)

Do/While is good for bootstrapping/pre-initializing code before your condition is checked and the while loop is run.

SuPra
  • 8,488
  • 4
  • 37
  • 30
6

In our coding conventions

  • if / while / ... conditions don't have side effects and
  • varibles must be initialized.

So we have almost never a do {} while(xx) Because:

int main() {
    char c;

    do {
        printf("enter a number");
        scanf("%c", &c);

    } while (c < '0' ||  c > '9'); 
}

is rewritten in:

int main() {
    char c(0);
    while (c < '0' ||  c > '9');  {
        printf("enter a number");
        scanf("%c", &c);
    } 
}

and

Handle handle;
Params params;
if( ( handle = FindFirstFile( params ) ) != Error ) {
   do {
      process( params ); //process found file
   } while( ( handle = FindNextFile( params ) ) != Error ) );
}

is rewritten in:

Params params(xxx);
Handle handle = FindFirstFile( params );
while( handle!=Error ) {
    process( params ); //process found file
    handle = FindNextFile( params );
}
TimW
  • 8,351
  • 1
  • 29
  • 33
  • 7
    See the bug in your first example? Maybe the do-while loop would have let you avoid that. :-) – Jouni K. Seppänen Jun 15 '09 at 09:01
  • 3
    I hate reading code with do...while loops. It's as if someone were giving you details about something you don't care about at all, then after 15 minutes of blabing, they tell you why you should care about it in the first place. Make the code readable. Tell me up front what the looping is for, and don't make me scroll for no reason. – Kieveli Jun 15 '09 at 12:27
  • @Kieveli - my thoughts as well. I think programmers are used to having conditions at the top, like it is with 'for' and 'if'. do/while is an oddity, really. – Dustin Boswell Jun 15 '09 at 18:01
  • 4
    Most programmers don't seem to have problems with conditions in the middle (`if (...) break;`), often even combined with `while(true)` resp. `for(;;)`. So what makes `do ... while`, where you at least know where to look for the condition, worse? – celtschk Jun 18 '13 at 21:24
  • @celtschk All of my former workplaces considered the `if (bCondition) break;` a bad design and encouraged the usage of **proper loop conditions**. Of course there are a _few_ cases where it can not be avoided but most of the times `break` and `continue` are signs of lazy coding. – mg30rg Oct 15 '14 at 12:45
5

It's all about readability.
More readable code leads to less headache in code maintenance, and better collaboration.
Other considerations (such as optimization) are, by far, less important in most cases.
I'll elaborate, since I got a comment here:
If you have a code snippet A that uses do { ... } while(), and it's more readable than its while() {...} equivalent B, then I'd vote for A. If you prefer B, since you see the loop condition "up front", and you think it's more readable (and thus maintainable, etc.) - then go right ahead, use B.
My point is: use the code that is more readable to your eyes (and to your colleagues'). The choice is subjective, of course.

Ron Klein
  • 9,178
  • 9
  • 55
  • 88
3

It is only personal choice in my opinion.

Most of the time, you can find a way to rewrite a do ... while loop to a while loop; but not necessarily always. Also it might make more logical sense to write a do while loop sometimes to fit the context you are in.

If you look above, the reply from TimW, it speaks for itself. The second one with Handle, especially is more messy in my opinion.

ϹοδεMεδιϲ
  • 2,790
  • 3
  • 33
  • 54
3

Read the Structured Program Theorem. A do{} while() can always be rewritten to while() do{}. Sequence, selection, and iteration are all that's ever needed.

Since whatever is contained in the loop body can always be encapsulated into a routine, the dirtiness of having to use while() do{} need never get worse than

LoopBody()
while(cond) {
    LoopBody()
}
  • The question is about what happens when "LoopBody" gets long (say 10 lines), and you don't want to create a function for it. Repeating those 10 lines is silly. But in many cases, the code can be restructured to avoid duplicating those 10 lines, but still using a "while". – Dustin Boswell Jun 15 '09 at 18:14
  • 1
    Of course, it's pretty bad for maintenance to have the same code twice. – Nosredna Jun 15 '09 at 18:14
  • Yes, certainly having both loop options enables easier, cleaner code. My point was that it has been proven (for decades) that both are not strictly necessary. –  Jun 15 '09 at 23:40
  • Another rewrite would be: `for (bool first=true; first || cond; first=false) { ... }`. This would not duplicate code, but introduce an additional variable. – celtschk Jun 18 '13 at 21:28
2

A do-while loop can always be rewritten as a while loop.

Whether to use only while loops, or while, do-while, and for-loops (or any combination thereof) depends largely on your taste for aesthetics and the conventions of the project you are working on.

Personally, I prefer while-loops because it simplifies reasoning about loop invariants IMHO.

As to whether there are situations where you do need do-while loops: Instead of

do
{
  loopBody();
} while (condition());

you can always

loopBody();
while(condition())
{
  loopBody();
}

so, no, you never need to use do-while if you cannot for some reason. (Of course this example violates DRY, but it's only a proof-of-concept. In my experience there is usually a way of transforming a do-while loop to a while loop and not to violate DRY in any concrete use case.)

"When in Rome, do as the Romans."

BTW: The quote you are looking for is maybe this one ([1], last paragraph of section 6.3.3):

From my experience, the do-statement is a source of error and confusion. The reason is that its body is always executed once before the condition is tested. For the correct functioning of the body, however, a similar condition to the final condition has to hold in the first run. More often than I expected I have found these conditions not to be true. This was the case both when I wrote the program in question from scratch and then tested it as well as after a change of the code. Additionally, I prefer the condition "up-front, where I can see it". I therefore tend to avoid do-statements.

(Note: This is my translation of the German edition. If you happen to own the English edition, feel free to edit the quote to match his original wording. Unfortunately, Addison-Wesley hates Google.)

[1] B. Stroustrup: The C++ programming language. 3rd Edition. Addison-Wessley, Reading, 1997.

Tobias
  • 6,388
  • 4
  • 39
  • 64
  • 12
    "gotos? Luxury! When I were a lad we grabbed the instruction pointer with our bare hands and dragged it over to the code we wanted to execute next" – Steve Jessop Jun 15 '09 at 09:49
  • 2
    "Dragging? You lucky bastards. We had to whip slaves to push our instruction pointers, which was very difficult because we were our own slaves. Then our mums would thrash us to sleep with broken bottles." – Kieveli Jun 15 '09 at 12:24
  • @Tobias - thanks for finding the quote. I put it into the original question (taken from my English edition of the book). – Dustin Boswell Jun 15 '09 at 18:18
  • @Tobias Here's an example of how the rewritten version only added confusion to perfectly clear code: suppose you have a loop counter variable, counting how many times the loop has been executed. In your re-written while loop, should such a counter be inside the loopBody function or inside the while loop? The answer is "it depends", there is no obvious answer. But in the do-while loop, it wouldn't have mattered. – Lundin Nov 13 '13 at 10:53
2

I hardly ever use them simply because of the following:

Even though the loop checks for a post-condition you still need to check for this post condition within your loop so that you don't process the post condition.

Take the sample pseudo code:

do {
   // get data 
   // process data
} while (data != null);

Sounds simple in theory but in real world situations it would probably turn out to look like so:

do {
   // get data 
   if (data != null) {
       // process data
   }
} while (data != null);

The extra "if" check just isn't worth it IMO. I have found very few instances where it's more terse to do a do-while instead of a while loop. YMMV.

rein
  • 32,967
  • 23
  • 82
  • 106
  • This doesn't really answer the question. In the situation that you're specifically calling out where you may already be in a state where you do not need to run the inner code, it's definitely true that `while() {...}` would be the appropriate choice - `while() {...}` is intended for exactly that case where you need to run code zero or more times based on a condition. However, that doesn't mean there are never cases for `do {...} while()`, situations where you know code needs to run at least once prior to checking the condition (see the selected answer from @david-božjak). – Chaser324 Jun 19 '14 at 16:39
2

In response to a question/comment from unknown (google) to the answer of Dan Olson:

"do { ... } while (0) is an important construct for making macros behave well."

#define M do { doOneThing(); doAnother(); } while (0)
...
if (query) M;
...

Do you see what happens without the do { ... } while(0)? It will always execute doAnother().

Markus Schnell
  • 1,075
  • 1
  • 10
  • 13
  • 1
    You don't need a do while for this though. #define M { one(); two(); } will suffice. – Simon H. Jun 15 '09 at 08:39
  • 2
    But then if (query) M; else printf("hello"); is a syntax error. – Jouni K. Seppänen Jun 15 '09 at 08:59
  • Only if you put a ; after the M. Otherwise it works just fine. – Simon H. Jun 15 '09 at 09:06
  • 4
    But that's worse: it means users have to remember whenever they use your macros that some of them expand to a block instead of an expression. And if you have one that's currently a void expression, and you need to change the implementation to something that requires two statements, then now it expands to a block and you have to update all the calling code. Macros are bad enough as it is: as much as possible of the pain of using them should be in the macro code, not the calling code. – Steve Jessop Jun 15 '09 at 09:43
  • @Markus: for this specific example, if do/while were not available in the language you could replace it with #define M ((void) (doOneThing(), doAnother())). – Steve Jessop Jun 15 '09 at 09:59
  • Those who use the sensible practice of always writing {} after statements do not need obscure macros like this. I've always regarded these do-while macro as obfuscation. But of course, the true source of the problem here, is that function-like macros were used in the first place. – Lundin Nov 13 '13 at 09:57
1

Well maybe this goes back a few steps, but in the case of

do
{
     output("enter a number");
     int x = getInput();

     //do stuff with input
}while(x != 0);

It would be possible, though not necessarily readable to use

int x;
while(x = getInput())
{
    //do stuff with input
}

Now if you wanted to use a number other than 0 to quit the loop

while((x = getInput()) != 4)
{
    //do stuff with input
}

But again, there is a loss in readability, not to mention it's considered bad practice to utilize an assignment statement inside a conditional, I just wanted to point out that there are more compact ways of doing it than assigning a "reserved" value to indicate to the loop that it is the initial run through.

Felps
  • 1
  • 1
1

I like David Božjak's example. To play devil's advocate, however, I feel that you can always factor out the code that you want to run at least once into a separate function, achieving perhaps the most readable solution. For example:

int main() {
    char c;

    do {
        printf("enter a number");
        scanf("%c", &c);

    } while (c < '0' ||  c > '9'); 
}

could become this:

int main() {
    char c = askForCharacter();
    while (c < '0' ||  c > '9') {
        c = askForCharacter();
    }
}

char askForCharacter() {
    char c;
    printf("enter a number");
    scanf("%c", &c);
    return c;
}

(pardon any incorrect syntax; I'm not a C programmer)

Niko Bellic
  • 2,370
  • 2
  • 29
  • 25
1

consider something like this:

int SumOfString(char* s)
{
    int res = 0;
    do
    {
        res += *s;
        ++s;
    } while (*s != '\0');
}

It so happens that '\0' is 0, but I hope you get the point.

Nefzen
  • 7,819
  • 14
  • 36
  • 34
1

My problem with do/while is strictly with its implementation in C. Due to the reuse of the while keyword, it often trips people up because it looks like a mistake.

If while had been reserved for only while loops and do/while had been changed into do/until or repeat/until, I don't think the loop (which is certainly handy and the "right" way to code some loops) would cause so much trouble.

I've ranted before about this in regards to JavaScript, which also inherited this sorry choice from C.

Nosredna
  • 83,000
  • 15
  • 95
  • 122
  • But every major coding style out there is consistent, they always write the `while` of `do-while` on the same line as the `}`. And there is always a semicolon at the end. No C programmer with at least a slight bit of competence gets confused or "tripped" when encountering the line: `} while(something);`. – Lundin Nov 13 '13 at 11:01