goto statement in control statements - C-Tutorial

Latest

Sunday, 9 October 2016

goto statement in control statements

goto statement:           The goto statement is used to alter the normal sequence of program execution by transferring the control to some other part of the program.  In its general form, the goto statement can be written as:


            
                   Syntax:           goto Label;

Where,
            Label is an identifier used to specify the target statements to which control will be necessary to transfer.   The target statements must be labeled and the label must be followed by a colon as:

            Syntax:           Label : Statements

Each label statement with in the program must have a unique name.

Depending on passing the control, goto statements can be classified as forward jump and backward jump.



Example program for Goto statement:

#include<stdio.h>
#include<conio.h>
#include<math.h>

main()
{
int n,s;
clrscr();
read:
printf("\n Enter n value: ");
scanf("%d",&n);
if(n>0)
{
goto square;
}
else
{
goto read;
}
square:
s=sqrt(n);
printf("\n Square root value = %d",s);
getch();
}

No comments:

Post a Comment