what is the difference between break statement and continue statement
Answers
Answered by
3
Difference Between break and continue
Difference Between break and continuebreakcontinue
A break can appear in both switch and loop (for, while, do) statements.
A continue can appear only in loop (for, while, do) statements.
A break causes the switch or loop statements to terminate the moment it is executed. Loop or switch ends abruptly when break is encountered.
A continue doesn't terminate the loop, it causes the loop to go to the next iteration. All iterations of the loop are executed even if continue is encountered. The continue statement is used to skip statements in the loop that appear after the continue.
The break statement can be used in both switch and loop statements.
The continuestatement can appear only in loops. You will get an error if this appears in switch statement.
When a breakstatement is encountered, it terminates the block and gets the control out of the switch or loop.
When a continuestatement is encountered, it gets the control to the next iteration of the loop.
A break causes the innermost enclosing loop or switch to be exited immediately.
A continue inside a loop nested within a switch causes the next loop iteration.
Similarities Between break and continue
Both break and continue statements in C programming language have been provided to alter the normal flow of program.
Example using 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).
Example using 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
Difference Between break and continuebreakcontinue
A break can appear in both switch and loop (for, while, do) statements.
A continue can appear only in loop (for, while, do) statements.
A break causes the switch or loop statements to terminate the moment it is executed. Loop or switch ends abruptly when break is encountered.
A continue doesn't terminate the loop, it causes the loop to go to the next iteration. All iterations of the loop are executed even if continue is encountered. The continue statement is used to skip statements in the loop that appear after the continue.
The break statement can be used in both switch and loop statements.
The continuestatement can appear only in loops. You will get an error if this appears in switch statement.
When a breakstatement is encountered, it terminates the block and gets the control out of the switch or loop.
When a continuestatement is encountered, it gets the control to the next iteration of the loop.
A break causes the innermost enclosing loop or switch to be exited immediately.
A continue inside a loop nested within a switch causes the next loop iteration.
Similarities Between break and continue
Both break and continue statements in C programming language have been provided to alter the normal flow of program.
Example using 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).
Example using 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
Answered by
0
they both are the in java.... the difference between them is that
the break is statement is used to teminate the squence of statement... whereas the continue statement is used when we have to join another statement in the same program..
the break is statement is used to teminate the squence of statement... whereas the continue statement is used when we have to join another statement in the same program..
Similar questions