AutoHotkey Community

It is currently May 26th, 2012, 4:36 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: March 28th, 2009, 8:48 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
Functions: for and forE

Description
  • Use for or forE as the count in a loop to emulate a for loop.
  • Supports nested loops (see example 2 below)


Download
for.zip

Requirements
None



Functions
for(ByRef LoopVariable, start, stop, ByRef step)
Loop from start to stop (inclusive), with the specified step. If step is 0 or the empty string, 1 will be used if start <= stop, and -1 will be used if start > stop.

ReturnValue
Returns the number of loops required to do this - for use as the count in a loop.



forE(ByRef LoopVariable, start, stop, ByRef step)
Loop from start to stop (exclusive), with the specified step. If step is 0 or the empty string, 1 will be used if start <= stop, and -1 will be used if start > stop.

ReturnValue
Returns the number of loops required to do this - for use as the count in a loop.


Note: if start < stop and step is negative or start > stop and step is positive, zero loops will be done.



Code (for function)
Code:
;loops from start to stop (inclusive) with the given step
for(ByRef LoopVariable, start, stop, ByRef step)
{
    if (!step)
        step := (start <= stop ? 1 : -1)

    LoopVariable := start

    return floor((stop - start) / step) + 1
}


Code (forE function)
Code:
;loops from start to stop (exclusive) with the given step
forE(ByRef LoopVariable, start, stop, ByRef step)
{
    if (!step)
        step := (start <= stop ? 1 : -1)

    LoopVariable := start

    return ceil((stop - start) / step)
}


ex
Code:
;loops zero times (start < stop, but step is negative)
Loop, % for(i, 5, 7, step := -1)
{
    MsgBox, % i

    i += step
}


;loops zero times (start > stop, but step is positive)
Loop, % for(i, 3, -1, step := 2)
{
    MsgBox, % i

    i += step
}



Note: if you use a continue statement in the loop, don't forget to increment the value before continuing.

ex
Code:
Loop, % for(i, 1, 7, step := 2)
{
    if (i = 3)
    {
        ;don't forget to increment the value before continuing
        i += step

        continue
    }

    MsgBox, % i

    i += step
}



Example 1
Code:
/*
General form

Loop, % for(LoopVariable, start, end, IncrementVariable := step)
{
    ;loop code goes here

    LoopVariable += IncrementVariable
}


or if IncrementVariable already contains the step

Loop, % for(LoopVariable, start, end, IncrementVariable)
{
    ;loop code goes here

    LoopVariable += IncrementVariable
}
*/


;if !step (0 or blank), 1 is used if start <= stop, and -1 if start > stop
MsgBox, % "Loop from -1 to 3 (inclusive), step 1"

;IncrementVariable can be set to a function's return
;func() returns "" - so 1 will be used as the step (since start <= stop)
Loop, % for(i, -1, 3, step := func())
{
    MsgBox, % i

    i += step
}


;if !step (0 or blank), 1 is used if start <= stop, and -1 if start > stop
MsgBox, % "Loop from 3 to -1 (exclusive), step -1"

;-1 will be used as the step (since start > stop)
Loop, % forE(i, 3, -1, step := 0)
{
    MsgBox, % i

    i += step
}


MsgBox, % "Loop from 0 to -2 (inclusive), step 2 (loops zero times)"

Loop, % for(i, 0, -2, step := 2)
{
    MsgBox, % i

    i += step
}


MsgBox, % "Loop from 1 to 5 (inclusive), step 2"

Loop, % for(i, 1, 5, step := 2)
{
    MsgBox, % i

    i += step
}


MsgBox, % "Loop from 6 to 3 (exclusive), step -3"

Loop, % forE(i, 6, 3, step := -3)
{
    MsgBox, % i

    i += step
}


MsgBox, % "Loop from -5 to -2 (inclusive), step 2"

Loop, % for(i, -5, -2, step := 2)
{
    MsgBox, % i

    i += step
}


;even works for decimals
MsgBox, % "Loop from 0.2 to 0.95 (exclusive), step 0.25"

Loop, % forE(i, 0.2, 0.95, step := 0.25)
{
    MsgBox, % i

    i += step
}

return

;returns the empty string - used as the step for first loop
func()
{
}



Example 2
Code:
;an example showing nested for loops - no difference in syntax
;note: you can use an the outer loop's variable as either
;   the start, stop, or step for an inner loop.

;number of times to loop
n := 5

MsgBox, % "Nested loop:`n"
    . "i loops from 1 to n (inclusive), step 1`n"
    . "j loops from i to n (exclusive), step 1`n"

loop, % for(i, 1, n, i_step := 1)
{
    loop, % forE(j, i, n, j_step := 1)
    {
        MsgBox, % "i: " . i . "`n"
            . "j: " . j
               
        j += j_step
    }

    i += i_step
}



How to use
Extract the zip's contents to a library folder for automatic inclusion - StdLib compliant.

A copy of the above examples can be found in the "Func Examples" folder.


Download for function

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: maul.esel and 13 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group