Continue Statement in C: A Friendly Guide for Beginners

Hello there, future programmers! Today, we're going to dive into one of the most useful control flow statements in C programming: the continue statement. Don't worry if you're new to this; we'll start from the basics and work our way up. By the end of this tutorial, you'll be a pro at using continue statements!

C - Continue Statement

What is Continue Statement in C?

Imagine you're in a buffet line, picking out your favorite foods. Suddenly, you see a dish you don't like. What do you do? You skip it and move on to the next one, right? Well, that's exactly what the continue statement does in programming!

The continue statement allows us to skip the rest of the current iteration of a loop and jump to the next iteration. It's like saying, "Nope, not interested in this one. Let's move on to the next!"

Why Use Continue?

  1. It helps us avoid unnecessary code execution.
  2. It makes our loops more efficient.
  3. It can simplify complex conditional logic.

Now, let's look at how we actually write this magic statement!

Continue Statement Syntax

The syntax of the continue statement is beautifully simple:

continue;

That's it! Just one word followed by a semicolon. But don't let its simplicity fool you – this little statement can be incredibly powerful when used correctly.

Continue Statement Flowchart

To better understand how the continue statement works, let's visualize it with a flowchart:

         ┌─────────────┐
         │ Loop Starts │
         └──────┬──────┘
                │
         ┌──────▼──────┐
         │ Condition   │
         │   Check     │
         └──────┬──────┘
                │
         ┌──────▼──────┐
         │  Continue   │
         │ Encountered │
         └──────┬──────┘
                │
                │    ┌─────────────────┐
                └────► Skip Rest of    │
                     │ Current Iteration│
                     └─────────┬───────┘
                               │
                     ┌─────────▼───────┐
                     │ Next Iteration  │
                     └─────────────────┘

As you can see, when the continue statement is encountered, it immediately jumps to the next iteration, skipping any code that comes after it in the current iteration.

Continue Statement with Nested Loops

Now, let's talk about something a bit more advanced: using continue with nested loops. Don't worry, it's not as scary as it sounds!

When you use continue in a nested loop, it only affects the innermost loop containing the continue statement. It's like having a "skip" button for each individual ride in an amusement park, rather than skipping the whole park!

Let's look at an example:

#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 3; i++) {
        printf("Outer loop iteration %d:\n", i);

        for (j = 1; j <= 5; j++) {
            if (j == 3) {
                continue;
            }
            printf("  Inner loop: %d\n", j);
        }
    }

    return 0;
}

In this example, the continue statement is inside the inner loop. It causes the loop to skip printing when j is 3, but it doesn't affect the outer loop at all.

Output:

Outer loop iteration 1:
  Inner loop: 1
  Inner loop: 2
  Inner loop: 4
  Inner loop: 5
Outer loop iteration 2:
  Inner loop: 1
  Inner loop: 2
  Inner loop: 4
  Inner loop: 5
Outer loop iteration 3:
  Inner loop: 1
  Inner loop: 2
  Inner loop: 4
  Inner loop: 5

See how the number 3 is missing from the inner loop output? That's our continue statement at work!

Continue Statement Examples

Now, let's look at some more examples to really cement our understanding of the continue statement.

Example 1: Skipping Even Numbers

#include <stdio.h>

int main() {
    int i;

    for (i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            continue;
        }
        printf("%d ", i);
    }

    return 0;
}

Output:

1 3 5 7 9

In this example, we're using the continue statement to skip even numbers. When i is divisible by 2 (i.e., when it's even), the continue statement causes the loop to skip to the next iteration without printing the number.

Example 2: Skipping Negative Numbers in User Input

#include <stdio.h>

int main() {
    int num, sum = 0;

    printf("Enter numbers (enter 0 to stop):\n");

    while (1) {
        scanf("%d", &num);

        if (num == 0) {
            break;
        }

        if (num < 0) {
            printf("Negative numbers are not allowed. Try again.\n");
            continue;
        }

        sum += num;
    }

    printf("Sum of positive numbers: %d\n", sum);

    return 0;
}

In this example, we're using continue to skip negative numbers in a user input scenario. If the user enters a negative number, we print a message and use continue to skip adding that number to the sum.

Example 3: Processing Specific Items in an Array

#include <stdio.h>

int main() {
    int numbers[] = {1, -2, 3, -4, 5, -6, 7, -8, 9, -10};
    int i, positiveSum = 0;

    for (i = 0; i < 10; i++) {
        if (numbers[i] < 0) {
            continue;
        }
        positiveSum += numbers[i];
    }

    printf("Sum of positive numbers: %d\n", positiveSum);

    return 0;
}

Output:

Sum of positive numbers: 25

In this final example, we're using continue to skip negative numbers in an array and sum only the positive ones.

Conclusion

And there you have it, folks! We've journeyed through the world of continue statements in C. From understanding what they are and how they work, to seeing them in action with nested loops and real-world examples, you're now equipped to use continue statements like a pro!

Remember, programming is all about practice. So don't be afraid to experiment with these concepts in your own code. Try combining continue with different types of loops, or use it to solve problems in your own unique way.

Happy coding, and may the continue be with you! ?

Method Description
continue; Skips the rest of the current iteration and moves to the next iteration of the loop

Credits: Image by storyset