Continue in loops

The continue keyword allows you to skip the current iteration of a loop and move to the next iteration. It can be used with all of the loops and is useful in that it allows you to ignore certain code in your loop in the event that certain conditions have been met.

This program has a for loop going from 1 to 10 that increments the variable counter. In normal circumstances counter would have a value of 10 when the for loop finished. However, in this instance an if statement is used to check when i is 5 and if this is the continue keyword is used, resulting in the remaining code for the loop being ignored and the loop moving onto the next iteration, where i will be 6 and then continues as normal. Due to us skipping out one interation the final value of counter will be 9.

do
	counter as integer = 0
	
for i = 1 to 10 if i = 5 continue endif
counter = counter + 1 next i
print ( counter )
sync ( ) loop

It's important to note that the continue keyword only affects instructions that follow it. If the previous program was adjusted so that the addition to the counter variable was done prior to the continue then its final value would be 10 because the continue hasn't skipped any code.


do counter as integer = 0
for i = 1 to 10 counter = counter + 1
if i = 5 continue endif next i
print ( counter )
sync ( ) loop