The major difference between break
and continue
statements in C language is that a break
causes the innermost enclosing loop or switch
to be exited immediately. Whereas, the continue
statement causes the next iteration of the enclosing for
, while
, or do
loop to begin. The continue
statement in while
and do
loops takes the control to the loop's test-condition immediately, whereas in the for
loop it takes the control to the increment step of the loop.
The continue
statement applies only to loops, not to switch
. A continue
inside a switch
inside a loop causes the next loop iteration.
Practically, break
is used in switch
, when we want to exit after a particular case
is executed; and in loops, when it becomes desirable to leave the loop as soon as a certain condition occurs (for instance, you detect an error condition, or you reach the end of your data prematurely).
The continue
statement is used when we want to skip one or more statements in loop's body and to transfer the control to the next iteration.
break
and continue
break | continue |
---|---|
A |
A |
A |
A |
The |
The |
When a |
When a |
A |
A |
break
and continue
Both break
and continue
statements in C programming language have been provided to alter the normal flow of program.
break
The following function, trim
, removes trailing blanks, tabs and newlines from the end of a string, using a break to exit from a loop when the rightmost non-blank, non-tab, non-newline is found.
/* trim: remove trailing blanks, tabs, newlines */ int trim(char s[]) { int n; for (n = strlen(s)-1; n >= 0; n--) if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n') break; s[n+1] = '\0'; return n; }
strlen
returns the length of the string. The for
loop starts at the end and scans backwards looking for the first character that is not a blank or tab or newline. The loop is broken when one is found, or when n
becomes negative (that is, when the entire string has been scanned).
continue
As an example, the following piece of code sums up the non-negative elements in the array a
; negative values are skipped.
/* sum up non-negative elements of an array */ #include <stdio.h> int main() { int a[10] = {-1, 2, -3, 4, -5, 6, -7, 8, -9, 10}; int i, sum = 0; for (i = 0; i < 10; i++) { if (a[i] < 0) /* skip negative elements */ continue; sum += a[i]; /* sum positive elements */ } printf("Sum of positive elements: %d\n", sum); } OUTPUT ====== Sum of positive elements: 30
Hope you have enjoyed reading differences and similarities between break
and continue
. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!
Share this page on WhatsApp