break & continue Statements Skip or terminate
It is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression. In such cases, break and continue statements are used.
break Statement The break statement terminates the loop immediately when it is encountered, Simply comes out of the loop. Immediate statement after the closing brace of the loop will be executed. Syntax of break statement break;
Flowchart of break statement
How break statement works?
continue Statement The continue statement skips some statements inside the loop. Loop will restart when continue is encountered. Syntax of continue Statement continue;
Flowchart of continue Statement
How continue statement works?
Examples include<stdio.h> Void main(){ int i; for(i=0;i<=4;i++){ printf("%d“,i); break; printf("Label 1"); } printf("Label 2"); } Output: 0 label 2
#include<stdio.h> Void main(){ int i=5; do{ printf("%d“,i); continue; i++; } while(i<=10); } Output: Infinite loop