You Learn Something (Scary) Every Day
Well it turns out you can actually use 'goto' statements in C# !! private void button1_Click(object sender, System.EventArgs e) { goto done; MessageBox.Show("Bet this doesn't get shown!"); done: return; } Conversation overheard in the next cubicle: I didn't realise that VB.net still has 'goto' statements. Scary. Want to know something even scarier? (pause for a beat.) C# has a goto statement too. Really!? Yeh, but no one ever uses it. Why not? Cause you'd be shot. That's why.
'CGamesPlay' on Wed, 05 Jul 2006 10:37:36 GMT, sez: Well, I'm having trouble deducing the attitude of the author towards gotos, but that is a neat trick.
'Groucho' on Wed, 05 Jul 2006 14:35:52 GMT, sez: There is a time when you might need to use the goto statement because of the handling of switch statements. The compiler won't let you fall-through from one case to the other like from case 2 to case 3 below.
switch(i) {
case 1:
case 2:
/* some code */
case 3:
/* some code */
break;
default:
break;
}
It will allow case 1 to case 2 as far as I know because case 1 has no code (like an OR).
But the compiler will force you to have a break at the end of case 2, unless you put a goto, return, or throw.
So if you actually want it to fall through, you would need:
switch(i) {
case 1:
case 2:
/* some code */
goto case 3;
case 3:
/* some code */
break;
default:
break;
}
'lb' on Wed, 05 Jul 2006 21:02:02 GMT, sez: yeh i've seen this reason given.
is this a bit of a big hammer for a small nail?
'marty' on Wed, 05 Jul 2006 21:56:38 GMT, sez: I thought the whole "GOTO is bad!!!!!" thing died years ago, but it always comes back :)
'Groucho' on Wed, 05 Jul 2006 22:22:12 GMT, sez: @lb
I certainly think so. Apart from their use in switch statements (I have issues with how switch statements work in .NET so that's arguable at best), I don't see a reason for them. That hazards simply outweigh the benefits, and you can always accomplish the same thing with the standard control flow statements. if,while,for, etc. are there so we don't need goto's after all.
Interestingly, I see them pop up in disassemblies of .NET exe/dll's more often than I would have expected. They replace continue statements in loop blocks for instance:
while(true) {
...
continue;
}
becomes
Label_0332:
while(true) {
...
goto Label_0332;
}
'Edsger W. Dijkstra' on Wed, 05 Jul 2006 22:25:26 GMT, sez: For a number of years I have been familiar with the observation that the quality of programmers is a decreasing function of the density of go to statements in the programs they produce.
It seems that this C# compiler is a programme of the lowest quality.
'lb' on Thu, 06 Jul 2006 00:15:13 GMT, sez: so under the cover (in IL i mean) are all of these 'GOTO'/Jump statements:
break
continue
goto
return
throw
??
'Groucho' on Thu, 06 Jul 2006 14:19:36 GMT, sez: I know very little about the process so I couldn't say. The code ends up being re-arranged quite a bit because of the optimization the compiler goes through, so it would all depend on the code. But, I tested it out quickly. I only saw the continue statement being replaced with a goto, and only in a while loop, not for loops (having it jump to just before the loop like in the while loop wouldn't make any sense). Instead, the code gets re-arranged to remove the continue statement. No continue's in IL apparently.
'http://' on Mon, 10 Jul 2006 15:49:27 GMT, sez: There are reasons for using a GOTO, even in C#. However, only *very* experienced programmers are going to know when it is appropriate and when it is just a crutch. Because of that, I'm kind of glad people haven't noticed that it exists.
With C# there are enough control structures there is never a case that a GOTO can't be worked another way. Perhaps the avoidance of GOTO adds perhaps a bit of overhead: but at this point the minor amount of overhead is rarely a problem in the real world.
If you want to see excellent examples of GOTO used properly, find a copy of the old TeX code by Donald Knuth. Considering he wrote the book on programming (literally: see the "Art of Computer Programming") it shows it can be used instead of abused. Sadly, he is also one of the best programmers ever to exist: most programmers simply abuse it, thus the prohibition.
'Zooba' on Wed, 12 Jul 2006 03:02:35 GMT, sez: Most likely (I'm not extremely well-versed here) IL is based on similar constructs to assembly language, which (as is always taught at universities) 'only' has GOTO. It also has RETURN, so to look at the HLL statements and their equivalent low-level ones:
BREAK - go to next closing brace
CONTINUE - go to last opening brace
GOTO - go to a place which has a name
RETURN - go to address on top of stack
THROW - set a global variable then RETURN
Because of the architecture of the x86, GOTO equivalents (jumps) are unavoidable, and everything has to resolve to a GOTO at some point, otherwise the code runs straight from top to bottom. There are certainly times when using a GOTO is clearer than the equivalent 'structured' construct, though any more than a whisper about it is enough to bring the HLL-purists down on you.
'lb' on Wed, 12 Jul 2006 04:31:23 GMT, sez: cheers Zooba -- good points.
i use goto statements in batch files a lot. i think they can be very neat so long as you're very very consistent with them.
actually the real challenge is having consistent and meaninful label names.
from the little i remember about assembly language isn't there JMP (that can jump anywhere) and JMP IMMED (branches maybe??) that can't jump as far? oh i'll shut up now, i remember nothing.
cheers
lb
'Zooba' on Wed, 12 Jul 2006 23:13:48 GMT, sez: That's true. However, generally the assembler will decide which one to use. Modern assemblers have high level constructs as well (see http://www.masm32.com/).
Generally conditional jumps (based on the result of a previous comparison) can't go as far as an unconditional one, but that's just the instruction set.
'lb' on Thu, 13 Jul 2006 04:04:59 GMT, sez: hey zooba -- email me if you want (leon bambrick at gmail dot com, all one word)-- i wanna talk about your goto method (http://www.byteclub.net/blog/zooba/?p=23)
'just a visitor' on Wed, 02 Aug 2006 00:03:46 GMT, sez: Actually, goto can be useful if you translate from a language that has them (example: fortran) and if this language has a huge legacy library (example: netlib for many numerical computations) and you have a way to tranform the programs automatically(example: using f2c).
[Comments closed due to spam]
|