Is there a way of skipping inside of a for-loop by a certain increment?

I know you could use continue to do this, but what if I want to skip a number of times inside of a for-loop.

For example:

local textString = [[The beautiful butterfly has flew up into space.]]

for i = 0, #textString, 1 do
	
	if textString:match("beautiful", i) then

		local startOfWord, endOfWord = textString:find("beautiful") --> would return: 5, 13
		local incremsToSkip = endOfWord - startOfWord
		
		continue incremsToSkip --> in my head, it would skip 8 times.
	end
end

I don’t know if this is even possible, but if it is, how could I replicate something like this?

Without continue, you could just store what iteration the loop is on temporarily then add a number to that, finally check when the loop iteration is >= the variable you set.

So, how would that look like? Would I do something like this?

local textString = [[The beautiful butterfly has flew up into space.]]
local iterationNum = 0

for i = 0, #textString, 1 do
	
	if i == iterationNum then
		if textString:match("beautiful", i) then

			local startOfWord, endOfWord = textString:find("beautiful") --> would return: 5, 13
			local incremsToSkip = endOfWord - startOfWord
		
			iterationNum = i + incremsToSkip
		end
	end
end

i+=incremsToSkip
i-=1
continue

i has 1 subtracted from it because it increments after each loop

Where would I put this line of code at?

would put it whenever you want to skip iterations, so probably at “continue incremsToSkip” in your example.
just make sure you are only changing i whenever you want to skip iterations. if incremstoskip is 0 then it will just end up repeating the same iteration.

1 Like