Understanding the goto statement in C/C++

Filed Under: C++
Goto Statement Featured Image

Programming languages like C and C++ have a provision of jumping over statements unconditionally using the goto statement. Although the usage of goto statement is almost extinct, it is a handy tool in a programmer’s arsenal.

In this article, we will be going through the syntax and usage of the goto statement.

Syntax of goto statement in C/C++

The basic syntax involves two statements – starting and destination of the jump.

// Jump to label = abcd
// Initiation
goto abcd;

...

...

...

// label = abcd
// Destination
abcd:

In the above code snippet, the label is named as abcd. Hence, if we ever need to jump to a label, we will write goto abcd.

Similarly, we can jump to a preceding line of code using goto statement by:

// label = abcd
// Destination
abcd:

...

...

...


// Jump to label = abcd
// Initiation
goto abcd;

In addition to the syntax, goto statement for initiation and destination must be present in the same function. In short, we can not transit between functions using goto commands.


Applications of the goto statement

There are many uses of the classic goto statement. Let us go through each one of them.

1. goto as a code-jumper

One of the basic advantage of goto statement is its ability to bypass certain lines of code without any effort.

#include<iostream>
using namespace std;

int main(){

	// Name and Age
	string name = "luffy";
	int age = 17;

	getAadharCard();

	// If less than 18 years, skip other identification
	if(age < 18)

		// Goto jump initiation
		goto end;

	getPanCard();

	getVoterId();

	getDrivingLicense();

	// Goto Jump Destination
	end:

	return 1;
}

The goto statement may seem a child’s play at first, but it can easily raise complications in a small piece of code.


2. goto statement as a loop

Before the invention of for and while loops, the goto statment was the go-to statement for all programmers. However, the use of goto makes the program complex, and therefore its use is discouraged.

#include<iostream>
using namespace std;

int main(){

	// Iterator
	int i = 0;
	
	// Label name = loop
	// Label destination
	loop:
		cout<<i<<endl;
		// Loop updation
		i++;
		// Condition for the loop
		if(i < 5)
			// Label initiation
			goto loop;

	return 1;
}

Output:

0
1
2
3
4

The logic flow of the code within the main function is as follows:

  • Initialization of the iterator – int i = 0;
  • Printing of the iterator – cout<<i<<endl;
  • Increment of the iterator – i++;
  • Conditional statement check – if(i < 5)
  • Goto statement for jumping to label = loop – goto loop;
  • Label destination – loop:
  • Printing of the iterator – cout<<i<<endl;
  • And this goes on …….

In brief, the execution of code proceeds normally until a goto statement is encountered. However, when the goto is encountered, the flow of code jumps to the label mentioned by goto.


3. Goto statement as a break

A single break statement can end a single loop. Therefore, in order to break through multiple loops, we require conditions and multiple breaks.

This code-intensive method of finishing more than one loops can be simplified by using the goto statement.

#include<iostream>
using namespace std;

int main(){

	// 2D Array
	int arr[][3] = {{0, 0, 1}, {0, 1, 0}, {1, 0, 1}};
	
	// Number of rows
	int r = sizeof(arr)/sizeof(arr[0]); 

	// Outer loop for rows
	for(int i=0; i<r; i++){

		// Inner loop for values
		for(int j=0; j<3; j++){
			
			// Condition if 1 is encountered
			if(arr[i][j] == 1)

				// Goto Jump Initiation
				goto out;
		}
	}

	// Goto Jump destination
	out:	

	return 1;
}

The breaking of multiple loops becomes very convenient for the programmer. Although, during the code-review, the goto statement might catch the reviewer off-guard.


Conclusion

Even though the use of goto statement may look appealing from this article, it still remains a bad programming concept. Therefore, the readers are advised to not make this statement, a daily coding habit.

Thank you for reading. Feel free to comment below for any queries or suggestions.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages