Skip to content
StudyFame

break and continue in C

Sometimes you want to stop a loop early: "I found what I was looking for, no need to check further." Sometimes you want to skip just one round: "This number is negative, ignore it and move to the next."

C gives you two keywords for this: break and continue. In this lesson you will learn both and the difference between them, a very common exam question.

What You Will Learn

  • What break does inside a loop.
  • What continue does inside a loop.
  • The difference table between break and continue.
  • How break works in nested loops.
  • A real program using both.

break and continue in C Explained

Exam definition: break immediately terminates the nearest enclosing loop (or switch) and continues after it. continue skips the rest of the current iteration and jumps to the next iteration of the loop.

Pointbreakcontinue
What it doesExits the loop completelySkips to the next iteration
Loop after itEndsKeeps running
Used inLoops and switchLoops only (not switch)
In a for loop, goes toThe statement after the loopThe update part (i++), then the condition
Example useStop searching once foundIgnore invalid values

Memory trick: Watching a series: break = switch off the TV (stop watching). continue = skip this episode and play the next one.

Syntax of break and continue

for (...)
{
    if (condition1)
        break;       /* leave the loop now */
    if (condition2)
        continue;    /* skip the rest, go to next iteration */
    /* rest of the body */
}

Example 1: break vs continue Side by Side

#include <stdio.h>

int main(void)
{
    printf("break at 6   : ");
    for (int i = 1; i <= 10; i++)
    {
        if (i == 6)
            break;
        printf("%d ", i);
    }

    printf("\ncontinue at 3: ");
    for (int i = 1; i <= 5; i++)
    {
        if (i == 3)
            continue;
        printf("%d ", i);
    }

    printf("\n");
    return 0;
}

Output

break at 6   : 1 2 3 4 5
continue at 3: 1 2 4 5 

Code Explanation

  1. break: when i becomes 6, break runs and the loop ends immediately. 6 to 10 are never printed.
  2. continue: when i is 3, continue skips the printf for that round only. The loop goes on with 4 and 5.
  3. In a for loop, continue still runs the update (i++), so the loop doesn't get stuck.

How break and continue Work

  • break jumps to the first statement after the loop.
  • continue jumps to the end of the current iteration: in a for loop that is the update; in while and do-while it is the condition check.
  • In a while loop, make sure the update happens before continue, or you may create an infinite loop.
  • In nested loops, break and continue affect only the innermost loop they are in, not the outer one.

Example 2: Add Positive Numbers, Skip Negatives, Stop at 0

#include <stdio.h>

int main(void)
{
    int n, sum = 0;

    while (1)
    {
        printf("Enter a number (0 to stop): ");
        scanf("%d", &n);

        if (n == 0)
            break;          /* stop the loop */
        if (n < 0)
        {
            printf("Skipping negative number\n");
            continue;       /* ignore this one */
        }
        sum += n;
    }

    printf("Sum of positive numbers = %d\n", sum);
    return 0;
}

Output

Enter a number (0 to stop): 5
Enter a number (0 to stop): -2
Skipping negative number
Enter a number (0 to stop): 8
Enter a number (0 to stop): 0
Sum of positive numbers = 13

while (1) would run forever, but break gives it an exit. A prime-number check uses the same idea: stop the loop with break as soon as a divisor is found. See our prime number program in C.

Important Points to Remember

  • break = exit the loop. continue = skip to the next iteration.
  • break works in loops and switch; continue only in loops.
  • Both affect only the innermost loop.
  • In a for loop, continue still runs the update.
  • while (1) + break is a common pattern.

Common Mistakes Beginners Make

MistakeWhat happensFix
continue before i++ in a while loopInfinite loopUpdate before continue
Expecting break to exit all nested loopsOnly the inner loop endsUse a flag variable (or goto, rarely)
Using continue inside a switch (not in a loop)Compile errorcontinue only works in loops
break outside any loop or switchCompile errorUse it only inside loops/switch

break and continue Practice Questions

  1. Write three differences between break and continue.
  2. Can continue be used in a switch statement?
  3. What is the output of for (i = 1; i <= 5; i++) { if (i == 4) break; printf("%d", i); }?
  4. What is the output of for (i = 1; i <= 5; i++) { if (i % 2 == 0) continue; printf("%d", i); }?
  5. In nested loops, which loop does break exit?
Show answers
  1. break exits the loop, continue skips to the next iteration; break works in switch, continue doesn't; after break the loop ends, after continue it keeps running.
  2. No (only if the switch is itself inside a loop, and then continue affects the loop).
  3. 123
  4. 135
  5. Only the innermost loop that contains it.

Coding Exercise

Print numbers from 1 to 20 but skip multiples of 3, and stop completely when you reach a number greater than 14.

Show solution
#include <stdio.h>

int main(void)
{
    for (int i = 1; i <= 20; i++)
    {
        if (i > 14)
            break;
        if (i % 3 == 0)
            continue;
        printf("%d ", i);
    }
    printf("\n");
    return 0;
}
1 2 4 5 7 8 10 11 13 14 

Quick Quiz: break vs continue (MCQ)

  1. Which statement ends a loop immediately?

    A) continue   B) break   C) return 1   D) goto only

  2. Which statement skips only the current iteration?

    A) continue   B) break   C) exit   D) stop

  3. Which can be used in a switch?

    A) continue   B) break   C) Both   D) Neither

  4. Output: for(i=1;i<=3;i++){ if(i==2) continue; printf("%d",i); }

    A) 123   B) 13   C) 1   D) 23

  5. In nested loops, break exits:

    A) All loops   B) The outermost loop   C) The innermost loop containing it   D) The program

Show answers
  1. B: break.
  2. A: continue.
  3. B: break.
  4. B: 13.
  5. C: the innermost loop containing it.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

breakExits the loop (or switch)
continueSkips to the next iteration
break used inLoops and switch
continue used inLoops only
Nested loopsAffect only the innermost loop
continue in forGoes to the update, then condition

Frequently asked questions

What is the difference between break and continue?
break ends the loop; continue skips the rest of this iteration and goes to the next.
Can break exit two loops at once?
No. It exits only the innermost loop; use a flag to exit outer loops.
Can continue be used without a loop?
No, it is a compile error outside a loop.

Remember

  • break = switch off the TV; continue = skip the episode.
  • Both act on the innermost loop.

Next Topic

Next lesson: Infinite Loops in C. You will learn why loops sometimes never end, and how to fix them.