Answer: break exits the loop immediately; continue skips the remaining statements of the current iteration and goes to the next one.
- break: used in loops and switch; control jumps to the first statement after the loop.
- continue: used only in loops; in a for loop control goes to the update (i++) and then the condition.
Program:
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
{
if (i == 5)
continue; /* skip the rest of this iteration */
if (i == 8)
break; /* leave the loop completely */
printf("%d ", i);
}
printf("\nLoop ended at i = %d\n", i);
return 0;
}Sample output (actual run):
1 2 3 4 6 7
Loop ended at i = 8Explanation: 5 is not printed because of continue; when i becomes 8, break ends the loop, so 8, 9 and 10 are never printed.