QUESTION 28 · Jump Statements
Explain the continue statement and its behaviour in different loops.
5 marksAnswer in brief
The continue statement skips the remaining statements of the current loop iteration and moves to the loop’s continuation step.
Behaviour in each loop
- In for, continue moves to the update expression and then the condition test.
- In while, it moves to the condition test.
- In do-while, it moves to the condition test at the end.
Example trace
The example runs i from 1 to 5 and skips 3. For i = 1 and 2, the condition i == 3 is false and printf runs. When i = 3, continue skips printf. The for update still increments i, so the next iterations print 4 and 5. Unlike break, continue does not end the loop.
Important precaution
In a while loop, an update at the end of the body can be skipped by continue. If the counter remains unchanged and the condition stays true, the loop may never finish. Arrange the update so every repeating path makes progress. A continue inside a switch that is itself inside a loop continues the loop; it does not merely leave the switch.
Complexity
Skipping one print does not remove the other counter iterations. A loop from 1 through n that performs constant work per iteration is still O(n), even when some iterations skip later statements. Only a fixed counter is needed, so auxiliary space is O(1).
C program and output
/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
for (int i = 1; i <= 5; ++i) {
if (i == 3) { continue; }
printf("%d\n", i);
}
return 0;
}
Sample input and output
This example uses values already written in the program.
Output
1
2
4
5