Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Game Loop - Need some help looping..

loop

  • Please log in to reply
33 replies to this topic

#25 Lance

Lance

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 270 posts

Posted 15 December 2008 - 06:01 AM

NEVER use goto! If you can give me a concrete of where goto is the best solution, I'd love to see it, and then I'd tell you "NEVER use goto!" Goto is a holdover from the bad old days of BASIC, and only has any use in a language where structured programming is not possible. There is never a situation where goto is necessary (mathematically proven). Don't do it, don't encourage it, don't make it sound acceptable.


I don't have it right now, but you can do a grep on linux source tree, I believe you can can dozens if not hundreds of goto. Last time I read linux kernel coding standard, it has a very reasonable rule on the use and non-use of goto. I'll try to find a link to that text.

EDIT: I searched and could not find any mention of goto in both Linux Kernel coding standard and GNU coding standard. Look like something has changed since I read it around 5 years ago, or probably I read it somewhere else. Anyway, it says something like:
1. do not have more than 1 goto location in a function;
2. do not go backward.

Edited by Lance, 15 December 2008 - 06:21 AM.

  • 0

#26 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 15 December 2008 - 08:32 AM

There are times when a goto is actually appropriate (such as kernal code). When learning programming, the best thing to do is avoid it like the plague, though.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#27 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts
  • Location:Nevada
  • Learning:Java, Objective-C, PHP, JavaScript, Perl, PL/SQL, ActionScript, VBScript, Others

Posted 15 December 2008 - 08:50 AM

I understand giving an opinion, but some of the newer people who are joining this forum are telling our fellow members not to do something just because some guys says not to in a book. I don't know how they think they have the right to start telling a member that they are wrong or they should do it this way and only this way because its in a book. Well there is a book on how to eat pasta but it doesn't always mean its the "right" way. The only reason I am making this post is because it is amazing that when I open a thread and all the wonderful people who I consider "CodeCall" members are here to help me by explaining things or providing correct code, but some of the newer people who join here just act like they know it all or go explaining themselves based on what one person says in a book, sit here "With no reputation" telling the fellow member or members that they are wrong. Get off your high horses and contribute with a reason why it is or isn't bad if you think it is that way. So if you are new to codecall do us a favor and do not post in a thread unless you have a kind way of telling a member your opinion or just don't post at all.

I am not saying that all new members act like this. I am only referring to the people who just joined here to either show off or to start down talking the fellow members. I know these issues are in the rules but I obviously people are not reading them.

Thanks to the following helping me out with this code.

- Lance
- WingedPanther
- The rest of the people who posted here without creating a dispute.
  • 0

#28 Lance

Lance

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 270 posts

Posted 15 December 2008 - 11:37 AM

Now that I have some half hour's break, let me continue on "goto". Addressing the same reply by WingedPanther

Originally Posted by WingedPanther View Post
NEVER use goto! If you can give me a concrete of where goto is the best solution, I'd love to see it, and then I'd tell you "NEVER use goto!" Goto is a holdover from the bad old days of BASIC, and only has any use in a language where structured programming is not possible. There is never a situation where goto is necessary (mathematically proven). Don't do it, don't encourage it, don't make it sound acceptable.


C didnot inherit goto from Basic. As much as many people dislike goto, many modern languages continue to choose to include it, sometimes in a diguised form.

Java: It's no longer called goto, but it's goto nonetheless.
outmost:
for(int i=0; i<10; ++i){
     for(int j=0; i<100; ++j){
           if ( certain_condition_is_met_with(i,j) )
                      break outmost;
      }
}

Now call me a fool if "break outmost;" does anything different than "goto outmost;", or it makes the program more structural.

C#: goto remains a part of the language.


Talking about somebody saying something is bad, there are more than one big potatoes saying that Pointers are evil, and Java and C# indeed have eleminated it (while they choose to keep goto or goto_renamed). Now tell me who is a C++ programmer and never using a pointer?


Yes, "goto" can be replaced by some one way, but not always without a price, however minimal. For example, it often involves using a flag and testing the flag. Eg:

bool outmost, second_outmost;

for(int i=0; i<10000; ++i){
    outmost=false;
    for(int j=0; j<100000 && !outmost; ++j){
          second_outmost=false;
          for(int k=0; k<10000 && !outmost && !second_outmost; ++k){
                 if (should_goto_outmost(i,j,k) )
                     outmost=true;
                 else if(should_goto_second_out_most(i,j,k) )
                     second_outmost=true;
                 else
                     do_regular_action_on(i,j,k);
             }
      }
}

call me a fool if you believe the code is more understandable and efficient than

outmost:
for(int i=0; i<10000; ++i){
    second_outmost:
    for(int j=0; j<100000 && !outmost; ++j){
          for(int k=0; k<10000 && !outmost && !second_outmost; ++k){
                 if (should_goto_outmost(i,j,k) )
                     goto outmost;
                 else if(should_goto_second_out_most(i,j,k) )
                     goto second_outmost;
                 else
                     do_regular_action_on(i,j,k);
             }
      }
}




All a "while" can be changed to a "for" without loosing any efficiency in the compiled binary, and vise versa, but both are in the language and have its place. So that doesn't justify that "goto" should be eliminated either.

That being said, I do agree it's a common sense that you should think thrice before using "goto". When I wrote that sample code, I orignally wrote "return 0;" in the place of "goto blabal;" I purposedly changed it to goto because I want to make a point that a break here won't work as it only break out of the switch block instead of the "do{}while;" loop.
I should have put a comment there.

And also, even though I never actually follow it, I do remember there are big potatoes preaching a funtion should have only one exit point to make it easier to debug. For those people, there would actually prefer a "goto" than mutiple "return". I mention this to demonstrate that there are many people say many thing and not for no reasons, while you cannot listen to/abide by all of those in all situations.
  • 0

#29 Lance

Lance

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 270 posts

Posted 15 December 2008 - 11:48 AM

Thanks, Donovan. Constructive discussion should always be encouraged, while religious fever should be ignored.
  • 0

#30 Lance

Lance

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 270 posts

Posted 15 December 2008 - 11:48 AM

The only places that goto[s] are considered acceptable (generally) are in deeply embedded expressions where they allow easy access to an exit routine.


Good point.
  • 0

#31 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts
  • Location:Nevada
  • Learning:Java, Objective-C, PHP, JavaScript, Perl, PL/SQL, ActionScript, VBScript, Others

Posted 15 December 2008 - 12:32 PM

Thanks, Donovan. Constructive discussion should always be encouraged, while religious fever should be ignored.


No problem, I just do not like to see our new members talked to in a way like they are stupid or what they say does not count. I admit I do not know who this Bjarne guy is because I am extremely new to this programming stuff and coding in C++. My knowledge is strictly basic C++ stuff. Although I am fluent in the basic stuff I do not claim to know it all. By the way thanks again for your helpful code.
  • 0

#32 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 15 December 2008 - 12:58 PM

I'll agree that there are times when a goto is the best solution. However, I also think that the likelihood of it being the best solution is very low in 99% of programs. Perhaps I came across a little to stridently.

One of the things about Java that annoys me is they insist on calling their pointers "references". From a behavioral standpoint, a Java reference is equivalent to a C++ pointer with a couple restrictions.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#33 Lance

Lance

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 270 posts

Posted 15 December 2008 - 03:03 PM

Agreed with WingedPanther.

To Donovan:
Dr. Bjarne Stroustroup is the designer and orignal implementor of C++. His "the Design and Evolution of C++" is a must read if you decide go to with C++ instead of C, which I suggest you do. I myself only read selected chapters on the great book though. Not sure if it's the best text book for beginner. Maybe you can move on to it when you are more at ease with the language.
  • 0

#34 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts
  • Location:Nevada
  • Learning:Java, Objective-C, PHP, JavaScript, Perl, PL/SQL, ActionScript, VBScript, Others

Posted 15 December 2008 - 09:54 PM

Thanks, right now I have almost finish the Beginning C++ Through Game Programming, Second Edition. I also provided all 10 chapter project files in my C++ guide along with the program itself with the book. Which is why some people are jumping out of there seats cause usually that is $29.99 haha:D
  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download