Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

Follow Me

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


Alessandro Crugnola on July 20, 2006 in general

Programming logic

During my tests with Flex2 I encountered a strange behavior with the "for" statement and I first thought about a bug of the player.
This simple script was crashing the player (it enters in an infinite loop):

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="test()">
	<mx:Script>
		<![CDATA[
			private function test():void
			{
				for(var a:uint = 10; a >= 0; a--)
				{
					trace(a);
				}
			}
		]]>
	</mx:Script>
</mx:Application>

Once I changed the type of the "a" variable into a "Number" and an "int" I understand what I was missing...
The script worked well after the type change. But to be sure I did also a little test in C which gave me the same results as the flex2 one (infinite loop):
#include 
#include 

int main(int argc, char *argv[])
{
  unsigned int a;
  for(a = 10; a >= 0; a--)
  {
	printf("a = %d\n", a);
  }
  system("PAUSE");	
  return 0;
}

Yes, it's absolutely logical!
After the last step, when a is 0, the program loses next value because it cannot be a negative value and then the loop continues to cicle.
Ok, it's not a player bug... it's me :(

 

Bookmark and Share

 

Tags: logic , programming 

 

2 comments
offcourse it's pretty normal: the a-- is allowed to go trough 0 and below, cause the for's stop condition is set to >=0, so when a, values axactly 0, it stills going down on, decrementing a, and thus as it's an UNSIGNED int variable type, it CANNOT go below 0, and might crash with a number like 65535 or a more crappy processor fault. Hope this helps.
Hi Alessandro :-) #include #include int main(int argc, char *argv[]) { unsigned int a; for(a = 10; a > 0; a--) { printf("a = %d\n", a); if(a == 1) printf("a = %d\n", --a); } system("PAUSE"); return 0; }


Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Type the characters you see in the picture above.





 

TrackBacks

TrackBack URL for this entry: http://www.sephiroth.it/cgi-bin/mt/mt-tb.cgi/121