473,657 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what's the prob with goto

i m new to the C world...

i ve been told by my instructors not to use goto stmt..
but no one could give me a satisfactory answer as to why it is so..

plz help me out of this dilemma, coz i use a lot of goto in my codes....

Nov 15 '05 #1
28 2714
Even though goto were not used directly, but they are implicitly part
of control constructs of a programming lang.

gotos would reduce the redability of the program. If the programming
spanning across pages, it owuld be really difficult to understand the
programs with gotos.

Nov 15 '05 #2
Vishal Naidu said:
i m new to the C world...

i ve been told by my instructors not to use goto stmt..
A wise student obeys his instructors unless he knows them to be wrong.
but no one could give me a satisfactory answer as to why it is so..
Not all instructors understand the advice they give. That doesn't
necessarily mean it is bad advice.
plz help me out of this dilemma, coz i use a lot of goto in my codes....


The problem is that goto doesn't scale well. The more you use it, the harder
your programs will be to maintain, because the flow of control of your
program becomes harder to read. This phenomenon is known as "spaghetti
code", and with good reason.

In many organisations, goto is actually banned altogether. So it's a good
idea to learn how to program without it. Once you have done so, you'll
wonder what you ever saw in it.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29 July 1999
http://www.cpax.org.uk
Email rjh at the above domain

Nov 15 '05 #3

Vishal Naidu wrote:
i m new to the C world...

i ve been told by my instructors not to use goto stmt..
but no one could give me a satisfactory answer as to why it is so..

plz help me out of this dilemma, coz i use a lot of goto in my codes....


Bad structured code is orders of magnitude easier to debug and maintain
than bad unstructured code.

You shouldn't need to use goto very often. One problem with using goto
is that it can destroy the ability to debug code by inspection. It's
not so much the goto itself that's the problem, but rather than the
uncertainty introduced in the code around the target of the goto. For
instance, given the code fragment:

i = 7;
label: printf("%d\n", i);

What value gets printed for any particular path of execution? Until I
account for every instance of "goto label;", I don't know. I have to
trace all possible paths of execution before I can say with any
confidence what gets printed under what conditions. This makes code
harder to debug and maintain. God forbid I have to make any changes,
because I can't say with any confidence that any change will not
inadvertantly affect *some* path of execution.

The story I like to tell is one program I stumbled across early in my
career written by an electrical engineer. A 5000-line main() function,
with something 15 gotos, some of which branched forward, some backward.
It took my co-worker two full weeks to figure out how the damn thing
actually worked. We were tasked with improving performance of the app,
but anything we did simply broke the code. We finally recommended that
the customer just buy faster hardware; it would be cheaper than the
effort to make the code faster would cost.

Now, that particular application was pathological beyond the use of
gotos, but they made a bad situation impossible. Had the code been
somewhat structured, we probably could have made some improvements.

Gotos are useful in a limited set of circumstances, like when you need
to break out of a deeply nested loop, or need to make sure some cleanup
code runs before exiting a routine. But they should not replace
control structures like if-else , for, while, do-while, or switch
statements.

If you're going to use a goto, make sure you branch forward *only*, and
that you do not branch into a block.

Nov 15 '05 #4
Richard Heathfield wrote:
The problem is that goto doesn't scale well. The more you use it, the harder
your programs will be to maintain, because the flow of control of your
program becomes harder to read. This phenomenon is known as "spaghetti
code", and with good reason.

In many organisations, goto is actually banned altogether.


<snip>

But that's okay, because you can achieve similar functionality with
a for loop, a switch statement and liberal application of control
variables. <g>

--
imalone
Nov 15 '05 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vishal Naidu wrote:
i m new to the C world...

i ve been told by my instructors not to use goto stmt..
but no one could give me a satisfactory answer as to why it is so..

plz help me out of this dilemma, coz i use a lot of goto in my codes....


Please read "Goto Statement Considered Harmfull" by Dr. Dijkstra, in the
"Communicat ions of the ACM". You can find a public version of this paper at
http://www.acm.org/classics/oct95/
- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDFu44agV FX4UWr64RArKPAJ 9XQL0H+A5gRyx3q ECnM38veR4RjACe Idks
lhUo4XErlOIavJP/3iqLmQk=
=j7B8
-----END PGP SIGNATURE-----
Nov 15 '05 #6
ajm
Vishal,

John's point about goto into a block is real important, for example if
you skip past the initialisation of certain local variables then the
results can be "undefined" - e.g., static local variables. The C
reference manual (and doubtless other publications) make specific note
of this issue so you have been warned ;)

ajm.

Nov 15 '05 #7
ajm wrote:
Vishal,

John's point about goto into a block is real important, for example if
you skip past the initialisation of certain local variables then the
results can be "undefined" - e.g., static local variables. The C
reference manual (and doubtless other publications) make specific note
of this issue so you have been warned ;)


The warning is good, but the detail is not: a static
variable cannot be uninitialized, and furthermore its
initialization occurs before the program starts and thus
does not depend on the run-time flow of execution. The
problem ajm mentions afflicts auto and register variables,
but not statics.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #8
ajm
apologies! yes of course that is what I meant to say (statics are
handled entirely differently as Eric points out)

Nov 15 '05 #9
"Vishal Naidu" writes:
i m new to the C world...

i ve been told by my instructors not to use goto stmt..
but no one could give me a satisfactory answer as to why it is so..

plz help me out of this dilemma, coz i use a lot of goto in my codes....


If the instructor had taught the course properly you wouldn't be using a lot
of gotos, because you wouldn't even no the stmt existed till the last day of
the course.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2052
by: Dave | last post by:
hi another simple problem sorry. i've got a string, "Buffer", and an int, Loop. Somehow, this: Buffer.length<=Loop+1 gives this: error C2296: '<=' : illegal, left operand has type 'unsigned int (__thiscall
51
13348
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this wrong????? Function x select case z
1
1287
by: David B | last post by:
Have this code to send a xls file. Works fine if the To = "sid@home.com" I would like to send from the customer form to the address in the txt box txtEmailaddress but it seems not to "see" the address -- To = "Forms!! " Can not figure out what I need to do TIA David B
3
386
by: Vishal Naidu | last post by:
i m new to the C world... i ve been told by my instructors not to use goto stmt.. but no one could give me a satisfactory answer as to why it is so.. plz help me out of this dilemma, coz i use a lot of goto in my codes....
1
1073
by: James R | last post by:
Ok, I'm new at this and learning as I go, but under extreme pressure from work.. So many bugs and little tricky obsticals have got me very far behind, and this is the latest.. I made a simple databinding form, but not using the wizard, because it doesn't use the SQL driver. I'm keeping it simple right now with a very normal table with a primary key 'SIN' (social insurance number). Well I'm following along in the book to do the whole...
23
1773
by: marora | last post by:
I keep hearing about not to use 'go_to' in your code. However, I don't know what's the reasoning behind it? Can any one here shed some light? Thanks in advance, Manish
28
3796
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.