1. #1
    Senior Member Follow User Gallery
    Join Date
    May 2007
    Location
    Raleigh NC
    Posts
    477

    Default Loop-Counter Variable

    Does the optional Loop-Counter Variable count up regarless if the loop completes or not?

    If I have the Loop number set to 15 but it exits on 5 does the varible return 5 or 15?

  2. #2
    Moderator Follow User Gallery
    Join Date
    Jun 2004
    Location
    UK
    Posts
    8,748

    Default

    The loop counter variable will return whatever value it has when the loop ends. If you use [LoopExit] to exit the loop then it will be the value at that point (at the start of that iteration). The starting value is 0, so if the loop code only executes once the value of the loop counter will still be 0.

    If you are using the loop counter variable in code it is good policy to use [Val,loopcounter] rather than just #loopcounter. For example:

    [CODE]
    [Loop,15,
    [If,[Val,n]<=5,
    [Note,[StrMerge,"Loop counter value is : ",[Val,n]],,1]
    ,
    [LoopExit] // at this point the loop counter variable is >5
    ]
    ,n]
    [Note,[StrMerge,"Loop counter END value is : ",[Val,n]],,1]
    [/CODE]

    As you'll see, the loop ends as soon as the loop counter is above 5 - so the end value is 6.

  3. #3
    Senior Member Follow User Gallery
    Join Date
    May 2007
    Location
    Raleigh NC
    Posts
    477

    Default

    Thanks for the quick reply Marcus.
    Interesting. That is the way I assumed it worked. I didn't know about the [Val] tho.

    The reason I ask is I am trying to easily return to a subtool (without having to mess with the blasted scroll bar) by looping "Select Down" after looping "Select Up" to get to the top subtool.

    Snipit:

    [CODE][VarSet,totalSubTools,[StrExtract,[IGetTitle,Preferences:Misc:SubTools Count],10,256]]
    [IFreeze,
    [Loop,totalSubTools,[IUpdate]
    [If,[IExists,Tool:SubTool:SelectUp],
    [IPress, Tool:SubTool:SelectUp],
    [LoopExit]]
    ,loopCount]]

    [IFreeze,
    [Loop, [Val,loopCount], [IUpdate]
    [IPress, Tool:SubTool:SelectDown]]][/CODE]


    However it seems to be pressing "Select Down" the total number of loops subtools NOT the loopCount Variable. It returns to the bottom of the list not the previously active subtool. I must be doing something wrong. If there is a better way to do this in general I am open for suggestions.

  4. #4
    Moderator Follow User Gallery
    Join Date
    Jun 2004
    Location
    UK
    Posts
    8,748

    Default

    Your problem here is due to your use of [IExists] for testing the SelectUp button. The button always exists, so the loop continues even after the top subtool is reached. What you need instead is [IsEnabled], then all will be well.

  5. #5
    Senior Member Follow User Gallery
    Join Date
    May 2007
    Location
    Raleigh NC
    Posts
    477

    Default

    Ah perfect! I knew it was some little mistake. Thanks so much Marcus! Where would I be without you?! Saved the day once again.

Posting Permissions

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