WHAT'S NEW?
Loading...

goto Statement in C Programming (Example)

The Goto statement in C Programming is used to alter the flow of a program. When the compiler reaches the goto statement then it will jump unconditionally in both forward and backward to the location specified in the goto statement.
 Syntax 
       goto  label
        .............
        .............
        label:

Example  1: Program to print Pass or Fail using goto Statement in c.

#include<stdio.h>
void main(){
 int m; 
 printf("Enter your marks : "); 
 scanf("%d",&m); 
 if(m>=40)
  goto pass; 
 else
  goto fail; 
 
 pass: 
 printf("You are Passed !!"); 
 goto last;  
 
 fail: 
 printf("Try Again !!"); 
 
 last:
 getch(); 
}
Output
Enter your marks : 60
You are Passed !!

Example 2: Program to print the natural numbers from 1 to 10 using goto statement in C. 
#include<stdio.h>
void main(){
 int i = 1; 
 top: 
 printf("%d\n",i); 
 i++; 
 if(i<=10)
  goto top; 
 getch(); 
}
Output
1
2
3
4
5
6
7
8
9
10

 Example 3:Program to print multiplication table of given number using goto statement in C. 
 
#include<stdio.h>
void main(){
 int i = 1; 
 top: 
 printf("%d\n",i); 
 i++; 
 if(i<=10)
  goto top; 
 getch(); 
}
Output

NOTE: Although all the programming languages supports goto statement. However, it is always good practice to avoid using it or at least minimise the usage. Instead of using goto statement, we can use the alternatives such as Break, Continue statements.

0 comments:

Post a Comment