How to exit a repeat with loop after 10 iteration regardless

I have an Applescript with contains the following snippet:


--
-- theElements variable is initialised here
--
repeat with theElement in theElements
    --
    -- do lots of stuff with theElement
    --
end repeat

Unfortunately without adding anything the loop might run for many iterations - for test purposes how can I exit the loop after say 10 or 100 iterations?

Hi.

If you don’t know how many elements there’ll be (ie. more or less than your limit), the best way would be to implement a counter:

set limit to 10
set counter to 0

repeat with theElement in theElements
	set counter to counter + 1
	if (counter > limit) then exit repeat
	--
	-- do lots of stuff with theElement
	--	
end repeat

Or:

set limit to 10

repeat with counter from 1 to (count theElements)
	if (counter > limit) then exit repeat
	set theElement to item counter of theElements
	--
	-- do lots of stuff with theElement
	--	
end repeat

Thanks Nigel.

exit repeat - that’s exactly what I was after.

Thanks,
Olli