×
 Introduction
Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
 Loops
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
 Operators
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
 Miscellaneous
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

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

 

goto

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.

 

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.

© Shubham Ramdeo

Site Map
with
© Shubham Ramdeo