Jumping statements in C | break statement in C | continue statement in C | goto statement in C | exit in C
There are four types of jumping statements in C language.
1. break statement in C
break is a keyword of C language. break statement in C is used to transfer the control of program out of switch or looping statement.
Program of break statement in C |
#include<stdio.h> int main() { int i; for(i=11;i<=15;i++) { if(i==13) break; printf("\n%d",i); } return(0); } |
Output |
11 12 |
for statement starts with 11 and goes upto 15. When value of i becomes 13, break would execute and control would leave for statement hence nothing after 12 is displayed
2. continue statement in C
continue statement in C is used with looping statements. It skips all statements after it and proceeds with next iteration of a looping statement.
Program of continue statement in C |
#include<stdio.h> int main() { int i; for(i=11;i<=15;i++) { if(i==13) continue; printf("\n%d",i); } return(0); } |
Output |
11 12 14 15 |
for statement starts with 1 and should go upto 5. When value of i becomes 3, continue statement would execute and control would go to the next iteration of for statement i.e. statements after continue would be skipped and value of i would become 4 and so on.
3. goto statement in C
goto statement in C is used to transfer the program control from one part to another part of same program. goto statement in C can be used in two ways:
a. Forward goto
The label appears after goto statement.
Program to use forward goto statement in C |
#include<stdio.h> int main() { printf("\nwelcome"); goto stop; printf("\nHi"); /*Line 1*/ stop: /*Line 2*/ printf("\nBie"); /*Line 3*/ return(0); } |
Output |
Welcome Bie |
Description of above program
In the above program, stop is the label defined by the programmer in Line2. After displaying welcome the control would transfer after the label stop: i.e. Line1 is skipped and control transfers to line 3.
b. Backward goto
In backward goto statement, the label exists before the goto statement.
Program to demonstrate the use of backward goto statement in C |
#include<stdio.h> int main() { int counter=1; start: /*Line 1*/ printf("\n%d",counter); counter++; if(counter<=5) goto start; /*Line 2*/ return(0); } |
Output |
1 2 3 4 5 |
In the above program, start is the label defined by the programmer in Line 1. When the program is executed for the first time, value of counter=1 id displayed. After displaying value of counter, counter is incremented by 1. then condition counter<=5 is checked. If it is true program control transfers to line 1 again. In this way as long as condition is true, value of counter is displayed.
4. exit() function in C
Whenever exit() function is found in a program, the program terminates and no statement after it would be executed.
Program to demonstrate the use of exit() function in C. |
#include<stdio.h> #include<process.h> int main() { int n; printf(”\nEnter an integer=”); scanf(“%d”,&n); if(n>0) exit(0); else printf(“\n%d”,n); return(0); } |
Output |
Enter an integer value=10 Program terminated.. |
In the above program, If value of a is positive, program would terminated due to exit(0).