There are three different controls used to jump from one C program statement to another and make the execution of the programming procedure fast. These three Jumping controls are:
- goto statment
- break statment
- continue statment
It is also sometimes convenient to be able to exit from a loop other than by testing the loop termination condition at the top or bottom. These statements are useful in that case as well.
goto statement
The powerful Jumping statement in C-Language is goto statement. It is sometimes also called part of branching statement. The goto moves the control on a specified address called label or label name. The goto is mainly of two types. One is conditional and the other is unconditional.
Example Program:
/*The following program using goto statement*/ #include <stdio.h> #include <conio.h> void main() { int l; clrscr(); Laura: //here Laura is the name of goto Label printf("Enter any No."); scanf("%d",&l); if(l==5) { goto Laura; } printf("\n%d",l); getch(); }
break statement
Break is always used with then decision-making statement like if and switch statments. The statement will quit from the loop when the condition is true.
The general syntax for break statement is as:
break;
Example Program:
/*The following program using break statement*/ #include <stdio.h> #include <conio.h> void main() { int i=1; clrscr(); while(i<=10) { if(i==6) { break; } printf("\n I=%d",i); i++; } getch(); }
Output is as follows:
I=1 I=2 I=3 I=4 I=5
Continue statement
Continue statement also comes with if statement. This statement is also used within any loop statement like do loop, while loop, and for statement.
The general syntax for continue statement is as:
continue;
This statement has skipped some part of iteration (loop) and comes to the next looping step i.e. it will increment/decrement the loop value, when continue occurs.
/*The following program using continue statement*/ #include <stdio.h> #include <conio.h> void main() { int i=1; clrscr(); while(i<=10) { if(i==6) { continue; } printf("\n I=%d",i); i++; } getch(); }
Output is as follows:
I=1 I=2 I=3 I=4 I=5 I=7 I=8 I=9 I=10