Goto Statement


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

  1. //Using continue statement
  2. #include <stdio.h>
  3. #include <conio.h>
  4. void main()
  5. {
  6. int dividend,divisor,quotient,remainder;
  7. char ch;
  8. clrscr();
  9. do
  10. {
  11. printf("Enter dividend : ");
  12. scanf("%d", & dividend);
  13. printf("Enter divisor : ");
  14. scanf("%d",&divisor);
  15. if(divisor==0)
  16. {
  17. goto input;
  18. }
  19. quotient=divident/divisor;
  20. remainder=divident%divisor;
  21. printf("Quotient is : %d ,",quotient);
  22. printf("Remainder is : %d",remainder);
  23. printf("\nDo you want to do another calculation? (y/n)");
  24. scanf("%c",&ch);
  25. }while(ch!='n');
  26. input:
  27. printf("Invalid divisor entered, quiting the program");
  28. getch();
  29. }

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'.