Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

goto
c++
c
statement

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

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring