Trusted answers to developer questions

What is the goto statement in C/C++?

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

The goto statement alters the normal sequence of execution in a C/C++ program; it is similar to an​ unconditional jump.

Syntax

svg viewer

The label is an identifier. When the goto statement is encountered, control of the program jumps to label:, and the code starts executing after it.

Code

The following example uses the goto statement in a function that checks whether or not​ a number is a multiple of 33:

#include <iostream>
using namespace std;
// function to check even or not
void multipleOfThree(int num)
{
if (num % 3 == 0)
goto multiple;
else
goto notMultiple;
multiple:
cout << num << " is a multiple of 3." << endl;
return; // return if even
notMultiple:
cout << num << " is not a multiple of 3." << endl;
}
// Driver code
int main()
{
int num1 = 18;
multipleOfThree(num1);
int num2 = 19;
multipleOfThree(num2);
}

​While the goto statement appears to be very useful, its use is discouraged because, more often than not, it makes the program logic complex, difficult to follow, and creates complications when modifying the code. Therefore, one must be very cautious when using the goto statement.

RELATED TAGS

goto
c++
c
statement
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?