C++/C# bad practices: learn how to make a good code by bad example

Beware of the ‘continue’ operator inside do {…} while (…)

Fragment taken from the Haiku project (inheritor of BeOS). The code contains an error that analyzer diagnoses in the following way: V696 The ‘continue’ operator will terminate ‘do { … } while (FALSE)’ loop because the condition is always false.

The way continue works inside the do-while loop, is not the way some programmers expect it to. When continue is encountered, there will always be a check of loop termination condition. We’ll try to explain this in more details. Suppose the programmer writes code like this:

for (int i = 0; i < n; i++)
{
if (blabla(i))
continue;
foo();
}

Or like this:

while (i < n)
{
if (blabla(i++))
continue;
foo();
}

Most programmers by intuition understand that when continue is encountered, the controlling condition (i < n) will be (re)evaluated, and that the next loop iteration will only start if the evaluation is true. But when a programmer writes code:

do
{
if (blabla(i++))
continue;
foo();
} while (i < n);

the intuition often fails, as they don’t see a condition above the continue, and it seems to them that the continue will immediately trigger another loop iteration. This is not the case, and continue does as it always does – causes the controlling condition to be re-evaluated.

It depends on sheer luck if this lack of understanding of continue will lead to an error. However, the error will definitely occur if the loop condition is always false, as it is in the code snippet given above, where the programmer planned to carry out certain actions through subsequent iterations. A comment in the code “//try again” clearly shows their intention to do so. There will of course be no “again”, as the condition is always false, and so once continue is encountered, the loop will terminate.

In other words, it turns out that in the construction of this do {…} while (false), the continue is equivalent to using break.

Correct code

There are many options to write correct code. For example, create an infinite loop, and use continue to loop, and break to exit.

Try to avoid continue inside do { … } while (…). Even if you really know how it all works. The thing is that you could slip and make this error, and/or that your colleagues might read the code incorrectly, and then modify it incorrectly. We will never stop saying it: a good programmer is not the one who knows and uses different language tricks, but the one who writes clear understandable code, that even a newbie can comprehend.

Written by Andrey Karpov.This error was found with PVS-Studio static analysis tool.