Fandom

Livecode Wiki

Repeat

2,161pages on
this wiki
Add New Page
Comments0 Share

"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.

It has many form, every form suits to special programming strategies, let's analyse one by one.

ForeverEdit

It's the most dangerous form, you can create infinite loops by accident, so use it wisely. You must insert at least an exit repeat, otherwise you create an infinite loop that will stuck the program. Example:

put 0 into myVar
repeat forever
  add 1 to myVar
  if myVar = 10 then
    exit repeat
  end if
end repeat

UntilEdit

This form repeat action until a given condition is true, example:

put 0 into myVar
repeat until myVar = 10
  answer myVar
  add 1 to myVar
end repeat

the example will show only 1 to 9. It will not show 10.

WhileEdit

This form repeat action until a given condition is no longer true, example:

put 0 into myVar
repeat while myVar < 10
  answer myVar
  add 1 to myVar
end repeat

the example will show only 1 to 9. It will not show 10.

For n timesEdit

This form repeat action a number of times, example:

put 0 into myVar
repeat for 10 times
  answer myVar
  add 1 to myVar
end repeat

the example will show 1 to 10.

WithEdit

This form repeat action a number of times changing automatically a variable, example:

repeat with myVar=21 to 32
  answer myVar
end repeat

the example will show 21 to 32.

You can also impose the increment (or decrement), example:

repeat with myVar=21 to 32 step 5
  answer myVar
end repeat

the example show 21,26,31 and 36!!! At 31 the repeat code analyse the sequence; since it didn't reach the end, it performs another pass.

You can also decrease, look the following examples:

repeat with myVar=21 down to 1
  answer myVar
end repeat
repeat with myVar=21 down to 1 step -3
  answer myVar
end repeat

For eachEdit

This is a very helpful structure, you can use with word, item, array element or chunk. It repeats for every condition it find in a variable and use it as a local variable, let's see the examples.

Items are comma separated values:

put "red,green,blue" into myVar
repeat for each item temp in myVar
  answer temp
end rep

the example will show separately red, green, blue.

Word are space separated values:

put "red green blue" into myVar
repeat for each item temp in myVar
  answer temp
end repeat

the example will show separately red, green, blue.

Chunk expressions are more complex expressions, you can use line, char, word, card, stack, script, item, token:

put "Hello world!" & return & "Hello world!" into myVar
repeat for each line temp in myVar
  answer temp
end repeat

the example will show separately "Hello world!" two times.

Let's see with array:

put 1 into myVar["Mario"]
put 2 into myVar["Luigi"]
repeat for each element temp in myVar
  answer temp
end repeat

the example will show 1 and 2. However it works only at the first level of the array, not for the sub levels, so it doesn't show myVar["Mario"]["car"]; use the keys function.

You can use also the key word, so:

put 1 into myVar["Mario"]
put 2 into myVar["Luigi"]
repeat for each Key temp in myVar
  answer temp
end repeat

You get: Mario, Luigi.

If you need to travel to all keys, but you need to modify them, use 2 arrays: one 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

Next repeatEdit

Skips the rest of the current iteration and goes back to the top of the repeat structure.

Use the next repeat control structure to skip part of a repeat loop.

The next repeat control structure skips the rest of the current iteration, and continues to the next iteration. 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.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.