Hi. I have been given an assignment in my CS class to come up with a situation when it would be a good idea to use a goto statement and write a program for it. It can be any situation (though I have to write the program in 3 days) and so far I haven't thought of anything that couldn't easily be solved either with recursion or iteration. The only thing that I thought of was Towers of Hanoi, but that can be done in only a few lines with recursion. Do you have any suggestions? Thank you for your help.
6 Replies - 1618 Views - Last Post: 22 February 2011 - 06:58 AM
#1
When is a good time to use a GOTO statement in any language?
Posted 21 February 2011 - 02:15 PM
Replies To: When is a good time to use a GOTO statement in any language?
#2
Re: When is a good time to use a GOTO statement in any language?
Posted 21 February 2011 - 02:37 PM
I am not at all a fan of goto but you can certainly use it in extremely rare situations (even though I would argue that any place you put a goto you can certainly write an equally effective setup that doesn't need the goto). Many argue that a goto is great for breaking out of nested loops or statements. My argument to that is that you shouldn't be so deep that you need such a quick escape.
I am actually a bit shocked that they are asking you to come up with a situation where they are good and to code something like that. It is like encouraging a bad practice. But I guess they have their reasons.
Try creating a program with a deep nesting and show a goto that can get out of it when the answer to a solution is known.
I am actually a bit shocked that they are asking you to come up with a situation where they are good and to code something like that. It is like encouraging a bad practice. But I guess they have their reasons.
Try creating a program with a deep nesting and show a goto that can get out of it when the answer to a solution is known.
#3
Re: When is a good time to use a GOTO statement in any language?
Posted 21 February 2011 - 03:06 PM
Martyr2, on 21 February 2011 - 04:37 PM, said:
I am actually a bit shocked that they are asking you to come up with a situation where they are good and to code something like that. It is like encouraging a bad practice. But I guess they have their reasons.
Thanks for the suggestion. The professor is giving this assignment because the class is about the theory of designing and developing programming languages. We're supposed to be looking at all aspects.
#4
Re: When is a good time to use a GOTO statement in any language?
Posted 21 February 2011 - 04:03 PM
The answer you should give your teacher is there isn't one.
#5
Re: When is a good time to use a GOTO statement in any language?
Posted 21 February 2011 - 06:09 PM
lunixer, on 21 February 2011 - 03:06 PM, said:
The professor is giving this assignment because the class is about the theory of designing and developing programming languages. We're supposed to be looking at all aspects.
So, it looks like you are discussing programming languages in general, not just one programming language in particular, correct?
Keep in mind that rules against gotos generally only apply to high-level code. In fact, if you were to take a piece of high level code like a conditional while loop and look at the disassembled code that the compiler generates for it, more often than not you will have goto statements in it.
Also, if you were to take assembly language and try and write the same conditional while loop that you would in a higher level language, then you are most likely going to have to use goto/jump statement(s).
Does this assignment have to be done in a particular language, or can this assignment be done in any language that you choose (like one where goto statements would be a virtue)?
In legacy Visual Basic, you will often see statements similar to this:
On Error Goto ErrHandler: 'Your code which may produce an error ErrHandler: 'Here is the code to handle any errors
GoTo statements were often used to handle errors in this fashion because there are no Try Catch blocks available in lagacey Visual Basic...
This post has been edited by keakTheGEEK: 21 February 2011 - 06:13 PM
#6
Re: When is a good time to use a GOTO statement in any language?
Posted 21 February 2011 - 06:18 PM
keakTheGEEK, on 21 February 2011 - 08:09 PM, said:
lunixer, on 21 February 2011 - 03:06 PM, said:
The professor is giving this assignment because the class is about the theory of designing and developing programming languages. We're supposed to be looking at all aspects.
So, it looks like you are discussing programming languages in general, not just one programming language in particular, correct?
Keep in mind that rules against gotos generally only apply to high-level code. In fact, if you were to take a piece of high level code like a conditional while loop and look at the disassembled code that the compiler generates for it, more often than not you will have goto statements in it.
Also, if you were to take assembly language and try and write the same conditional while loop that you would in a higher level language, then you are most likely going to have to use goto/jump statement(s).
Does this assignment have to be done in a particular language, or can this assignment be done in any language that you choose (like one where goto statements would be a virtue)?
In legacy Visual Basic, you will often see statements similar to this:
On Error Goto ErrHandler: 'Your code which may produce an error ErrHandler: 'Here is the code to handle any errors
GoTo statements were often used to handle errors in this fashion because there are no Try Catch blocks available in lagacey Visual Basic...
Any modern high level language. No assembly.
#7
Re: When is a good time to use a GOTO statement in any language?
Posted 22 February 2011 - 06:58 AM
It is language dependent; some languages have few tools. Of course, in some languages you don't get a goto.
The infamous GOTO is JMP in ASM and pretty much required. Of course, ancient BASIC required GOTO to get anything done. And probably popularized the name ( as opposed to "jump" )
I modernly use GOTO in T-SQL when I write stored procedures in Microsoft SQL Server. Conditional exits and those resulting from exception are common in database flow. The tools often aren't there to do things more elegantly.
In C, you still see it a lot in low level network stuff. Some very popular books used goto everywhere. The reason made sense; no matter what fail condition you run into, you still want to clean up your connections. I just did a code hunt and found much of this code as been cleaned up in later editions. (Found one. ) Old Windows macros like WSAStartup and WSACleanup used to use goto.
When is a good time to use GOTO? When you have no other choice. In most modern languages, you do.
The infamous GOTO is JMP in ASM and pretty much required. Of course, ancient BASIC required GOTO to get anything done. And probably popularized the name ( as opposed to "jump" )
I modernly use GOTO in T-SQL when I write stored procedures in Microsoft SQL Server. Conditional exits and those resulting from exception are common in database flow. The tools often aren't there to do things more elegantly.
In C, you still see it a lot in low level network stuff. Some very popular books used goto everywhere. The reason made sense; no matter what fail condition you run into, you still want to clean up your connections. I just did a code hunt and found much of this code as been cleaned up in later editions. (Found one. ) Old Windows macros like WSAStartup and WSACleanup used to use goto.
When is a good time to use GOTO? When you have no other choice. In most modern languages, you do.
Page 1 of 1

New Topic/Question
Reply


MultiQuote






|