Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: fixing stubborn FOR loops

  1. #1

    fixing stubborn FOR loops

    I've got a for loop that goes "for i := 0 to arrayProperty.count - 1 do", and it's supposed to only run if there's at least one unit in the array property being counted. (If the array's size is 0, then the expression evaluates to -1, which is less than 0, and so the for loop should never enter the first iteration.) I know this is supposed to work, because I've done it in the past. But for some reason, now all of a sudden it's entering the first loop no matter what, and sometimes trying to access item [0] of an array that doesn't contain anything.
    I figure one of the settings must have come loose somewhere. How do I fix it so it'll behave correctly and not enter the loop the first time unless the condition evaluates true?

    Mason

  2. #2

    fixing stubborn FOR loops

    Code:
    if arrayProperty.count > 0 then
      for i := 0 to arrayProperty.count - 1 do

  3. #3

    fixing stubborn FOR loops

    Yeah, that's the obvious workaround, but how do I get it to evaluate at the top of the loop in the first place?

    Mason

  4. #4
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    fixing stubborn FOR loops

    You don't. At least you shouldn't anyway.

    One of the truths of dealing with 0-based arrays.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  5. #5

    re

    "for i := 0 to arrayProperty.count - 1 do",
    you should debug printing on screen what value is "i" geting the first time interaction,

    I think "i' have to be signed so it can get -1 value; if it is defined as unsigned type (word, byte, longword, etc) then you will get a problem.

    "arrayProperty.count - 1" should return -1 when arrayProperty.count = 0 if this is not returning a signed type then you are having a problem;

  6. #6

    fixing stubborn FOR loops

    Oh! That's the problem, all right. My i was a word. So it was probably evaluating -1 as MAXINT16 -1, (or whatever it's called,) which is a big positive number.

    Thanks!

    Mason

  7. #7

    Re: fixing stubborn FOR loops

    Quote Originally Posted by masonwheeler
    I've got a for loop that goes "for i := 0 to arrayProperty.count - 1 do", and it's supposed to only run if there's at least one unit in the array property being counted. (If the array's size is 0, then the expression evaluates to -1, which is less than 0, and so the for loop should never enter the first iteration.) I know this is supposed to work, because I've done it in the past. But for some reason, now all of a sudden it's entering the first loop no matter what, and sometimes trying to access item [0] of an array that doesn't contain anything.
    I figure one of the settings must have come loose somewhere. How do I fix it so it'll behave correctly and not enter the loop the first time unless the condition evaluates true?
    IMHO this is an internal error of Delphi 7 compiler (it hasn't occured to me after I moved away from Delphi 7). Usually changing the code somewhere else, moving some parts, etc., exiting Delphi and running it again, then rebuilding the application fixes the problem.

    Also make sure you don't overwrite memory in your application as this may trigger weird things to happen like "initialization" code in your unit not being executed, etc.

  8. #8

    fixing stubborn FOR loops

    I'm using BDS 2006. And Tpascal had the solution. But thanks for trying to help.

    Mason

  9. #9

    fixing stubborn FOR loops

    how about for i:= low() to high() do begin..??
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  10. #10

    fixing stubborn FOR loops

    2 reasons. 1, efficiency. No need to waste cycles on low() when I know it's a zero-based array. 2, I'm not sure what would happen if I tried that on an empty array property.

    Mason

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •