Tuesday, 11 December 2018

What-is-the-difference-between-a-break-and-continue-statement? tccicomputercoaching.com

Difference Between break and continue in Computer Programming.

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 continue statement can appear only in loops. You will get an error if this appears in switch statement.

When a break statement is encountered, it terminates the block and gets the control out of the switch or loop.

When a continue statement 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:

Example of break:

1. #include<stdio.h>
2. int main(){
3. int i;
4. for(i=0;i<5;++i){
5. if(i==3)
6. break;
7. printf(“%d “,i);
8. }
9. return 0;}

Output: 0 1 2

Example of continue:

1.      #include<stdio.h>
2.      int main(){
3.        int i;
4.        for(i=0;i<5;++i){
5.        if(i==3)
6.        continue;
7.        printf(“%d “,i);
8.        }
9.        return 0;
10.  }

Output: 0 1 2 4


To learn more about Programming Language at TCCI.

TCCI is located in Bopal and Satellite in Ahmedabad.

Call us @ 9825618292

Visit us @ http://tccicomputercoaching.com/course/

No comments:

Post a Comment