|  | |
12-01-2005, 07:23 PM
|
#21 (permalink)
|
Monster Techie Join Date: Jan 2005 Location: Canada Posts: 1,522
| Quote: Originally posted by mgoldb2 The goto I guess would work too but if any of my teachers ever saw a goto statment in one of my program that would be a automatic many point off. | That is the teacher's problem..  .. I swear, some people (teacher) just don't know "why" some things are the way they are. If I were to see the the code TheHeadFL posted (1), and the one you posted (2), I would much prefer (1). After all, the only reason people recommend "not" to use the goto statement is due to clarity. Making something "less clear" just for the sake of NOT using goto is not right.
Using goto does not affect your compiled code negatively. After all, the underlying assembly code will be a buncha branching to memory addresses and what with various calculations and moves.
You bring up a good point about having the set of "for" loops encapsulated in a function - without anything else.
But, you can't really do this in the general case. For example, the for loop segment might be heavily dependent on a LOT of variables - passing all of them (pointers if they need modificaton) would be ugly. Also, considering we are concerned with "clarity", separating out a for loop segment into a separate function might not have any conceptual meaning. I am sure you guys can list many other reasons.
Not using "goto" blindly, is not something I recommend. You always need to put things into context. Making your code "more ugly" just to stay away from "goto" is not something teachers should be teaching students. |
| |
12-02-2005, 02:35 AM
|
#22 (permalink)
|
Ultra Techie Join Date: Jul 2005 Posts: 530
| Frequently it helps to know the highest value that the counter values reached, so doing what he posted above doesn't fix that problem.
The only good way, is like I said, to use cascading breaks. You set some 'flag' in the loop, and each loop examines the flag and breaks.
But, as I said, why do something like that, which is unclear, when a clear solution exists?
I agree with the above post... program for the sake of clarity, not because you had a teacher who was anal retentive about goto statements. People still write in assembly (I do) and 'goto' is the name of the game, so its not unreadable. And as I already mentioned, all underlying code is being translated to conditional jumps or unconditional jumps. Readability and clarity is the only reasons for the 'convention' on the subject.
__________________ Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache) |
| |
12-21-2005, 07:56 AM
|
#23 (permalink)
|
Junior Techie Join Date: Jun 2005 Posts: 94
| Here's where I us gotos frequently:
int main (void){
//lots of initialization
//code
//one reason to exit
return_val = 1;
goto exit;
//more code
//another reason to exit
return_val = 1;
goto exit;
//yet more code
//normal exit
return_val = 0;
exit:
//cleanup
return return_val;
}
This can be done with a cleanup (int return_val) function or exceptions (which is conceptually a goto) but both might not have a lot of information needed for the cleanup. Putting the cleanup in a local catch block and rethrowing the exception means repetitive code, which is asking for trouble. |
| |
12-25-2005, 01:06 PM
|
#24 (permalink)
|
Super Techie Join Date: May 2005 Posts: 479
| there always is a more elegant way to do what you want to do while avoiding gotos.. always..
__________________ lisp hacker 
running: FreeBSD 5.4 - still learning 
develop with: SBCL + emacs for lisp, Anjuta IDE +gcc for c, SPE for python..
browse with: opera |
| |
12-27-2005, 03:26 AM
|
#25 (permalink)
|
Ultra Techie Join Date: Jul 2005 Posts: 530
| Quote: Originally posted by ever_thus Here's where I us gotos frequently:
int main (void){
//lots of initialization
//code
//one reason to exit
return_val = 1;
goto exit;
//more code
//another reason to exit
return_val = 1;
goto exit;
//yet more code
//normal exit
return_val = 0;
exit:
//cleanup
return return_val;
}
This can be done with a cleanup (int return_val) function or exceptions (which is conceptually a goto) but both might not have a lot of information needed for the cleanup. Putting the cleanup in a local catch block and rethrowing the exception means repetitive code, which is asking for trouble. | The example you mentioned previously can be handled better by using return statements instead of gotos.
For the situation in which cleanup code is neccesary when exceptions are thrown, in Java or C# you may do Code: try
{
// Code that may generate errors
if (error) throw Exception();
// Rest of code for normal execution
}
catch (Exception e)
{
// Print exception info or do whatever with it
}
finally
{
// Cleanup code here
}
Finally blocks work great for those situations.
__________________ Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache) |
| |  | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |