Un-conditional statements break, continue, and goto in C Programming
Following are some unconditional or jump statements in C
programming
- break
- continue
- goto
Break statement
The Break statement is used to exit from
while, for, do while, or switch structures. It can only be used inside the body
of for, while, do-while or switch statements. The break statement was written simply as a break without any embedded expressions or statements.
Syntax:
Example
1 #include <stdio.h> #include<conio.h> int main() { int num =0; while(num<=100) { printf("value of variable num
is: %d\n", num); if (num==2) { break; } num++; } printf("Out of while-loop"); getch(); } |
Output
value of variable num is: 0 value of variable num is: 1 value of variable num is: 2 Out of while-loop
|
Example 2 #include <stdio.h> #include<conio.h> int main() { int var; for (var =100; var>=10; var --) { printf("var: %d\n",
var); if (var==99) { break; } } printf("Out of for-loop"); getch(); }
|
Output
var: 100 var: 99 Out of for-loop
|
Continue statement
The continue statement skips the
remaining statements in the body of a while, for, or do while structure. The
continue statement can be included within a while, do-while or for statements. It is written simply s continue; without any
embedded statements or expressions.
Syntax:
Example:
1 #include <stdio.h> #include<conio.h> int main() { for (int j=0; j<=8; j++) { if (j==4) { /* The continue statement is encountered
when the value
of j is equal to 4.*/ continue; } /* This print statement would not
execute for the * loop iteration where j ==4
because in that case * this statement would be skipped. */ printf("%d ", j); } getch(); }
|
Output
0 1 2 3 5 6 7
8 Note: Note: Value 4 is missing in the output, why? When the value of variable j is 4, the program encountered a continue statement, which makes the control jump at the beginning of the for loop for the next iteration, skipping the statements for the current iteration (that’s the reason printf didn’t execute when j is equal to 4). |
Example 2 #include <stdio.h> #include<conio.h> int main() { int counter=10; while (counter >=0) { if (counter= =7) { counter--; continue; } printf("%d
", counter); counter--; } getch(); }
|
Output
10 9 8 6 5 4 3
2 1 0
Note: The print statement is quit for
loop when the value of the counter was 7.
|
Difference between break and continue
Break |
Continue |
1. This statement is used to terminate the
switch and loop.
2. This statement terminates the whole
operation of the loop.
3. The break keyword is used for the break.
4.
Syntax: break; 5. Example: #include<stdio.h> main() { int n; for(n=1;n<=5;n++) { if(n==3) continue; } printf("number = %d\n",
n); } getch(); |
1. This statement is used to bypass the
execution of the statement.
2. It passes only one (single) execution of
the loop.
3. The continue keyword is used for the continue statement.
4. Syntax: continue;
5. Example: #include<stdio.h> main() { int n; for(n=1;n<=5;n++) { if(n==3) continue; } printf("number = %d\n",
n); } getch();
|
goto statement
The Goto statement is used to alter the
normal sequence of program execution by transferring control to the other part
of the current function. The goto statement is written as a goto label; where the label
is an identifier that is used to label the target statement to which control
will be transferred.
Syntax:
Example:
#include<stdio.h>
#include<conio.h>
void main(){
clrscr() ;
printf("We are at first printf statement!!!\n") ;
goto last ;
printf("We are at second printf statement!!!\n") ;
printf("We are at third printf statement!!!\n") ;
last: printf("We are at last printf statement!!!\n") ;
getch() ;
}
Output:
No comments:
Post a Comment