Home > other >  Repeat with smaller steps than one
Repeat with smaller steps than one

Time:10-09

So I have an Applescript repeat with-loop. It ranges from 1 to 10 and logs the number every time:

repeat with i from 1 to 10
    log i
end repeat

This outputs something like this:

(*1*)
(*2*)
(*3*)
(*4*)
(*5*)
(*6*)
(*7*)
(*8*)
(*9*)
(*10*)

However, now I want i to go from 1 to 2 in steps of 0.1. Is there a built-in function to do that or do I have to work with a workaround?

CodePudding user response:

Not with the repeat with from to syntax but with repeat while and incrementing the index variable yourself

set i to 1.0
repeat while i ≤ 2.0
    log i
    set i to i   0.1
end repeat

or go from 10 to 20 and divide i by 10

repeat with i from 10 to 20
    log i / 10
end repeat

CodePudding user response:

However, now I want i to go from 1 to 2 in steps of 0.1. Is there a built-in function to do that or do I have to work with a workaround?

From repeat with loopVariable (from startValue to stopValue)

Syntax

repeat with  loopVariable  from  startValue  to  stopValue [ by  stepValue ]

    [ statement ]...

end [ repeat ]

stepValue
           Specifies a value that is added to loopVariable after each iteration of the loop. You can assign an integer or a real value; a real value is rounded to an integer.

Default Value: 1


So when using [ by stepValue ] this built-in function cannot do as you've expressed as is will always round a real to an integer.

  • Related