fuddlepixelFuddlepixel coz tech is fun();

Using Goto and Label

Let us discuss how can we jump between different parts of our code using the goto and label statements. Also why to avoid it ?

By Shubham Ramdeo
21 September 2015 · 6 mins read
# c  newbie 

Using Goto and Label

That’ how we can jump between codes, using goto statement !

Welcome to the Newbie Programmer Series. So, we were learning about controlling the flow and In the previous post (click here), we have learned how to use the conditional statements, the if and else to decide and make choices inside our program based on certain conditions. In this post, we will discuss the ‘goto’. Using the goto, we can control the flow further as now we will be able to jump in between our code. It’s a little but important to know concept. So, if you are new to this series, please go to index(click here) and read out all the previous parts so that you can easily understand what I am talking about.

The theory behind goto is very simple. In our code, we can make some check points, or what we will call as label. Labels are not any separate block of code, it’s just like a bookmark. If a book is the code, and then labels are its bookmarks. We name these bookmarks for example A, B, C and when needed, we say “goto A”, the reader will turn the pages to bookmark A and will continue reading.

Now discuss the coding stuff. Please continue reading below.

Lets first of all see how to use it :

. . .
goto label;
. . .
. . .
label :
. . .

this label can be anywhere in the program

For example in the code you can do like this :

. . . . . . . . . .
. . . . . . . . . .
try :
. . . . . . . . . .
. . . . . . . . . .
if ( try failed )
goto try
. . . . . . . . . .
. . . . . . . . . .

Or something like this

. . . . . . . . . .
. . . . . . . . . .
if ( got error )
goto error
. . . . . . . . . .
. . . . . . . . . .
error :
. . . . . . . . . .
. . . . . . . . . .

The code will continue in the same up to down but when got a goto, the program will move to where the goto is telling to go for and then again continue running up to down.

Lets do some example to make this all more practical.

#include<stdio.h>
main()
{
printf("hello \n");
goto bye;
printf("Welcome \n");
printf("Thankyou \n");
bye:
printf("Bye ! \n");
}

OUTPUT :

hello
Bye !

You see, due to the goto statement, program jumped to bye and all the other statements skipped.

Looping

Do you know we can make awesome loops using goto.

#include<stdio.h>
main()
{
int num;
try:
printf("Please enter 5 \n");
scanf("%d",&num);
if(num == 5)
{
goto thanks;
}
else
{
goto try;
}
thanks:
printf("Thankyou !!! \n"); 
}

OUTPUT :

Please enter 5
4
Please enter 5
2
Please enter 5
166
Please enter 5
54
Please enter 5
5
Thankyou !!!

What is happening here is that the program will ask you for a number. And then it check if its 5. If it is 5, then the program will jump to the thanks label and will thank you. If it’s not 5, it will jump to try label. The try label is at the starting of the program and repeats everything again. So its like repeating the program again if you have not entered 5 and this makes a loop. The only way to come out of this loop is to enter 5. Other wise, the loop is for infinite !

Error handling

There is a lot of different ways people use goto. But the best use is for error handling. Suppose we have are making a large project. This will consist of a large amount of code. In the program, we will get a large amount of errors. What we can do is that we can make a label to handle error and if when we get any error, we will use goto to jump to the error label which can solve the trouble. Like the following piece :

. . . . . . . . . .
// Part A of code
. . . . . . . . . .
. . . . . . . . . .
if ( error )
goto error;
. . . . . . . . . .
. . . . . . . . . .
// Part B of code
. . . . . . . . . .
. . . . . . . . . .
if ( error )
goto error;
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
// Part B of code
. . . . . . . . . .
. . . . . . . . . .
if ( error )
goto error;
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
error :
// code to solve the error

Why not to use goto ?

The creator of the C language, the great Dennis M. Ritchie himself discourage the use of goto in his book The C programming Language. Most of the popular coders and software engineers have the same view. This is because using goto in large amount can make the code difficult to understand. It makes the logic of code complex and tangled. This statement is considered a bad practice. Any program can be written in C without using goto. We will learn them later.

So that’s it for today. In the next post, we learn another awesome thing to control the flow. So stay connected !

Any doubts ? Meet me at twitter @ramdeoshubham

Please share this post as much as you can so that we can get connected to more geeks who wants to learn programming but have no clue. Thanks for reading :) (c)Shubham Ramdeo, All rights reserved.


Share this :



Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

I'm in ♥


Or follow us
   


About author

Shubham Ramdeo

Shubham Ramdeo

Shubham Ramdeo is the founder of Fuddlepixel. He is a geek, a writer, involved with many technical and humanity projects.

Meet me!



Recent





Related

c 

newbie 

Top