Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
For...Loop does not work for negative indexes?

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
pjc30943



Joined: 02 Dec 2005
Posts: 220

Posted: 20 August 2008, 0:27 AM    Post subject: For...Loop does not work for negative indexes? Reply with quote

This code

Code:
Option TargetCPU zx1280n

Sub Main()
   dim Index as integer
   const minIndex as integer = -1, maxIndex as integer = 1
   
   debug.print "Begin Loop"
   for Index = minIndex to maxIndex
      debug.print "Inside Loop"
   next
   debug.print "Done with Loop"
End Sub


prints out

Code:
ZBasic v2.5.5
Begin Loop
Done with Loop



Based on the documentation, it seems that a negative minIndex should work, although in this case the loop never executes.
It would seem that negative integers are not supported, but

Code:
   const minIndex as integer = -3, maxIndex as integer = -1


correctly executes as

Code:
ZBasic v2.5.5
Begin Loop
Inside Loop
Inside Loop
Inside Loop
Done with Loop


Any ideas about what's going on?
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2493
Location: Portland, OR

Posted: 20 August 2008, 2:19 AM    Post subject: Re: For...Loop does not work for negative indexes? Reply with quote

pjc30943 wrote:
Based on the documentation, it seems that a negative minIndex should work, although in this case the loop never executes.
Quite so. You've discovered a previously undetected front end optimization error related to the interpretation of numeric constant values. The source of the problem has been found and corrected. This change will be in the next round of regression testing and we'll create a test case to cover it.

Thank you for providing a concise test case. That makes resolving problems easier than it might otherwise be.

In the mean time, you can turn off optimization and the problem will disappear. This shouldn't have any significant effect on your program since it is native mode and the back end compiler does its own optimization which is unaffected by --optimize=no-optimize. The second workaround is to introduce intermediate variables as illustrated below. Again, this shouldn't have any significant effect on your program because the back end compiler will optimize them away.
Code:
Sub Main()
   Dim Index as Integer
   Const minIndex as Integer = -1, maxIndex as Integer = 1
   Dim startIdx as Integer = minIndex
   Dim endIdx as Integer = maxIndex

   Debug.Print "Begin Loop"
   For Index = startIdx to endIdx
      Debug.Print "Inside Loop"
   Next
   Debug.Print "Done with Loop"
End Sub
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group