Is there a way (or recommended construct) to easily skip to the next iteration of a repeat loop (while not breaking out of it entirely)? Like php’s “continue” statement?
Hi,
there is a workaround with a second repeat loop
This adds all odd numbers from 1 to 20
set x to 1
set total to 0
repeat until x = 20
repeat 1 times
if (x mod 2) = 0 then exit repeat
set total to total + x
end repeat
set x to x + 1
end repeat
total -- 100
Each loop tests whether the SkipNext variable should be True or False.
set testList to {}
set SkipNext to false
repeat with i from 1 to 10
if SkipNext then
-- do nothing
set SkipNext to false
else
-- script goes here !!!
copy i to end of testList
-- test whether to skip the next loop
set SkipNext to ((random number) < 0.2)
end if
-- another place to test for SkipNext
set SkipNext to ((random number) < 0.25)
end repeat
return testList
Thanks, both very usable and duly noted!
Since I needed way to be able bail out of the current loop at will at various points, I’ll go with Stefan’s nested loop example, as repeat 1 time combined with exit repeat looks like it does (functionally) exactly the same as php’s continue.