>
>
>
V3098. The 'continue' operator will ter…


V3098. The 'continue' operator will terminate 'do { ... } while (false)' loop because the condition is always false.

The analyzer detected a code fragment that may mislead programmers reading it. Not all developers know that using the "continue" statement in a "do { ... } while(false)" loop will terminate it instead of continuing its execution.

So, after executing the 'continue' statement, the '(false)' condition will be checked and the loop will terminate because the condition is false.

Consider the following example:

int i = 1;
do
{
  Console.Write(i);
  i++;
  if (i < 3) 
    continue;
  Console.Write('A');
} while (false);

The programmer may expect the program to print '12A', but it will actually print '1'.

Even if the code was intended to work that way and there is no error, it is still recommended to revise it. For example, you can use the 'break' statement:

int i=1;
do {
  Console.Write(i);
  i++;
  if(i < 3) 
    break;
  Console.Write('A');
} while(false);

The code has become clearer; one can immediately see that the loop will terminate if the "(i < 3)" condition is true. In addition, it won't trigger the analyzer warning anymore.

If the code is incorrect, it must be fixed. There are no set rules as to how exactly it should be rewritten since it depends on the code's execution logic. For example, if you need the program to print '12A', it is better to rewrite this fragment as follows:

for (i = 1; i < 3; ++i)
  Console.Write(i);
Console.Write('A');

This diagnostic is classified as: