Unconditional Branching (goto)

Goto : 

  • Goto statement provides a way for unconditional branching.
  • It used to jump or switch program execution control from one location to other location(anywhere) in a program. 
  • Syntax : goto label;
  • Here goto instructs a computer to jump at location where label is specified in a program.
  • Example Let's consider following block of code:
#include<stdio.h>
int main()
{
   int num;
   labe1:printf("Number is Even");
   label2:printf("Number is Odd");

   printf("Enter number:");
   scanf("%d",&num);
   if(num%2==0)
     goto label1;
   Else
     goto label2;
return 0;
}

  • Here we are printing result using goto statement. 

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.