C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

Goto Statement in C with Examples


Abstraction:
         This tutorial describes about the Goto Statement and its working with the help of examples



              As the name indicates, goto statement is used to jump from one place of code to another. It is like linking a code to another. When the compiler sees the goto statement, then the corresponding ‘Label’ code will executed next. Let us see with an example: 
  
#include<stdio.h>
#include <conio.h>
int main()
 {
  printf ("Hello World\n");
  goto Label;
  printf("How are you?");
  printf("Are you Okey?");
  Label:
  printf("Hope you are fine");   
  getch();
 }
What will be the output of above code?
Output:
Gotol statement in c

Yes, skipped these codes of line:

printf("How are you?");
printf("Are you Okey?");

Gotol statement in c example

why?
When compiler find this code: 'goto Label;' then compiler find the word 'Label'  and jump to the codes below Label, that causes skipping of codes in between goto and Label.

Here we saw goto statement causes jumping from one place to another. Note that, here label is user defined word; you can use anything instead of the word Label like read, find, cat, look etc.
There are two types of goto statement. Forward jumping and backward jumping.

Forward Jumping:
Here label comes after the goto statement.
goto label;
…………… 
……………   

Label:
……………
……………

In our first example, that was the forward jumping. That skipped statement between goto and label.

Backward Jumping:

Here label declared before declaring goto.
Label:
………..

………..     

goto label;
……….

Backward jumping causes loop. Here codes between label and goto repeat many times even infinity.

Example:
  • Program to find square root of given number if it is a positive number:
#include<stdio.h>
#include <conio.h>
#include<math.h>
int main()
 {
        int num;
        int answer;
  Label:
  printf ("Enter a number:");
  scanf("%d",&num);
  if(num>0)
  answer = sqrt(num);
  printf("Square root of %d is %d\n",num,answer);   
  goto Label;
  getch();

When reach at ‘goto Label’, jumps to Label. That makes a infinite loop.

how works goto statement in c

Sample Output:
find square root in c using goto statement

Another Example:
  • Program to accept and display a number until entering a negative number 
#include<stdio.h>
#include <conio.h>
#include<math.h>
int main()
{
   int num;
   Read:
   printf ("Enter a number:");
   scanf("%d",&num);
   if(num>0)
    {
      printf("Entered number is %d\n",num);    
      goto Read;
    }
  getch();
}

This also makes a loop until user enter a negative number. 

Sample Output:
Accept and display a number unit enter a negative number in c

Note: Goto statement is not popular and not widely used keyword in C. Also goto is mainly used for menu driven programs.

No comments:

Post a Comment