Here we are going to write c program to display first five odd numbers using goto statement.
goto statement is a jump statement which is used to create unconditional jumps from goto to a labeled statement in the program i.e., to jump from one part of the code to any other part of the same code.
//goto statement syntax:
goto label;
.......
.......
label:
.......
.......C Program to display First Five Odd Numbers using goto statement-
#include <stdio.h>
int main()
{
int i=1,c=1;
beg:
if(i%2==1)
{
printf("%d\n",i);
c++;
}
i++;
if(c<=5)
{
goto beg;
}
return 0;
}Output-
1
3
5
7
9goto statement is rarely used as it makes the program very complex and confusing to understand. Using goto statement makes the program very difficult to verify its correctness.
Instead use statements like break and continue.
Post a Comment
If you have any doubts, please let me know