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(); }
Enter your marks : 60
You are Passed !!
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(); }
1
2
3
4
5
6
7
8
9
10
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(); }
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