Fandom

Livecode Wiki

Repeat

1,965pages 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.

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.