C++

C++ Goto

While creating any program, we encounter such a situation in which we need to visit any particular part of the program directly without waiting for the line-by-line execution of the code or by making the loop to complete its execution. A C++ goto statement is a solution to this scenario.

C++ Goto Statement

This statement is the one that is used as a jump statement. This statement is used to jump from any point in the code to anywhere inside the function. This is an unconditional statement that is used to transfer the control of a program to any other command line. The flow of execution is moved to a specific location inside the function.

Syntax:

Sample:q   
{ }
Goto Sample;

A sample is any feature, statement, loop, or operation inside the function. It must be labeled to be identified by the goto statement. The goto statement contains the ‘goto’ keyword along with the name of the identifier. This is the user-defined identifier.

Implementation of Goto Statement in C++

Example 1

The program will display the sequence of numbers starting from a specific number and will go incrementing till the last number is attained. First, we will introduce a library.

#include <iostream>

This library is required for each c++ program to get executed as it enables the cin and cout functions to work properly. After that in the main program, we will declare a simple integer type variable with a value.

To ensure the usage of the goto statement or to make it feasible, a specific name is given to the function or any feature inside the main program or maybe a loop that we want to repeat by using got statement. Similarly, here we have used a do-while loop and this loop is given a name ‘loop’ as you can see that.

LOOP: do { }

So in this way, it will be easier for the goto statement to find that specific place where it wants to jump. Here the goto statement acts as a recursive call to a function.

The loop contains the condition that the loop should continue executing and add 1 in each present number. A condition is also applied here in which if-statement specifies that if the number is equal to 15 or you can add any number to specify any condition, then the iteration is skipped, and else, add all the numbers with one. After that step, the goto statement is used to start the execution of a loop again.

Goto LOOP;

Outside the loop body, the values are displayed and again the addition statement is written.

A = a+ 1;
Now, the do body is closed and the while statement contains the condition that the numbers should be smaller than 10.

While ( a < 10)

After using this program, save the respective file with the extension of ‘.c’. to execute the file, we need to add a C++ compiler, that is G++. This compiler loads the input file, compile it, and execute the code by displaying the resultant value in the output file.

$ g++ -o goto goto.c
$ ./goto

The resultant value shows that the value of the variable that starts from 5 will be incremented according to the statement and will last until the number 10 is attained.

Example 2

This example, unlike the previous one, involves the user demonstrating the working of the goto statement. But first, we will use the same iostream library to get the values through cin and display by using cout. So inside the main program, we use three variables of float type. One variable will take the number from the user. The second is the average to calculate the average of the numbers, whereas the third one will calculate the sum of all numbers, as the average function is linked with the sum function. More two variables are used here that are integer types. One will take the number of inputs from the user and the other variable is used to iterate the loop, we will use in the program.

A message is displayed to ask the user to enter the number of inputs he wants to enter so that the average is to be calculated.

The user will enter the number. Now, we will use this number in the loop to execute each time before the relevant number. In each iteration, the user will be asked to enter a number. Inside the loop, an if statement is used to check if the user has entered the negative value, then the control goes towards the function we have used separately to calculate the average. In this situation, the goto statement act as a function call.

Goto jump;

Outside the loop, the sum will contain the total of all the entered numbers. Now, consider the jump portion of the program. The average formula is used here to calculate the average

Average = sum/ (I – 1);
And all the average calculated will be displayed.

Now, compile the code to see the resultant values.

$ g++ -o goto goto.c
$ ./goto

On execution, the user will enter the number, he wants to enter the number of values for the average calculation. After that, he will enter all the numbers line-wise. And at the end average will be calculated.

Example 3

This example will display the message that either the number is even or it is odd. Each time in the if-else statement goto statement is used. So, let us start the program. This function will take the number in the parameter. Inside the function, the statement will check if the modulus of the entered number is zero. It means that the number is even, so go to the even part.

If (num % 2 = 0)

Goto Even

Whereas in the else part, if the modulus by 2 is not equal to zero then;

Goto Odd

Both the even and the odd portions contain the cout statement to display the message accordingly. The function body is close now. In the main program, a number is initiated to the variable and then this variable is passed to the function through the parameter.

Now, execute the code in the file. It will display that the number is even. And this specific message was displayed by the even portion inside the function.

Drawbacks of Using GOTO Statement

  • There are some reasons that experts say to avoid the use of the goto statement in any programming language. It is a good point that it gives us the ability to jump to any required part of the source code, but it makes the logic very complex and makes it hard to understand by the user.
  • This goto statement is replaceable with the break and the continue statements in C++.

Conclusion

To summarize the topic ‘Goto C++’, we will throw light on the basic description of the goto statement, it’s working, and the examples. There are many ways to use this statement inside the source code, but every time the purpose of using it is the same. We have used some easily understandable examples to elaborate the working of this statement.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.