A security guard checking identity cards stops searching the moment the wanted person is found, and waves past anyone who is not a student without further questions. Loops sometimes need the same two abilities: to stop early, and to skip one item. C provides break and continue for these purposes.
The break Statement
Definition: break. A statement that immediately ends the nearest enclosing loop or switch statement and transfers control to the statement that follows it.
break is used when further passes are pointless. In a search, once the item is found there is nothing to gain by examining the remaining values. In the prime test of this unit, one exact divisor is enough to settle the answer.
The continue Statement
Definition: continue. A statement that skips the remaining statements of the current pass of the nearest enclosing loop and proceeds to the next pass.
continue does not end the loop. It abandons only the current pass. It is used to ignore values that need no processing, such as negative numbers in a list that should contain marks.
Table 3.10 summarizes the differences.
Table 3.10: Comparison of break and continue
| Point | break | continue |
|---|---|---|
| Effect | Ends the loop | Ends only the current pass |
| Control goes to | The statement after the loop | The next test of the loop condition |
| Used in | Loops and switch | Loops only |
| Typical use | Stop when the answer is found | Skip an unwanted value |
Behaviour in the Three Loops
After continue, a for loop still carries out its update expression before the condition is tested again. In a while or do-while loop, control goes straight to the condition. If the update of the control variable is written after the continue statement in a while loop, it will be skipped, and the loop may never end.
Nested Loops
Both statements affect only the nearest enclosing loop. A break in an inner loop ends the inner loop; the outer loop carries on with its next pass.
Using Them Sparingly
Loops are easiest to understand when they have a single exit, through the loop condition. break and continue create additional paths. They should be used when they make the logic plainer, as in a search, and avoided when an ordinary condition would do.
Example 3.10: Skipping and stopping
Problem: Read up to 10 integers. Ignore negative values, stop at the first zero, and display the sum of the positive values read.
Solution:
- Use a for loop for at most 10 values.
- If the value is 0, leave the loop with break.
- If the value is negative, skip it with continue.
- Otherwise add it to the sum.
#include <stdio.h>
int main(void)
{
int i, x, sum = 0;
printf("Enter up to 10 integers (0 to stop):\n");
for (i = 1; i <= 10; i++)
{
scanf("%d", &x);
if (x == 0)
{
break;
}
if (x < 0)
{
continue;
}
sum = sum + x;
}
printf("Sum of positive values = %d\n", sum);
return 0;
}
Input:
5 -3 8 -1 4 0 9
Output:
Enter up to 10 integers (0 to stop):
Sum of positive values = 17
Result: The values −3 and −1 are skipped by continue. The zero triggers break, so 9 is never read. The sum is 5 + 8 + 4 = 17.
Common mistakes
- Expecting
breakin an inner loop to end the outer loop as well. - Placing the update of a
whileloop aftercontinue, so that it is skipped and the loop never ends. - Confusing the two statements:
breakleaves the loop,continueonly starts the next pass.
Key takeaways
breakends the nearest loop or switch at once.continueskips the rest of the current pass and begins the next.- In a
forloop the update expression still runs aftercontinue. - Both statements act only on the nearest enclosing loop.
Practice questions
- Which statement would you use to stop a search loop once the item is found?
- After continue in a for loop, what is carried out next?
- A break is carried out in the inner of two nested loops. Which loop ends?
Answers to the practice questions
- break.
- The update expression, and then the loop condition is tested.
- Only the inner loop; the outer loop continues.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit III
