Page 1 of 2
learning The Repeat Function
Posted: Mon Jul 31, 2017 10:45 am
by bidgeeman
Hello.
I can't seem to get my head around how to repeat a task in Livecode.
Looked at the dictionary and studied examples but it doesn't sink in. Does anyone have a very very simple example of how the repeat function works for a complete beginner they could share here?
Many thanks
Bidge
Re: learning The Repeat Function
Posted: Mon Jul 31, 2017 11:08 am
by jmburnod
Hi Bidge,
Here is a basic exemple
Code: Select all
on mouseUp
repeat with i = 1 to 10
put i & "," after tMyresult
end repeat
delete char -1 of tMyresult --avoid an empty item
put tMyresult
end mouseUp
Best regards
Re: learning The Repeat Function
Posted: Mon Jul 31, 2017 11:26 am
by mrcoollion
Hello Bidge,
I will do my best to explain.
In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached.
Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number.
In short: A repeat command is a loop that evaluates its body until a specified stopping criterion is satisfied.
The most simple repeat loop is one where a loop is set for a number of times to do something. In this case give me a beep (From LiveCode dictionary)
Code: Select all
repeat for 3 times
beep
end repeat
or
Code: Select all
put 4 into tNumberOfTimes
repeat with tCounter = 1 to tNumberOfTimes
Answer tCounter
end repeat
or
Code: Select all
put 4 into tNumberOfTimes
put 1 into tCounter
repeat until tCounter = tNumberOfTimes
Answer tCounter
add 1 to tCounter
end repeat
Be aware that you need to make sure that a repeat loop alway has an ending otherwise it keeps on running and often the only way to stop is to kill LiveCode with the application you are building.
There are different Repeat loop posibilities. See dictionary for this.
In liveCode the loopForm can be one of the following forms (from LiveCode dictionary):
forever (be very carefull with this one)
until condition
while condition
[for] number [times]
with counterVariable = startValue {to | down to} endValue [stepincrement]
for eachchunkType labelVariable in container
for each element labelVariable in array
for each key labelVariable in array
Hope to have been of some help.
Regards,
Paul
Re: learning The Repeat Function
Posted: Mon Jul 31, 2017 11:29 am
by bidgeeman
Hi Paul.
Ahhh...that has gone a long way to clearing things up for me. Thank you so much.
I totally understand your examples
Bidge
Re: learning The Repeat Function
Posted: Mon Jul 31, 2017 11:33 am
by mrcoollion
My pleasure.. have fun with LiveCode.. I do !

Re: learning The Repeat Function
Posted: Mon Jul 31, 2017 11:36 am
by bidgeeman
So...how do you put this into a more practical usage?
Say if I had 20 objects that I needed to perform a repeated function, like 20 boxes I needed to change the outline color for when a button is pressed? (Just as an example off the top of the head).
Bidge
Re: learning The Repeat Function
Posted: Mon Jul 31, 2017 12:09 pm
by Klaus
Hi David,
a good naming scheme is half the rent!
Well it is even more than that!
So of course you named your boxes like box1 - box 20, otherwise a repat loop will be a bit more difficult!
Code: Select all
...
lock screen ##!!!
repeat with i = 1 to 20
set the bordercolor of grc ("box" & i) to black
end repeat
unlock screen
...
Best
Klaus
Re: learning The Repeat Function
Posted: Mon Jul 31, 2017 1:36 pm
by bidgeeman
Wow..thanks Klaus.
That puts it totally into perspective for me. Great lesson!
Thank you
Bidge
Re: learning The Repeat Function
Posted: Mon Jul 31, 2017 4:15 pm
by bogs
Klaus is very correct on naming properly, since it allows you to repeat like this,
Code: Select all
repeat with x = 1 to the number of controls of me
if "txt" is among the characters of the name of control x then put "" into control x
end repeat
Re: learning The Repeat Function
Posted: Mon Jul 31, 2017 5:27 pm
by Klaus
Oh, what an unusual syntax, had to read it several time to validate it!
Code: Select all
repeat with x = 1 to the number of controls of me
if "txt" is among the characters of the name of control x then put "" into control x
end repeat
Lazy moi would have used:
Code: Select all
repeat with x = 1 to the number of controls of me
if the short name of control x OF ME contains "txt" then put "" into control x OF ME
end repeat

Re: learning The Repeat Function
Posted: Mon Jul 31, 2017 6:14 pm
by mrcoollion
Think we lost Bidge
There is much more one needs to know to understand those repeat loops.
Re: learning The Repeat Function
Posted: Mon Jul 31, 2017 8:09 pm
by dunbarx
One must practice. I think the best variant to start with is "repeat with.." since you can step through a loop in the debugger and watch the index increment. Then any statement that follows will contain the updated index, and you can track the result:
Code: Select all
on mouseUp
breakpoint
repeat with y = 1 to 10
put y && y * 2 into line y of temp --watch both "y" and the variable "temp"
end repeat
end mouseUp
Craig Newman
Re: learning The Repeat Function
Posted: Tue Aug 01, 2017 7:40 am
by bogs
Klaus wrote:Oh, what an unusual syntax, had to read it several time to validate it!
I concede my unusualness, it was ever thus
mrcoollion wrote:There is much more one needs to know to understand those repeat loops.
True that, and sorry to muddy up a murky topic. Btw, that really was an excellent and concise lesson on the subject =^) The only thing I might point out is using 'beep', if your on linux anyway (I don't think it applies here), sound is something that requires a very specific setup, so beep and a variety of other interesting things you can do with sound don't work on linux without that setup.
More on topic, the dictionary has some very good examples that are concise and actually work (located in the fine print). Since they are so simple and small, they are also easily modified to learn with.
Also as Craig points out, make sure you step through new to you code in the debugger, learn to develop it as a habit. It shows you things you sometimes don't anticipate easily.
Re: learning The Repeat Function
Posted: Tue Aug 01, 2017 10:37 am
by MaxV
Re: learning The Repeat Function
Posted: Tue Aug 01, 2017 10:59 am
by AndyP
Here is another great learning resource.
http://livecode.byu.edu/indexgeneric.php