0

C goto Statement


The goto statement looks like:

  statements...;
  goto label;
  statements..;
label:
  statements...;

Below is the sample code for the statement:

/*
 *
 * goto.c
 *
 * Demonstrate the use (or misuse) of the goto statement
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>

main() {
 int age;
 char name[41];
 int very_old;

 printf("Please enter your age: ");
 scanf("%d", &age);
 printf("You are %d years oldn", age);

 if (!(age <= 19 && age >= 13))
   goto other;
 if (age > 19 || age < 13)
   printf("You are not a teenagern");

up:
 if (age == 10 || age == 20 || age == 30 || age > 100)
   printf("You have a special agen");

 printf("Please enter your name: ");
 scanf("%s", name);

 if (!(strcmp(name, "Bruce") != 0 && age != 40))
   printf("You not called Bruce and are aged 40.n");
 if (strcmp(name, "Bruce") == 0 || age == 40)
   printf("You not called Bruce and are aged 40.n");

other:
 very_old = age > 80;

 if (!very_old)
   printf("You are not very oldn");
 if (very_old == 0)
   printf("You are not very oldn");

 if (age > 10)
   goto up;

 fflush(stdin);
 getchar();
}

Most programmers often don’t use the goto statement because it makes the code unreadable. This statement is used to loop between the code process. I think that it is best used to evaluate inputted value. For instance,

  printf("Please enter your age: ");
input:
  scanf("%d", &age);

  if (age <= 0) {
    printf("You entered an invalid age. Please re-enter: ");
    goto input;
  }


There are no posts related to C goto Statement.