Proper Loop

  • 14 years ago

    I'm having trouble getting a loop to work.

    I have tried Do....Loop, Do While.....Loop, Do Until.....Loop

    I have not tried a For Loop because I am unsure how to write it.

    Anyway....

    here's what I am trying to do:

    Dim PlayerLife As Integer = 1000

    Dim MonsterLife As Integer = 1000

    Dim PlayerDamage As Integer = 1

    Dim MonsterDamage As Integer = 2

     

    Loop

    PlayerLife = PlayerLife - MonsterDamage

    MonsterLife = MonsterLife - PlayerDamage

    End Loop

    Exit the loop when either PlayerLife or MonsterLife is < = 0.

    When I tried putting these equations in the loop structures that I mentioned above that I did try, 1 of 2 things happened:

    Either the program hung and froze while trying to do it OR only the first line in the loop was calculated down to 0 and the second line was ignored and remained at it's original value.

    According to the numbers I have posted above, PlayerLife would reach 0 first and MonsterLife would be a value of 500.

    What can I do to get this loop to work and calculate both results until one of them reaches 0?

  • 14 years ago
    I usually consider two different loops. A 'Do While .... Loop' or a 'Do ... loop until'.  The first one enters the loop if a condition is true and will loop until that condition is false.  The second style will execute the code in the loop at least once and will loop until the condition is True.  I'm having a difficult time figuring out exactly what you did wrong, but my suggesstion would be to use a 'Do While...loop'.  This is because technically the monster or player could already be dead.  You want to loop while the player and the monster are alive. 
            Do While PlayerLife > 0 AndAlso MonsterLife > 0
                PlayerLife = PlayerLife - MonsterDamage
                MonsterLife = MonsterLife - PlayerDamage
            Loop
    You could write it slightly differently but it's really just preference and changes nothing. The following loop uses 'Until' instead of 'While'. In this case we loop until either player/monster is dead. In the above we looped while they were alive. Both accomplish the same thing, like I said it's just preference.
            Do Until PlayerLife <= 0 OrElse MonsterLife <= 0
                PlayerLife = PlayerLife - MonsterDamage
                MonsterLife = MonsterLife - PlayerDamage
            Loop
  • 14 years ago

    Thank you TwoFaced, I will try your suggestion and see what happens.

    I just want to be able to figure out that when a player/monster battle occurs, who is alive at the end.

    If player is alive and the monster dies then call some procedure

    if player dies, and the monster is alive ..... call a different procedure...

    That I can check with an if statement, once the loop has completed, I just had difficulty making the loop properly calculate both mathematical statements either because it hung, or only did one and not the other.

    See, I had tried

    Do While PlayerLife And MonsterLife >= 0

    Do Until PlayerLife Or MonsterLife <= 0

    I dunno.... but I will try as you say and see what happens :)

  • 14 years ago
    I think the problem was you believed the condition "PlayerLife Or MonsterLife <= 0" would be true if Playerlife <=0 or Monsterlife <= 0.  In reality what that condition is saying is Do Until (PlayerLife) OR (MonsterLife <= 0).  Notice how I broke up the statement.  An integer value of 0 represents false.  An integer value of anything else represents true.  This would cause the loop to exit prematuly because PlayerLife (which was a number other then 0) was evaluating to true.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I invented the term Object-Oriented, and I can tell you I did not have C++ in mind.” - Alan Kay