learning The Repeat Function
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
learning The Repeat Function
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
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
Hi Bidge,
Here is a basic exemple
Best regards
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
https://alternatic.ch
-
- Posts: 738
- Joined: Thu Sep 11, 2014 1:49 pm
Re: learning The Repeat Function
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)
or
or
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
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
Code: Select all
put 4 into tNumberOfTimes
repeat with tCounter = 1 to tNumberOfTimes
Answer tCounter
end repeat
Code: Select all
put 4 into tNumberOfTimes
put 1 into tCounter
repeat until tCounter = tNumberOfTimes
Answer tCounter
add 1 to tCounter
end repeat
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
Last edited by mrcoollion on Tue Aug 01, 2017 10:08 am, edited 1 time in total.
Re: learning The Repeat Function
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
Ahhh...that has gone a long way to clearing things up for me. Thank you so much.
I totally understand your examples

Bidge
-
- Posts: 738
- Joined: Thu Sep 11, 2014 1:49 pm
Re: learning The Repeat Function
My pleasure.. have fun with LiveCode.. I do ! 

Re: learning The Repeat Function
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
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
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!
Best
Klaus
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
...
Klaus
Re: learning The Repeat Function
Wow..thanks Klaus.
That puts it totally into perspective for me. Great lesson!
Thank you
Bidge
That puts it totally into perspective for me. Great lesson!
Thank you
Bidge
Re: learning The Repeat Function
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
Oh, what an unusual syntax, had to read it several time to validate it!
Lazy moi would have used:


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

-
- Posts: 738
- Joined: Thu Sep 11, 2014 1:49 pm
Re: learning The Repeat Function
Think we lost Bidge
There is much more one needs to know to understand those repeat loops.

There is much more one needs to know to understand those repeat loops.
Re: learning The Repeat Function
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:
Craig Newman
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
Re: learning The Repeat Function
I concede my unusualness, it was ever thusKlaus wrote:Oh, what an unusual syntax, had to read it several time to validate it!![]()

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.mrcoollion wrote:There is much more one needs to know to understand those repeat loops.
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
This page is for you: http://livecode.wikia.com/wiki/Repeat
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Re: learning The Repeat Function
Andy .... LC CLASSIC ROCKS!