Repeat is the king of looping structures in Livecode, learn to master it in order to realise any repetitive task in Livecode. Don't use "repeat" for checking or intercepting user actions, loop structure must be used only for automated processes; otherwise you'll create buggy infinite loops.
Repeat has many forms, each form suited to a special programming strategy.
This loop will repeat until a given condition is true, example:
put 0 into myBuy -- myBuy = 0 repeat until myBuy = 10 -- repeat until condition true answer myBuy -- do answer add 1 to myBuy -- myBuy is one larger each loop iteration end repeat -- end of loop
In this example myBuy will iterate 9 times, from 1 to 9, answer will not show 10. The check of myBuy = 10 is made at the beginning of the loop and if true, the rest of the loop is ignored. Execution begins with the statement after the end repeat.
This form of repeat continues the looping until a given condition is no longer true. For example:
put 0 into myFatfax -- myFatfax = 0 repeat while myFatfax < 10 -- repeat while condition is true answer myFatfax -- do answer add 1 to myFatfax -- myFatfax is one larger each loop iteration end repeat -- end of loop
In this example myFatfax will iterate 9 times from 1 to 9, answer will not show 10. The check if myFatfax < 10 is made at the beginning of the loop and if false, the rest of the loop is ignored. Execution begins with the statement after the end repeat.
Repeat For n timesEdit
This form of repeat loops for a fixed number of times. For example:
put 0 into myNodes -- myNodes = 0 repeat for 10 time -- repeat looping for 10 times add 1 to myNodes -- myNodes is one larger for each iteration answer myNodes -- do answer end repeat -- end of loop
In this example myNodes will iterate 10 times, answer shows 1 to 10. Then execution continues with the statement after the end repeat.
This form maybe the most helpful. It continues looping for a fixed number of times. You can set the start number and the ending number. For example:
repeat with myNumber = 21 to 32 -- At start, myNumber = 21. Since no increment answer myNumber -- is stated, it is 1. Repeat will continue end repeat -- until myNumber > 32
In this example myNumber will iterate 12 times, from 21 to 32. Then execution continues with the statements after the end repeat. "Repeat with" is one of the slowest repeats. You can also choose your own increment. For example:
repeat with myLion = 14 to 21 step 5 -- At start myLion = 14. Increment is 5 answer myLion -- Second iterate myLion = 19 and third end repeat -- iterate exceeds 21 ending repeat
In this example myLion is set to 14, then incremented again to 19. But myLion has not yet exceeded 21, so repeat continues for another loop. That makes myLion = 24 for that last loop.
Down to Edit
Repeat can also count down, the iterate must be a negative number. For example:
repeat with Boogles = 21 down to 1 -- At start Boogles = 21, The increment is not answer Boogles -- stated, so it is -1. Repeat will continue end repeat -- until Boogles is less than 1
In this example Boogles will iterate 21 times, from 21 down to 1. Then execution continues with the statements after the end repeat. For counting down, the iterate can be any negative number. For example:
repeat with Boots = 16 down to 2 step -3 -- At start Boots = 16. The increment is answer myVar -- stated as -3. Repeat will continue end repeat -- looping until Boots is less than 2
In this example Boots will iterate 7 times, from 16 down to 2. Then execution continues with the statements after the end repeat.
Repeat For eachEdit
This is a another helpful form, also one of the fastest. You can use it with containers, strings, arrays or most any chunk expression. For example:
Item in a containerEdit
Items are comma separated values:
put red,green,blue into varMy -- varMy = red,green,blue repeat for each item Temp in varMy -- for each loop, Temp contains a new item answer Temp -- Temp will show red, then green, then blue. end repeat -- end of loop
Words in a stringEdit
Strings are composed of words. A word is text delimited by a space.
put "red green blue" into myFissure -- myFissure = "red green blue" repeat for each word Razz in myFissure -- For each loop, Razz contains a new word answer Razz -- Razz is "red", then "green", then "blue" end repeat -- end of loop
Elements in an arrayEdit
Let's see with array, you can use element this way:
put 1 into myVolt["Mario"] -- myVolt["Mario"] = 1 put 5 into myVolt["Luigi"] -- myVolt["Luigi"] = 5 put 10 into myVolt["Bobo"] -- myVolt["Bobo"] = 10 repeat for each element Muffy in myVolt -- For each loop, Muffy is a new number answer Muffy -- Muffy will show 1, then 5, then 10 end repeat -- end of loop
However the above works only at the first level of the array, not for the sub levels. Use the keys function.
Lines in a string Edit
put "Hello" & return & "Boxing" & return & "Rats" into Hite repeat for each line Bone in Hite answer Bone -- answer will show "Hello", then "Boxing", then "Rats" end repeat
Keys in an arrayEdit
You can use also the key word, so:
put 1 into Maxy["Mario"] put 2 into Maxy["Luigi"] repeat for each Key DoDah in Maxy answer DoDah -- answer will show Mario, then Luigi end repeat
If you need to travel to all keys, but you need to modify them, use 2 arrays: one is the copy of the other:
function svuotaArray tarr repeat for each key tKey in tArr put empty into tArr2[tKey] put svuotaArray(tArr[tkey]) into tArr2[tkey] end repeat return tArr2 end svuotaArray
This statement causes an exit from the rest of the current iteration, execution continues with the next iteration. Use the next repeat control structure to skip part of a repeat loop. It must be on a line of its own.
The following example performs the loop action only if the current card's name contains the letter "e" :
repeat for the number of cards of this stack go to next card if "e" is in the short name of this card then next repeat end if put the short name of this card & return after myCardsList end repeat
Usually, next repeat is used within an if control structure, so that the current iteration is skipped if a condition is true and continues if the condition is false.
This statement causes an exit from a repeat loop. The rest of the current iteration will be skipped and execution will continue with the statements after the end repeat. Use the exit repeat to end the repeat loop. It must be on a line of its own.
put 1 into myVar -- myVar = 1 repeat for 10 times -- repeat looping for 10 times answer myVar -- do answer add 1 to myVar -- myNodes is one larger for each loop iteration if myVar is 6 then -- check if myVar is 6 exit repeat -- if condition is true, then exit repeat end if -- end if statement end repeat -- end repeat statement
Usually, exit repeat is used within an if control structure, so that the current iteration is skipped if the condition is true and continues if the condition is false.
Repeat Forever Edit
This is the most dangerous form of the repeat loop, so use it wisely. Unless you insert at least one escape, it will continue and continue ... forever. The judicious use of an exit repeat is called for to escape the loop.
An example of a forever loop, done the right way:
put 0 into myFleur -- myFleur = 0 repeat forever -- start of the loop add 1 to myFleur -- myFleur is one larger each loop iteration if myFleur = 10 then -- check when myFleur becomes 10... exit repeat -- if condition is true, then exit from loop end if -- end of if command end repeat -- end of loop