A goto statement is used to jump unconditionally from the goto label statement to a labeled statement in the same function.
Let's take an example on it.
- #include <stdio.h>
- int main () {
- /* local variable definition */
- int a = 10;
- /* do loop execution */
- LOOP:do {
- if( a == 15) {
- /* skip the iteration */
- a = a + 1;
- goto LOOP;
- }
- printf("value of a: %d\n", a);
- a++;
- }while( a < 20 );
- return 0;
- }
The output will be as follows:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Explanation
Inside the while loop a with initial value of 10 will be incremented till 19 the last value before 20. Inside the while block there's an if that when a reaches 15 goto will go back to LOOP after incrementing a one time. For this reason the number 15 will be skipped.
No comments:
Post a Comment