Goto statement

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 20473 Views - Last Post: 26 December 2012 - 11:47 AM

#1 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 28-September 12

Goto statement

Posted 23 December 2012 - 01:35 AM

hello all
i read in many books that we should not use goto in our code,
but i don't get detail explanation that why we should avoid goto ?

what problems can occur by using goto can any one explain ?and what can replace it for jump,since goto is used for jump.
If they are any previous thread similar to one please post the link
Is This A Good Question/Topic? 0
  • +

Replies To: Goto statement

#2 azemyth  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 12-December 11

Re: Goto statement

Posted 23 December 2012 - 02:44 AM

Simply don't use goto statement. But even if you use it, it wont hurt you and nor your program.
Why? The reason is behind OS memory management concepts. Goto statement brakes the reference of locality which increases page faulting which is not good and so computer performance is decreased.
Nothing can replace goto statement just enjoy the natural flow of your program and dont jump anywhere and don't create spaghetti code. Goto should be banned but it was left because of traditional reasons.
Was This Post Helpful? 1
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Goto statement

Posted 23 December 2012 - 02:50 AM

I'm sure you could find a zillion hits on this topic, with Google, or on any C/C++ forum, but here's the gist of it:

Historically, goto's were used, to simulate loops, because the compilers didn't have them at first - and it was a small pain, but tolerable. The part that made it intolerable was, working with it, seeing it, it took your head and moved it away from what you needed to be thinking about - which is the logic of the code you're writing.

But even THAT was overshadowed by another problem, because everybody found "slick" ways of using goto's - goto here, goto there, goto someplace else. There were no function calls in most languages, you had to use goto's, and later, gosub's, (like a function call).

Not only CAN you write very obfuscated code that way - it's almost a natural that you WILL write code that is logically, difficult to follow. Following the logic is like trying to follow a strand of long spaghetti, in a big serving bowl heaped up with spaghetti noodles. :eek:/> It drives you quite far around the bend, believe me.

And that's no way for the budding programming industry to move forward. Finally, a strong NO GOTO! movement arose, and that got everyone back to their senses - and we had a variety of loops in the compilers by then, making it all possible.

The time to use goto's is when you are in a nested loop, and need a quick exit from all the loops, not just one:


For instance, traversing a 3D array:
for(depth=0;depth<SIZE;depth++) {
   for(row=0;row<numRows;row++) {
      for(col=0;col<numCols;col++) {
         //code to ID something you are searching for
         //and eventually, find. Now you need to exit
         //all these nested loops, quickly. 

         goto done;
      }
   }
}
done:


You could set a flag when the search is successful, and add other logic to handle it, in all the outer loops, but goto is easily better, in this case.

The use of the goto is also very clear to anyone needing to extend, or debug your code.

This post has been edited by Adak: 23 December 2012 - 02:51 AM

Was This Post Helpful? 3
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Goto statement

Posted 23 December 2012 - 03:34 AM

There has been a lot of discussion on gotos and why they are bad... You could have found them easily by using the </dream.in.code> Search...

Here is a good one, also a recent one...

But if you are one of the fans of SpectateSwamp, then you should encourage and use them... ^^
Was This Post Helpful? 4
  • +
  • -

#5 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 28-September 12

Re: Goto statement

Posted 23 December 2012 - 04:14 AM

Thanks friend Topic closed

This post has been edited by CY5: 23 December 2012 - 04:14 AM

Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Goto statement

Posted 23 December 2012 - 06:36 PM

Quote

i read in many books that we should not use goto in our code,
but i don't get detail explanation that why we should avoid goto ?

The problem is not goto, it's how easy it is to abuse goto. A goto statement is one of those flow control structures that can take simple code and turn it into a convoluted spaghetti mess instantly. This is why everyone declares goto as "evil", and for beginners that is not a bad seed to plant. So yes I agree, goto should not be used by beginners; however, would I declare it evil? eh.. no. But I will say that there are better alternatives.

Those who despise "goto" being in the language must realize that statements such as "continue" or "break" are just that.
Was This Post Helpful? 0
  • +
  • -

#7 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Goto statement

Posted 23 December 2012 - 08:34 PM

Quote

Why? The reason is behind OS memory management concepts. Goto statement brakes the reference of locality which increases page faulting which is not good and so computer performance is decreased.


where did you get that idea? untimely everything is implemented in terms of jumps

if(x < 10) {
  std::cout<<"hello world";
}


will compile to something almost the same, if not the same, as
if(x >= 10) goto l1;
std::cout<<"hello world";
l1:;


this will not change code locality at all, I can assure you. on top of that code locality shouldn't really be affected by the OS, only by the caching behavior of the processor. Any sane OS wouldn't waste time storing code pages to the hard drive so this only comes down to hardware caching behavior. becuase loops and if statements are all just implemented with jumps anyhow in object code, especially once you get to microcode, there wont be a difference in how the hardware handles branch predictions and caching. even if the OS did cache executable pages becuase the object code is the same it wouldn't matter. the idea that locality and OS memory management is the reason for not using goto is just plain wrong.

the reason people avoid goto is becuase it leads to less organized code and clutters the namespace. there are actually still instances, in C and even C++, where goto ends up being a necessity not just an option becuase no other means of control flow adequately handles the situation. I've never actually *had* to do it myself but I've seen code and arguments that are pretty sound. I was once working with a sorting algorithm where I had to continue the outer most loop from an inner loop. I could've used a boolean to tell it to break from the inner loops and continue when it got to the outer loop and did at first. I also broke it into functions. After testing about 3 different ways, and many variations on each way, I concluded that the goto was most efficient. I actually didn't think to do it until I looked at libc++'s(clang's standard library) std::sort.

This post has been edited by ishkabible: 23 December 2012 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: Goto statement

Posted 23 December 2012 - 09:27 PM

View Postjjl, on 23 December 2012 - 08:36 PM, said:

The problem is not goto, it's how easy it is to abuse goto.


No, the problem is actually goto. It's a very bad instruction, period. There may be are certainly cases where it's the lesser evil, but it's never a good thing to have in your code. (this is why modern languages don't have a goto at all)

Quote

Those who despise "goto" being in the language must realize that statements such as "continue" or "break" are just that.


This is approximately as incorrect as saying that objects are the same thing as pointers.
The compiler might generate a jmp statement in its output, but the point of a programming language is to insulate you the programmer from the underlying details. The semantics of a "break" or a "continue" are specific: they can be used in a restricted set of contexts, and they can only do one thing. This means that when you come across them in the source code, you know what they're going to do, and it means that you know what they're not going to do.
A goto can drop you anywhere that your goto can target. This means that if you come across a goto in some code, you have no way of knowing what it's going to do until you check, and what's worse, it means if you see a goto target, you have no way of knowing what will be happening when control is transferred to that point.

Or, to put it another way, a continue statement means "we're done with this iteration of the loop and we're going back to the top to do another". A goto statement means "we're going somewhere else".

Calling these two the same thing is entirely wrong.
Was This Post Helpful? 0
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Goto statement

Posted 23 December 2012 - 10:25 PM

Adak hit the nail on the head if you ask me. There are other cases where goto is needed but they are few and far between.

Quote

It's a very bad instruction, period. There may be are certainly cases where it's the lesser evil, but it's never a good thing to have in your code.


I don't understand this. To me programing is about approximating the least evil solution(aka the best solution) what ever that means in the particular case. What ever the best solution is is a good solution. So if a goto is the least evil thing you can do, isn't it a good thing? Certainly I wouldn't put it there without a good bit of thought but I don't see it as never being a good thing.

People seem to think goto was maid by the devil. Not sure where this comes from other than the fallout from the feud over it in earlier years where structural programing won. It's not the plague, it's just not something that's ideal to use very often.

This post has been edited by ishkabible: 23 December 2012 - 10:38 PM

Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Goto statement

Posted 24 December 2012 - 02:58 AM

Quote

Or, to put it another way, a continue statement means "we're done with this iteration of the loop and we're going back to the top to do another".

Well of course break and continue are not directly "goto" statements, hence the different names; however, they are a specific use of goto.

Quote

A goto statement means "we're going somewhere else".

Yes, as does a break or continue statement. Jumping out of a loop, or to the beginning is also somewhere "else".

There is really only one place I use goto, and that is to break multiple nested loops. Adding extra logic for something so trivial just seems to convolute code.

This post has been edited by jjl: 24 December 2012 - 03:05 AM

Was This Post Helpful? 0
  • +
  • -

#11 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 201
  • Joined: 21-April 09

Re: Goto statement

Posted 24 December 2012 - 04:13 AM

View Postjjl, on 24 December 2012 - 04:58 AM, said:

There is really only one place I use goto, and that is to break multiple nested loops. Adding extra logic for something so trivial just seems to convolute code.


Really? Most cases where you find you need multiple loops you should likely break it out into a function. In which case a return statement will be of more use. I've used a goto statement once. Then my professor demand I rewrite the code. Since I was use the goto code like a local function, I just ended up writing it as a recursive function (he didn't like that much either, but too bad! lol).
Was This Post Helpful? 1
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,628
  • Joined: 16-October 07

Re: Goto statement

Posted 24 December 2012 - 06:13 AM

View Postishkabible, on 24 December 2012 - 12:25 AM, said:

There are other cases where goto is needed


No. To be clear, a goto is never needed ( at least, not in C/C++ ).

It may appear to offer a simplest solution to a particular problem, but there is nothing that can't be done without it. The example of "I'm really deep in loops here and a goto gets me out" is easily solved, and more clear, by simply using functions. Goto, essentially, predates functions. Once you move out of assembly or BASIC or whatever, and into procedural languages, then goto is simply a hold over for folks who can't adjust to a new paradigm.
Was This Post Helpful? 1
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: Goto statement

Posted 24 December 2012 - 07:24 AM

View Postjjl, on 24 December 2012 - 04:58 AM, said:

Well of course break and continue are not directly "goto" statements, hence the different names; however, they are a specific use of goto.


The point of language design, however, is that we don't need to know about the implementation of the language: it'll work according to the spec, and that's what we need to know. So if there happens to be a goto involved in an implementation of a break or a loop or a conditional statement in some compiler of some language, that is of no importance, and it certainly doesn't mean that the break or the loop or the conditional statement is the equivalent of a goto.

The semantics of a goto are over-general, this is precisely why we have high level languages with reasonable formulations like "if" statements (also implemented with an underlying jump) instead of having to construct our loops and conditionals from scratch. So again: to say that a break is the same thing as a goto displays a simple failure to comprehend the question. It's a popular slogan, but it's simply not true.
Was This Post Helpful? 0
  • +
  • -

#14 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Goto statement

Posted 24 December 2012 - 12:01 PM

Quote

No. To be clear, a goto is never needed ( at least, not in C/C++


ok, "need" probably isn't the word. C and C++ are turing complete without goto but I'll maintain that sometimes it is the best solution even in C and C++. I prefer Java's named loops to goto and labels for the nested loops case but there are still more cases where goto is still pretty ideal.

This post has been edited by ishkabible: 24 December 2012 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: Goto statement

Posted 24 December 2012 - 12:04 PM

Sure, "need" is the right word. We say a junkie "needs" his fix, don't we? :)
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3