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

#13 TALucas

TALucas

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 118 posts
  • Programming Language:Java, (Visual) Basic, Visual Basic .NET, Transact-SQL
  • Learning:C++, PHP, ActionScript

Posted 13 December 2008 - 07:13 PM

GOTO's are fairly common place in the COBOL code where I work. I agree with you that GOTO's are very dangerous, and can lead to code that is difficult to follow, and almost impossible to test, but I also think if used properly, they can make code more readable. Check out the example COBOL below:

2000-PROCESS-ORDER-HEADER SECTION.
       STRT-PROCESS-ORDER-HEADER.
            PERFORM 4100-READ-SAINHDR03.
            IF WS-SAINHDR03-IS-AT-END
               GO TO EXIT-PROCESS-ORDER-HEADER
            END-IF.
            IF SIH3-INVOICE-DATE > WS-ENDING-DATE
               GO TO EXIT-PROCESS-ORDER-HEADER
            END-IF.
            IF NOT SIH3-INVOICE-ITEM
               GO TO STRT-PROCESS-ORDER-HEADER
            END-IF.
            MOVE SIH3-INVOICE-NUMBER TO SDR2-INVOICE-NUMBER.
            MOVE ZERO                TO SDR2-ITEM-NUMBER.
            PERFORM 4200-START-SADTL02.
            IF WS-SADTL02-IS-AT-END
               GO TO STRT-PROCESS-ORDER-HEADER
            END-IF.
            PERFORM 2100-PROCESS-ORDER-DETAIL.
            GO TO STRT-PROCESS-ORDER-HEADER.
       EXIT-PROCESS-ORDER-HEADER.
            EXIT.

This section of code processes a file, and contains 5 GO TO statements. The first two GOTO's direct program flow to quit processing the file. The 3rd and 4th GOTO's Continue processing the file, but just skip the current record, and the last GOTO is what makes this block of code a loop.

The GOTO is mainly used to control looping. With that said I don't use GOTO's in Java, C++, etc... I think they are a bad habit for most new programmers to develop, and are not needed in most languages.
  • 0
Your thoughts are the architects of your destiny.

#14 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 13 December 2008 - 07:35 PM

And there you hit on why goto's exist. In older languages, like COBOL, they were necessary to enable looping and/or procedure-like behavior.
  • 0

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

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


#15 Feral

Feral

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 158 posts

Posted 13 December 2008 - 09:01 PM

A do while loop will always run once no matter what, in a situation like a game this is not a good thing since you would want to check to see if the condition of the loop is meet before entering it.

Also do while loops do have a place, but for the most part a standard while loop would suffice. And yes Bjarne Stroustrup does say to avoid do while loops when ever possible.

So as to the op, the code that lance posted will work for your needs but changing it to a while loop would be a more correct way of doing things in the situation of a game.
  • 0

#16 Termana

Termana

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 971 posts

Posted 14 December 2008 - 12:27 AM

I think you are saying in some spots "while loop" when you mean an "if loop" Feral as you have contradicted yourself. Posted via CodeCall Mobile
  • 0

#17 Feral

Feral

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 158 posts

Posted 14 December 2008 - 12:51 AM

No I was talking about the different between a 'do while' and a 'while' loop.

There is no such thing as an 'if loop', there is an 'if statement' but that is a conditional statement that executes if the conditions are meant and does not loop.
  • 0

#18 Termana

Termana

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 971 posts

Posted 14 December 2008 - 02:00 AM

oh sorry I see what your saying now, my bad :P
btw I was in a rush when I posted that and I ment to say FOR loop
  • 0

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#19 Feral

Feral

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 158 posts

Posted 14 December 2008 - 02:03 AM

Its all good, its like 5 am here and i'm still typing away at the computer. Staring at code to long gives tunnel vision lol or else i would have figured that out.
  • 0

#20 Lance

Lance

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 270 posts

Posted 14 December 2008 - 07:19 AM

can you elaborate on the bads of this language feature?
  • 0

#21 Feral

Feral

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 158 posts

Posted 14 December 2008 - 01:02 PM

I wouldn't say there is anything 'bad' per say about a 'do while' loop, they do have there place and time for use.

Bjarne Stroustrup himself says that he rarely uses a 'do while' loop because they can be the source of errors that are very hard to track down.

The point that a 'do while' loop always runs at least one time can cause headaches in cases where the code shouldn't be ran. It just serves a more general purpose to use a standard 'while' loop unless you absolutely need the code to execute at least one time. This will save you from experiencing the headaches that can come from trying to track an error cased by code that was executed that you did not want to execute.

So to recap, there is nothing bad about a 'do while' loop as long as you need the code to execute at least one time. Else stick with a normal 'while' loop if for any other reason then to save yourself the possibility of causing an unforeseen error.
  • 0

#22 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 14 December 2008 - 02:26 PM

Bjarne also admits that he's always learning new things about C++ himself. He doesn't claim to be some sort of god.
  • 0

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

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


#23 Feral

Feral

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 158 posts

Posted 14 December 2008 - 07:38 PM

Bjarne never said 'never use this feature' he simply said that he (as well as many industry professionals) uses it very rarely, relying more on a basic while loop the a do while. Simply for the reasons that I stated in my last post.
  • 0

#24 Lance

Lance

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 270 posts

Posted 14 December 2008 - 09:50 PM

Thank's for Feral's reply. The difference between do while and while is exactly that while is 0 or more pass, and do while is 1 or more pass. In this simple example, it's certainly OK to assigne choice a non-zero value and use while, there is virtually no cost to be Bjarne-compliant, so to speak.

Also thank WingedPanther. As much as I respect Bjarne, I won't workship him as a demi-god. But I do respect people's freedom of worship.
  • 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