Goto Statement - C Language Tutorial [C Programs]

Description

Goto statement is mainly used when program becomes large and unreadable for the user. And also to pass the control from one location of the program to another.

Syntax

goto label;
//statements
label:
// code

Source code

//Using goto statement
#include <stdio.h>
#include <conio.h>
void main()
{
int dividend,divisor,quotient,remainder;
char ch;
clrscr();
do
{
printf("Enter dividend : ");
scanf("%d", & dividend);
printf("Enter divisor : ");
scanf("%d",&divisor);
        if(divisor==0)
            {
             goto input;
}
quotient=divident/divisor;
remainder=divident%divisor;
printf("Quotient is : %d ,",quotient);
printf("Remainder is : %d",remainder);
printf("\nDo you want to do another calculation? (y/n)");
scanf("%c",&ch);
}while(ch!='n');
   input:
   printf("Invalid divisor entered, quiting the program");
getch();

}

Program working steps

This program is similar to previous continue statement program. The difference is in the statement used, here we have just used goto statement thats it.
When goto input statement on line no.17 is executed, then the program control jumps to the line no.26 and prints the statement 'Invalid divisor entered, quiting the program'.

Thank you for sharing Your Knowledge with others who need It.-Team LetML

Post a Comment

Copyright © LetML. Blogger Templates Designed by OddThemes