Friday, July 29, 2016

C Basics - The goto Statement in C

The goto statement allows to take the control where you want. A goto statement in C programming provides an unconditional jump from the goto to a labeled statement in the same function.

The syntax for a goto statement is,

goto label;
.
.
label: statement;

Here label can be any plain text except C keyword and it can be set anywhere in the C program above or below to goto statement.

#include <stdio.h>
 
int main () {

   int i = 1;

   LOOP:do {
   
      if( i == 5) {
        i++; 
 goto LOOP;
      }
      if( i == 8) {
        goto END;
      }
  
      printf("value of i is: %d\n", i);
      i++;
     
   } while( i < 10 );
 
END: 
 printf("Ending execution");

   return 0;
}

Output of above program,

value of i is: 1
value of i is: 2
value of i is: 3
value of i is: 4
value of i is: 6
value of i is: 7
Ending execution



Related topics:
Loops in C   |   The for Loop in C   |   The while Loop in C   |   The do-while Loop in C   |   Nested Loop in C   |   Infinite Loop in C   |   Loop Control Statements in C   |   The break Statement in C   |   The goto Statement in C   |   The return Statement in C

List of topics: C Programming

No comments:

Post a Comment