Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 11-23-2005, 02:16 PM   #1 (permalink)
baronvongogo's Avatar
 
Master Techie

Join Date: May 2005

Location: UK

Posts: 2,749

baronvongogo is on a distinguished road

Default to goto or not to goto

hi Im learning c++ at the moment, And it seems no one likes goto commands. Now if im only using 4 gotos in a function is it really that bad?

Basically im using it for a dos game if a user inputs say 20 when they can only spend 15 it uses goto to jump back to the original starting point.

Are goto`s that bad?
__________________
baronvongogo is offline  
Old 11-23-2005, 04:02 PM   #2 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

Ok you can only use a goto 1 time every 10 to 15 years.
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
Old 11-23-2005, 04:49 PM   #3 (permalink)
baronvongogo's Avatar
 
Master Techie

Join Date: May 2005

Location: UK

Posts: 2,749

baronvongogo is on a distinguished road

Default

lol ok i`ll try to stick to those confines
__________________
baronvongogo is offline  
Old 11-23-2005, 07:39 PM   #4 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

Heres the thing, its really only a matter of style.

When you compile your program, all your nice readable and elegant do/while, while, for loops, case statements, if/else statements... they're all converted to endless strings of jumps (goto) and conditional jumps (if then).

So, no, goto is not a particular awful and nasty thing to use, however, it is generally considered poor style. You don't want to get mired down in managing a bunch of gotos and goto target addresses... (the labels that specify goto targets). But, if it really makes what you are trying to do MUCH EASIER, and you comment it properly, then sure, use a goto.
__________________
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)
TheHeadFL is offline  
Old 11-23-2005, 07:41 PM   #5 (permalink)
baronvongogo's Avatar
 
Master Techie

Join Date: May 2005

Location: UK

Posts: 2,749

baronvongogo is on a distinguished road

Default

thanks and it is just for ease of use if i had more time Id find another way
__________________
baronvongogo is offline  
Old 11-28-2005, 03:15 AM   #6 (permalink)
 
Super Techie

Join Date: Jun 2005

Posts: 274

mgoldb2

Default

A long time ago I was given a huge program that used many goto statements to debug. After that experience I know exactly why they say NEVER to use go to statements in your code. Debugging a program where the user freely used goto where ever they wanted make a program almost impossiable to debug.

On a secound note I been using C++ for 6 years now and I did not even know it supported goto statements.
mgoldb2 is offline  
Old 11-28-2005, 07:19 AM   #7 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

I feel I need to clarify my statement.

For example...

Code:
for (something)
   {
   for (something)
   {
      for (even something else yet)
      {
         if (condition) {
         // Get out of all the loops
         }
      }
   }
}
This is extremely convoluted and painful to do with a break statement. In order to do it this way, you need to cascade several levels of breaks. In other words, outside every loop, you check to see if the loop prematurely exited, and if it did, you break out.

You can be married to the "no gotos" style, or you can just save yourself the hassle, confusion, and obtuse style of the cascading break method and just deign to use a goto.
__________________
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)
TheHeadFL is offline  
Old 11-28-2005, 07:59 AM   #8 (permalink)
baronvongogo's Avatar
 
Master Techie

Join Date: May 2005

Location: UK

Posts: 2,749

baronvongogo is on a distinguished road

Default

Yeah I think theres a huge difference in using goto`s to jump huge parts of code so its difficult to follow, But mine is all in a tiny function and in visiable range of each other and thier labels, So its easy to debug and follow. And i didnt no goto was in C++ until I typed it and the word highlighted so thought might as well try it out .
__________________
baronvongogo is offline  
Old 11-29-2005, 01:28 AM   #9 (permalink)
 
Super Techie

Join Date: Jun 2005

Posts: 274

mgoldb2

Default

Quote:
Originally posted by baronvongogo
Yeah I think theres a huge difference in using goto`s to jump huge parts of code so its difficult to follow, But mine is all in a tiny function and in visiable range of each other and thier labels, So its easy to debug and follow. And i didnt no goto was in C++ until I typed it and the word highlighted so thought might as well try it out .
I just curious why you would even want to use a goto statement instead of either a while,do while or for. I dont see any benfit from using the goto.
mgoldb2 is offline  
Old 11-29-2005, 11:47 AM   #10 (permalink)
baronvongogo's Avatar
 
Master Techie

Join Date: May 2005

Location: UK

Posts: 2,749

baronvongogo is on a distinguished road

Default

well, say I activate a lot of things such as:

void something(){

workstart:
work1
work2
work3
work4

if (work1 <=0)
goto workstart;
}


Just simple to use inside a function to go back to the beginning isnt it? Not like its jumping all over the code its within the eyeline of the label and simple to follow.
__________________
baronvongogo is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On