2 Replies Latest reply on Jun 1, 2007 10:18 AM by Newsgroup_User

    AS3 uint and for loops

    Rothrock Level 5
      Just wanted to share my experience.

      for(var i:uint=10;i>=0;i--){
      trace(i);
      }

      Is going to give you an endless loop. It makes sense once you know, but it was a bit of a surprise to me this morning. The issue is that the for loop evaluates after the assignment. So

      i=1
      trace 1
      deincrement, now i=0
      evaluate, yes i>=0
      trace 0
      deincrement, now i=4,294,967,295
      evaluate, yest i>0
      trace 4294967295
      and so on

      Anyways, maybe it was just me, but I thought I would share it before somebody else got caught by it.

      I think I'll stick to Number of int for my loops from now on!
        • 1. Re: AS3 uint and for loops
          kglad Adobe Community Professional & MVP
          only use uint for colors. i don't see any point in it otherwise. actually, i don't see any point in using uint for colors either but then i haven't done any testing.
          • 2. Re: AS3 uint and for loops
            Level 7
            Rothrock and kglad,

            >> deincrement, now i=4,294,967,295

            Wow, good catch!

            > only use uint for colors. i don't see any point in it otherwise.

            Technically speaking, a uint uses less memory than a Number (uint = 32
            bits, while Number "can use up to 53 bits," according to the Number entry of
            the ActionScript 3.0 Language and Components Reference). Of course, we're
            talking about the kind of savings experienced by sprinters who shave their
            legs to reduce wind drag.

            If that loop increments instead of decrements, there isn't likely to be
            a problem (I haven't hit over 4 billion of anything yet in Flash
            programming!). So the point in using uint is to save on RAM usage (so far
            as I understand it), but as Rothrock has pointed it, it's only useful in
            situations where positive integers are needed, as its range is 0 to
            4,294,967,295. In a decrementing loop, you could use an int
            (range -2,147,483,648 to 2,147,483,647) if the improved efficiency was more
            than negligible.


            David Stiller
            Adobe Community Expert
            Dev blog, http://www.quip.net/blog/
            "Luck is the residue of good design."