➤ If-else Statement in C
➤ Programs on if-else
➤ Switch Case in C
➤ Switch case Programs
➤ Conditional Operator
➤ While loop in C
➤ Do-while loop in C
➤ While vs do-while
➤ For loop in C
➤ Break keyword in C
➤ Continue keyword in C
➤ Break vs Exit in C
➤ Goto keyword in C
☕️ Flow Control Programs
➤ Largest in 3 Numbers
➤ Find Grade of student
➤ Find the absolute value
➤ Vowel or Consonant
➤ Leap Year Program
➤ Simple calculator in C
➤ Check Odd or Even
➤ Roots of Quadratic Equation
➤ Find Reverse of Number
➤ Factors of a number in C
➤ Generate Multiplication table
➤ Find Power of a Number
➤ Find GCD and LCM
➤ Find factorial of Number
➤ Count Number of Digits
➤ Sum of digits in Number
➤ Sum of N Natural Numbers
➤ Sum of Squares of Natural No.
➤ Find Sum of Odd Numbers
➤ Find the Sum of Series
➤ Find Fibonacci series in C
➤ Sum of the Fibonacci series
➤ Sum until enters +ve numbers
➤ Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
➤ Celsius to Fahrenheit
➤ Fahrenheit to Celsius
➤ Decimal ↔ Binary
➤ Decimal ↔ Octal
➤ Octal ↔ Binary in C
☕️ Number Programs in C
➤ Prime Number in C
➤ Strong Number in C
➤ Krishnamurthy Number
➤ Neon Number in C
➤ Palindrome number
➤ Perfect Number in C
➤ Armstrong Number
☕️ Pattern Programs in C
➤ Pattern programs in C
➤ Printing pattern using loops
➤ Floyd’s triangle Program
➤ Pascal Triangle Program
➤ Pyramid Star Pattern in C
➤ Diamond Pattern Programs
➤ Half Diamond pattern in C
➤ Print Diamond Pattern
➤ Hollow Diamond Pattern
➤ Diamond Pattern of Numbers
In this tutorial, you will learn the C goto statement with examples. What is the syntax to use the goto statement and how to use it in our program?
The goto statement is another type of control statement supported by C. The control is unconditionally transferred to the statement associated with the label specified in the goto statement.
goto syntax in C
Syntax:- goto label_name;
A statement label is defined in exactly the same way as a variable name, which is a sequence of letters and digits, the first of which must be a letter. The statement liable must be followed by a colon (:). Like other statements, the goto statement ends with a semicolon.
The goto is an unconditional branching statement and its use is discouraged in structured programming. In a language that provides the various loop statements like ‘for’, ‘while’, and ‘do-while’ as well as the switch statement, there is normally no need to use the ‘goto’ statement. Often, a break statement or a continue statement can eliminate the need for a goto statement.
Because the goto statement can interfere with the normal sequence of processing, it makes a program more difficult to read and maintain.
Examples of C goto
Program1:- Print first N natural numbers in C programming using the goto statement.
#include<stdio.h>
int main()
{
int n, i=1;
printf("Enter a number: ");
scanf("%d",&n);
start:
printf("%d\t",i);
i++;
if(i<n) goto start;
return 0;
}
Output:-
Enter a number: 10 1 2 3 4 5 6 7 8 9
In this program, the goto works similarly to do-while loop. Assume the label start
as do
, and goto start
as while
, and if(i<n)
as the condition
of the do-while loop. First, the statements are executed and then the condition is checked.
Program2:- Write a program to find the sum of positive numbers in C programming using goto statements. The program should take input repeatedly until the user enters a negative number.
#include<stdio.h>
int main()
{
int n, sum=0;
start:
printf("Enter a number: ");
scanf("%d",&n);
if(n<0) goto end;
sum = sum + n;
goto start;
end:
printf("Sum = %d",sum);
return 0;
}
Output:-
Enter a number: 10
Enter a number: 15
Enter a number: -5
Sum = 25
In this program, two goto statements are used. The first goto statement (goto end and label end) works as a break statement and the second goto statement (goto start and label start) works as a loop.
Use of goto statement
The goto statement is used:-
- To execute a group of statements repeatedly for a particular number of times.
- To come out of several nested loops at one stroke.
Program3:- Write a program to find factorial of a given number in C programming using the goto statement.
#include<stdio.h>
int main()
{
int num;
long fact = 1;
printf("Enter a number: ");
scanf("%d",&num);
if(num<0) goto end;
for(int i=1; i<=num; i++)
fact = fact * i;
printf("Factorial of %d is = %ld", num, fact);
end:
return 0;
}
Output:-
Enter a number: 5
Factorial of 5 is = 120
In the above program for calculating the factorial, we used a goto statement and for
loop. We can do the same without using for loop also. The below program is without for loop.
#include<stdio.h>
int main()
{
int num;
long fact = 1;
printf("Enter a number: ");
scanf("%d",&num);
if(num<0) goto end;
int i=1;
loop:
fact = fact * i;
i++;
if(i<=num) goto loop;
printf("Factorial of %d is = %ld", num, fact);
end:
return 0;
}
Output:-
Enter a number: 5
Factorial of 5 is = 120
Enter a number: 6
Factorial of 6 is = 720
Enter a number: 7
Factorial of 7 is = 5040
Note:- Generally, we don’t use the goto statement in C programming. It is recommended to use loops instead of using goto statement. Use goto statement, only if loops can’t do that operation.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Nice answer back in return of this issue with real arguments and explaining everything concerning that.