GOTO Alternative

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

74 Replies - 3171 Views - Last Post: 26 May 2012 - 03:26 AM Rate Topic: -----

#31 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,782
  • Joined: 20-September 08

Re: GOTO Alternative

Posted 24 May 2012 - 11:38 AM

View PostMorrowind_Fan, on 24 May 2012 - 02:36 PM, said:

Alright, from what I understand, I should re-write this using functions?

Yes ... for now .. focus on what Jim has so well said above.

But at some future point ... if you want to understand how functions and other structures work ... and what is a 'stack' ... and what is 'stack unwinding' ... :)
Was This Post Helpful? 0
  • +
  • -

#32 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,694
  • Joined: 19-March 11

Re: GOTO Alternative

Posted 24 May 2012 - 11:41 AM

View PostMorrowind_Fan, on 24 May 2012 - 01:36 PM, said:

Alright, from what I understand, I should re-write this using functions?


It'd be a good way to learn how they work - go for it!
By the way - you've done a gang of work writing this in C++ coming from BASIC. I'm guessing you figured this all out on your own? Good stuff, keep it up!
Was This Post Helpful? 0
  • +
  • -

#33 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 225
  • Joined: 03-March 11

Re: GOTO Alternative

Posted 24 May 2012 - 11:42 AM

Nobody seems to have pointed out the biggest difference between functions and goto's is that functions return to the spot that they were called from, where as goto's alter the entire structure, it just seems illogical. There may be a few instances where a function does not return to the previous place, but if it returns a value it will always go back to called point, thus not upsetting the natural flow of the structure of a program. My programming lecturer who is a complete nutter but start programming about 30 years ago and is still in the trade, he lectured us about goto's and how their presence should be minimal if not non-existent in modern structured programming, and I quote, he said:

Quote

Tony Dyer:

"A programmer may find one instance that they feel they need to use a goto, every 10 years of developing programs at an experienced level of programming where you're developing hundreds of programs, big programs every year. Even then, it is not 'necessary', only easier than the right way."


Seriously kid, do yourself a favor and just learn functions :P Just leave us to rant.

This post has been edited by mccabec123: 24 May 2012 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#34 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: GOTO Alternative

Posted 24 May 2012 - 11:47 AM

And sometimes that's exactly what you want to do.
Since Dijkstra's say on the subject has already been posted I'll go ahead and post Donald Knuth's views as well.
Structured Programming with goto statements
Was This Post Helpful? 0
  • +
  • -

#35 ishkabible  Icon User is offline

  • spelling expret
  • member icon




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

Re: GOTO Alternative

Posted 24 May 2012 - 12:06 PM

being so admittedly against anything, even goto, is bad. goto has a place and in certain instances actually solves problems better than other possibilities. I think a lot of the hatred toward goto is blind; people take what their teachers say at face value without waiting to have enough experience to evaluate it for themselves. with recursion and if statments alone you have Turing completeness, so I *could* do everything with just recursion; with while loops alone we have Turing completeness so I *could* do everything with while loops. I don't do this however; I use the control flow that I feel best suits the situation. no one form of control flow is ever wholly wrong or right; goto just happens to be bad a lot but that doesn't mean you should permanently forsake it.

but be careful with goto...
Posted Image

This post has been edited by ishkabible: 24 May 2012 - 12:09 PM

Was This Post Helpful? 1
  • +
  • -

#36 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2890
  • View blog
  • Posts: 10,019
  • Joined: 08-August 08

Re: GOTO Alternative

Posted 24 May 2012 - 12:18 PM

I'd agree that goto shouldn't be completely forsaken, but with the caveat that it shouldn't be taught at all until well after the beginner has learned about for, while, and do ... while loops, as well as functions, arrays, vectors, classes, and structs. And I'm probably missing some items in my list! I don't believe you can know enough to determine that it's ok to use a goto statement if you haven't got a good grasp of all of these.
Was This Post Helpful? 0
  • +
  • -

#37 Morrowind_Fan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: GOTO Alternative

Posted 24 May 2012 - 12:33 PM

View Postjon.kiparsky, on 24 May 2012 - 11:41 AM, said:

View PostMorrowind_Fan, on 24 May 2012 - 01:36 PM, said:

Alright, from what I understand, I should re-write this using functions?


It'd be a good way to learn how they work - go for it!
By the way - you've done a gang of work writing this in C++ coming from BASIC. I'm guessing you figured this all out on your own? Good stuff, keep it up!

Thank you for the compliment, and yes I did figure all this out on my own.
Was This Post Helpful? 0
  • +
  • -

#38 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 225
  • Joined: 03-March 11

Re: GOTO Alternative

Posted 24 May 2012 - 01:01 PM

View PostCTphpnwb, on 24 May 2012 - 12:18 PM, said:

I'd agree that goto shouldn't be completely forsaken, but with the caveat that it shouldn't be taught at all until well after the beginner has learned about for, while, and do ... while loops, as well as functions, arrays, vectors, classes, and structs. And I'm probably missing some items in my list! I don't believe you can know enough to determine that it's ok to use a goto statement if you haven't got a good grasp of all of these.


I'd also agree that it has it's place, but that place is not in 'structured' programming. We aim as programmers to create code that is as reliable as possible.

Goto is like:

A person wants to make his way home from the town, as he/she gets half way there, he/she is then picked up by some godlike force and thrown back to where he/she started off, or in some cases closer to his/her destination.

Functions on the other hand are like:

A person wants to make their way home from the town, so they make their way home and half way there they encounter a sign that says they can go home this way and thus are guaranteed to get home to their destination (unless eaten by wild bear).

In summary, with goto it can either be good or bad, there's no real telling and it's impossible to work out where you came from or where to go from your new destination. Where as functions are consistent and follow the path that a good 'structured' program should.

And I reiterate (hehe iterate, oh the irony) the key point in my bringing this up, is that structure is key, over almost anything. Just because there are a few cases where something can be easier, certainly does not mean that it is right. There's the easy way or the right way.
Was This Post Helpful? 0
  • +
  • -

#39 ishkabible  Icon User is offline

  • spelling expret
  • member icon




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

Re: GOTO Alternative

Posted 24 May 2012 - 01:23 PM

CTphpnwb: I agree 100%. in fact I think it should never be explicitly taught but rather after years of experience and a degree of expertise in solving a certain issue one could see for themselves the few instances where it's better than the alternative.

Quote

I'd also agree that it has it's place, but that place is not in 'structured' programming


I'll apply the multi-paradigm argument here. C and C++ are not just 1 paradigm, neither are they excursively structured languages. where it suits the problem C and C++ can become unstructured languages. it's like saying C is procedural(mostly, it is) or C++ is an OOP language(that one really irks me cuz that's such a small part of the language); they are both much more than that.

Quote

There's the easy way or the right way.

why are the two mutually exclusive? I would make a case that they are actually the same thing. the easiest thing *is* the right thing. what ever causes the fewest headaches is the right thing in programing. sometimes slowness is the headache, most of the time maintainability is the headache, frequently it's both. there are instances where goto is the right tool in all of those cases even in a "structured" language like C or C++.

can you define "right way"? what makes that way right?

why is your argument representing functions as the alternative to goto? loops are far more frequently the alternative at hand but yes, functions are a major part of well structured code.

I'm not advocating the usage of goto/labels but just walling your self off is a bad idea. you might be really surprised about somethings when you test and think about them.

This post has been edited by ishkabible: 24 May 2012 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#40 Morrowind_Fan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: GOTO Alternative

Posted 24 May 2012 - 01:26 PM

I'm having trouble figuring this out without too many functions; any tips?
Was This Post Helpful? 0
  • +
  • -

#41 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,782
  • Joined: 20-September 08

Re: GOTO Alternative

Posted 24 May 2012 - 01:29 PM

View PostMorrowind_Fan, on 24 May 2012 - 04:26 PM, said:

I'm having trouble figuring this out without too many functions; any tips?

Ok ... let's start with a clear step by step description of just what it is you want to do ...

Take this simple example:
Write a program to maintain a contact list that allows input, edit, sort, file and read from file ... a contact list
LastName, FirstName
3 line home address
Primary phone number
Primary e-mail
dob

This post has been edited by David W: 24 May 2012 - 01:43 PM

Was This Post Helpful? 1
  • +
  • -

#42 Morrowind_Fan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: GOTO Alternative

Posted 24 May 2012 - 01:37 PM

Okay, well, for this part of the game I have two paths: noticed and unnoticed. Now, for each path I have multiple choices on how to go. I can call the death sequence fine; however, going from choice to choice is where I'm lost. ..
Was This Post Helpful? 0
  • +
  • -

#43 Morrowind_Fan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: GOTO Alternative

Posted 24 May 2012 - 01:47 PM

Wait, hold on. I can just place text under each if statement right?
Was This Post Helpful? 0
  • +
  • -

#44 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,782
  • Joined: 20-September 08

Re: GOTO Alternative

Posted 24 May 2012 - 01:47 PM

View PostMorrowind_Fan, on 24 May 2012 - 04:37 PM, said:

Okay, well, for this part of the game I have two paths: noticed and unnoticed. Now, for each path I have multiple choices on how to go. I can call the death sequence fine; however, going from choice to choice is where I'm lost. ..


void A();
void B();   // use your descriptive names and define 

//...
if( condition ) A();
else B();
 // ...

//...

void B()
{
   if( condition1 ) // ... 
   else if( condition2 ) // ...
   // ...
   else // ...
}
void A()
{
   if( condition1 ) // ... 
   else if( condition2 ) // ...
   // ...
   else // ...
}

// ...




This post has been edited by David W: 24 May 2012 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#45 Morrowind_Fan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: GOTO Alternative

Posted 24 May 2012 - 01:59 PM

I just don't want ifs within ifs within ifs
Was This Post Helpful? 0
  • +
  • -

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