Lesson 32 of 60 – continue Statement in C
53%

continue Statement in C

The continue statement is used inside loops to skip the remaining statements of the current iteration and move to the next iteration of the loop.

Note: Unlike break, the continue statement does not terminate the loop. It only skips the current iteration.

1. What is continue Statement?

The continue statement skips the remaining part of the current loop iteration.

continue;

After continue, execution proceeds with the next iteration according to the rules of the loop.

2. Syntax of continue

continue;

The keyword continue is followed by a semicolon.

3. Simple continue Example

for (int i = 1; i <= 5; i++)
{
    if (i == 3)
    {
        continue;
    }

    printf("%d ", i);
}

Output:

1 2 4 5

When i is 3, the continue statement skips the printf() statement for that iteration.

4. continue with for Loop

for (int i = 1; i <= 10; i++)
{
    if (i % 2 == 0)
    {
        continue;
    }

    printf("%d ", i);
}

Output:

1 3 5 7 9

The even numbers are skipped.

5. continue with while Loop

int i = 0;

while (i < 10)
{
    i++;

    if (i == 5)
    {
        continue;
    }

    printf("%d ", i);
}

Output:

1 2 3 4 6 7 8 9 10

The value 5 is skipped.

6. continue with do-while Loop

int i = 0;

do
{
    i++;

    if (i == 3)
    {
        continue;
    }

    printf("%d ", i);
}
while (i < 5);

Output:

1 2 4 5

7. continue with if Statement

The continue statement is often used inside an if statement to decide which iterations should be skipped.

for (int i = 1; i <= 10; i++)
{
    if (i < 5)
    {
        continue;
    }

    printf("%d ", i);
}

Output:

5 6 7 8 9 10

8. continue Skips Remaining Statements

for (int i = 1; i <= 5; i++)
{
    if (i == 3)
    {
        continue;
    }

    printf("Number: %d\n", i);
    printf("Hello\n");
}

When i is 3, both printf() statements after continue are skipped.

9. continue for Even Numbers

for (int i = 1; i <= 10; i++)
{
    if (i % 2 != 0)
    {
        continue;
    }

    printf("%d ", i);
}

Output:

2 4 6 8 10

Odd numbers are skipped, so only even numbers are printed.

10. continue for Odd Numbers

for (int i = 1; i <= 10; i++)
{
    if (i % 2 == 0)
    {
        continue;
    }

    printf("%d ", i);
}

Output:

1 3 5 7 9

11. Skipping Multiples of 3

for (int i = 1; i <= 15; i++)
{
    if (i % 3 == 0)
    {
        continue;
    }

    printf("%d ", i);
}

Output:

1 2 4 5 7 8 10 11 13 14

12. continue with Array

int numbers[] = {10, -5, 20, -8, 30};

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

    printf("%d ", numbers[i]);
}

Output:

10 20 30

Negative values are skipped.

13. Skipping Zero Values

int numbers[] = {10, 0, 20, 0, 30};

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

    printf("%d ", numbers[i]);
}

Output:

10 20 30

14. continue with User Input

int number;

for (int i = 1; i <= 5; i++)
{
    printf("Enter number: ");
    scanf("%d", &number);

    if (number < 0)
    {
        continue;
    }

    printf("Positive number = %d\n", number);
}

Negative values are ignored and the next iteration begins.

15. continue in a Sum Program

int sum = 0;

for (int i = 1; i <= 10; i++)
{
    if (i % 2 != 0)
    {
        continue;
    }

    sum += i;
}

printf("Sum = %d", sum);

Output:

Sum = 30

Only even numbers are added to the sum.

16. continue in a Multiplication Program

for (int i = 1; i <= 10; i++)
{
    if (i == 5)
    {
        continue;
    }

    printf("5 x %d = %d\n", i, 5 * i);
}

The multiplication for 5 is skipped.

17. continue with Character Processing

char text[] = "HELLO";

for (int i = 0; text[i] != '\0'; i++)
{
    if (text[i] == 'L')
    {
        continue;
    }

    printf("%c ", text[i]);
}

Output:

H E O

Both occurrences of L are skipped.

18. continue with Strings

char text[] = "computer";

for (int i = 0; text[i] != '\0'; i++)
{
    if (text[i] == 'o')
    {
        continue;
    }

    printf("%c", text[i]);
}

Output:

cmputer

The character o is skipped.

19. continue and Loop Update

In a for loop, when continue is executed, control moves to the loop's update expression before the next condition check.

for (int i = 1; i <= 5; i++)
{
    if (i == 3)
    {
        continue;
    }

    printf("%d ", i);
}

After skipping 3, the i++ update still occurs.

20. continue and while Loop Update

In a while loop, you must be careful about updating the loop variable before reaching continue.

int i = 0;

while (i < 5)
{
    i++;

    if (i == 3)
    {
        continue;
    }

    printf("%d ", i);
}

Output:

1 2 4 5

The update occurs before the continue, so the loop progresses.

21. continue vs break

continue break
Skips the current iteration. Terminates the nearest loop or switch.
The loop continues. The loop ends immediately.
Useful for skipping selected values. Useful for stopping early.
// continue
if (condition)
{
    continue;
}

// break
if (condition)
{
    break;
}

22. continue in Nested Loops

for (int i = 1; i <= 2; i++)
{
    for (int j = 1; j <= 5; j++)
    {
        if (j == 3)
        {
            continue;
        }

        printf("%d ", j);
    }

    printf("\n");
}

The continue affects the nearest enclosing loop, so here it skips the current iteration of the inner loop.

23. continue Does Not Stop the Loop

for (int i = 1; i <= 5; i++)
{
    if (i == 3)
    {
        continue;
    }

    printf("%d ", i);
}

printf("\nLoop completed.");

Output:

1 2 4 5
Loop completed.

The loop continues after skipping 3.

24. Common Mistake: Infinite while Loop

A continue statement can accidentally create an infinite loop if the loop variable is not updated before reaching it.

int i = 1;

while (i <= 5)
{
    if (i == 3)
    {
        continue;
    }

    i++;
}

When i becomes 3, continue is executed and i++ is skipped. The value remains 3, so the loop can continue forever.

25. Correcting the while Loop

Move the update before the continue when necessary.

int i = 0;

while (i < 5)
{
    i++;

    if (i == 3)
    {
        continue;
    }

    printf("%d ", i);
}

Output:

1 2 4 5

26. Skipping Invalid Values

int numbers[] = {10, -2, 20, -5, 30};
int size = 5;

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

    printf("Valid: %d\n", numbers[i]);
}

The program ignores negative values and processes only non-negative values.

27. Complete Even Number Program

#include <stdio.h>

int main()
{
    int n;

    printf("Enter a positive number: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++)
    {
        if (i % 2 != 0)
        {
            continue;
        }

        printf("%d ", i);
    }

    return 0;
}

This program skips odd numbers and prints only even numbers.

28. Complete Positive Number Program

#include <stdio.h>

int main()
{
    int number;

    for (int i = 1; i <= 5; i++)
    {
        printf("Enter number %d: ", i);
        scanf("%d", &number);

        if (number <= 0)
        {
            continue;
        }

        printf("Valid positive number: %d\n", number);
    }

    return 0;
}

Invalid values are skipped, while positive values are processed.

29. Complete Array Processing Program

#include <stdio.h>

int main()
{
    int numbers[] = {10, -5, 20, 0, 30, -8};
    int size = 6;
    int sum = 0;

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

        sum += numbers[i];
    }

    printf("Sum of positive numbers = %d", sum);

    return 0;
}

The program skips zero and negative values and adds only positive values.

30. Practice Programs Using continue

Practice the following programs:

  1. Print numbers from 1 to 50 but skip multiples of 5.
  2. Print only even numbers from 1 to 100.
  3. Print only odd numbers from 1 to 100.
  4. Skip negative numbers in an array.
  5. Skip zero values in an array.
  6. Calculate the sum of only positive numbers.
  7. Print a multiplication table but skip the 5th row.
  8. Read five numbers and ignore negative values.
  9. Print a string while skipping a selected character.
  10. Use continue inside a nested loop and observe its effect.

Practice carefully with both for and while loops, especially when the update statement is involved.

📌 Key Points

  • The continue statement skips the current iteration.
  • It does not terminate the loop.
  • It is commonly used inside an if statement.
  • It can be used with for, while, and do-while loops.
  • It is useful for skipping unwanted or invalid values.
  • In a for loop, continue proceeds to the update expression.
  • In a while loop, make sure the loop variable still gets updated.
  • In nested loops, continue affects the nearest enclosing loop.
  • continue and break have different purposes.
  • Use continue when you want the loop to keep running while skipping selected iterations.

🧠 Quick Quiz

Question: What does the continue statement do inside a loop?