C break and continue
What is a break statement in C?
A break statement has a wide variety of application in C. Have you ever seen a premature exit of any loop in your program? The break statement will help us to jump out of the loop immediately. Basically for, while, do while, and switch loop implements break statements and sometimes If statement also uses it. Break statements will cause innermost enclosing loop or switch to jump out of the loop. The break statement will usage is illustrated below.
Syntax:
//C program to illustrate the usage of break statement in C. #include<stdio.h> int main(){ int i; for(i=0;i<10;i++){ if(i==4) break; printf("%d",i); } printf("\nThese numbers are the numbers before using break statement."); }
Likewise, the output of the above code will be:
Output:
These numbers are the numbers before using the break statement.
What is Continue Statement in C?
Like break statement, continue statement is also a loop control statement. In comparison to the break statement, continue statement will not enable an exit from the loop. Whereas, the continue statement causes the loop to continue with the next iteration. It will make the program skip the statements that are in between. In short, we can say that the continue statement will allow the loop to continue and to execute the next iteration.
The syntax of the continue statement is given below:
Syntax:
//C program to ilustrate the use of continue statement #include<stdio.h> int main() { int i; for(i=65;i<=122;i++){ if(i>=91&&i<=96){ continue; } else{ printf("%c=%d\t",i,i); } } }
Now let’s have a peek on the output of the above code.
Output: