Is continue Necessary?

On this blog, I try to show examples for all the C language keywords, but continue is one I struggle with. It doesn’t appear in Beginning Programming with C For Dummies. It barely appears in my C All-in-one Desktop Reference For Dummies. Even the venerable K&R manual has difficulty explaining how this keyword is worthy.

It’s said that continue is like the break keyword in that it interrupts the flow of a for, while, or do loop. Unlike break, continue directs the loop to keep spinning — providing that the loop’s condition remains true.

The problem I see is that you can’t do anything with continue that you could otherwise do with an if-else structure. In fact, I don’t believe I’ve ever seen continue used without an if statement. That evidence kinda drives home the point for me.

#include <stdio.h>
  
int main()
{
    int x;

    for(x=1;x<11;x++)
    {
        printf("%d",x);
        if(x%5==0)
        {
            putchar('\n');
            continue;
        }
        printf(", ");
    }

    return(0);
}

The code above outputs two rows of numbers. The if(x%5==0) statement breaks the output with a newline every 5 numbers. The continue statement directs the loop to repeat, skipping over the final printf() statement. Here’s the output:

1, 2, 3, 4, 5
6, 7, 8, 9, 10

You could, however, re-write the if block as follows:

if(x%5==0)
    putchar('\n');
else
    printf(", ");

One way I can figure that continue would be useful is in a loop with lots of statements and nested conditions. In that setup, continue saves you from coding a complex if-else structure just to bail out.

Yes, and I suppose the same argument I’ve made about continue can also be made about the break keyword. It’s possible to code without it, but having it available makes it easier to work in loops, break out of them — or continue looping — without a lot of complex overhead.

If you can think of a way to use continue that’s unique or clever, let me know.

2 thoughts on “Is continue Necessary?

  1. My feeling is that continue is a bit like goto (although not quite so bad) – if you think you need to use it you need to think again! There might be legitimate uses where it’s the best (but probably not only) solution but I can’t think of any offhand.

    According to Messrs. K&R:

    “The continue statement is often used when the part of the loop that follows is complicated, so that reversing a test and indenting another level would nest the program too deeply”

    which is effectively what you said:

    “One way I can figure that continue would be useful is in a loop with lots of statements and nested conditions. In that setup, continue saves you from coding a complex if-else structure just to bail out.”

  2. What prompted this post was code I saw where ‘continue’ was used and it’s surprised me. Then I looked again and figured because he used an ‘if’ before ‘continue’ he could have just added an ‘else’ and not needed ‘continue.’

    Your comment about ‘continue’ being like ‘goto’ is understood. I never thought of it that way, but you’re right.

Leave a Reply