Easy Tutorial
❮ C Do While Loop C Exercise Example7 ❯

C goto Statement

C Loops

The goto statement in C language allows the control to be unconditionally transferred to a labeled statement within the same function.

Note: The use of goto statements is not recommended in any programming language because it makes the control flow of the program difficult to trace, making the program hard to understand and modify. Any program that uses goto statements can be rewritten in a way that does not require the use of goto statements.

Syntax

The syntax for the goto statement in C:

goto label;
..
.
label: statement;

Here, label can be any plain text other than a C keyword, and it can be set before or after the goto statement in the C program.

Flowchart

Example

#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;
}

When the above code is compiled and executed, it produces the following result:

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

C Loops

❮ C Do While Loop C Exercise Example7 ❯