Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not GOTO Statement? [closed]

Tags:

java

c++

c

c#

I am doing Masters in Software Engineering. From College to University i heard from my teachers and instructors that never use GOTO statement in Programming Language.According to Software Engineering By D. Sundar

USE of goto statements makes a program unstructured and makes it very difficult to understand.

and i also read the same in book by MICROSOFT on its first page that never use GOTO Statmement in Programming.

It always arises question in my mind that if we are instructed that never use GOTO Statement then why it is the part of many common programming languages ?

like image 239
Usman YousafZai Avatar asked Nov 04 '13 10:11

Usman YousafZai


People also ask

Why should we avoid goto statements?

NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them.

What is the biggest problem with goto statement?

"The GOTO statement is generally considered to be a poor programming practice that leads to unwieldy programs. Its use should be avoided."

Is goto statement still used?

While overall usage of gotos has been declining, there are still situations in some languages where a goto provides the shortest and most straightforward way to express a program's logic (while it is possible to express the same logic without gotos, the equivalent code will be longer and often more difficult to ...


2 Answers

At least for some programming languages (none of which I'm the designer of, obviously) like C and C++, I imagine it's there because it

  1. Closely models what the hardware really can do, which is kind of the goal in these languages.
  2. Is sometimes actually useful and better than more "structured" approaches.

The most common example of when goto is often considered good is when doing nested error/resource management in C. See for instance the Linux kernel, a fairly large and successful C project which uses goto for this purpose.

like image 163
unwind Avatar answered Oct 18 '22 05:10

unwind


You want to read the seminal "Go to statement considered harmful" by Edsger W. Dijkstra (1968!)

like image 43
Tristan Brindle Avatar answered Oct 18 '22 04:10

Tristan Brindle