4

I'm getting a C4702: unreachable code warning in a for loop; the strange thing is that - by breaking apart the components inside the parens - the warning points to the increment part. Here's a sample program that demonstrates this error:

int main()
{
    int foo = 3;
    for (int i = 0;
        i < 999;
        i++)    // warning on this line
    {
        if (foo == 4);
        {
            break;
        }
    }
    return 0;
}

I can't figure out what's wrong with this line, because the for loop looks very straight-forward.

congusbongus
  • 13,359
  • 7
  • 71
  • 99

3 Answers3

13

You have a stray semicolon in your if-statement:

if (foo == 4);

Recall that for loops have the following structure:

for (initialisation; condition; increment/decrement)
    statement

Execution will proceed in the following order:

  1. initialisation
  2. condition; if false then end
  3. statement
  4. increment/decrement
  5. Go to step 2

If the compiler is warning about the increment/decrement being unreachable, it means that something before it is causing execution to always skip it - in this case, the stray semicolon causes the break to always execute, jumping out of the loop prematurely.

congusbongus
  • 13,359
  • 7
  • 71
  • 99
  • I know it's an old one but I just falled on this investigating the same mysterious warning. My case was of the form "for(auto variable: collection) { return variable->something(); }". This fake-loop was supposed to find the frst "variable" in my collection and return it's work. As the return always skips the hidden incrementation, this generates the "code not reached" warning. Adding a simple "if (variable)" before the return did the trick. (Assuming the collection is for example a vector.) – Francis Pierot Aug 03 '18 at 09:25
  • 1
    @FrancisPierot: A cleaner fix for your issue is to instead do `if (!collection.empty()) return collection.front().something();`, then it's clear you want a conditional and not a loop. – thakis Mar 29 '20 at 17:38
  • @thakis Indeed, much cleaner yes! I wanted to avoid using any "collection." reference using the smart for() to clean code but in the end it makes things more obscure. – Francis Pierot Mar 31 '20 at 17:33
5
for (int i = 0;
    i < 999;
    i++)    // warning on this line
{
    if (foo == 4);
    {
        break;
    }
}

This is same as

for (int i = 0;
    i < 999;
    )    
{
    if (foo == 4);
    {
        break;
    }
    i++;  /* Note the increment here */
}

So anyways you are going to break because of the if(foo == 4); so i++ is not reachable

Gopi
  • 19,784
  • 4
  • 24
  • 36
2

The problem (i.e. the unintended ;) is already described in two answers.

I just like to add that you should take a look at your compiler options setting. Many compilers can detect such "strange looking" code and give you a warning. For instance:

Warning: "empty body in an 'if' statement"

Further the compiler can be configured to treat all warnings as errors.

In other words - the compiler options can often help you finding "unintended code" so that you avoid wasting time.

BTW - the eclipse editor can even give a "suspicious semicolon" warning as soon as you type such an if-statement.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63