learning The Repeat Function

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

learning The Repeat Function

Post 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
jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2734
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: learning The Repeat Function

Post 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
https://alternatic.ch
mrcoollion
Posts: 744
Joined: Thu Sep 11, 2014 1:49 pm

Re: learning The Repeat Function

Post 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
Last edited by mrcoollion on Tue Aug 01, 2017 10:08 am, edited 1 time in total.
bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Re: learning The Repeat Function

Post 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
mrcoollion
Posts: 744
Joined: Thu Sep 11, 2014 1:49 pm

Re: learning The Repeat Function

Post by mrcoollion »

My pleasure.. have fun with LiveCode.. I do ! 8)
bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Re: learning The Repeat Function

Post 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
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: learning The Repeat Function

Post by Klaus »

Hi David,

a good naming scheme is half the rent!
Well it is even more than that! :D

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
bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Re: learning The Repeat Function

Post by bidgeeman »

Wow..thanks Klaus.
That puts it totally into perspective for me. Great lesson!

Thank you
Bidge
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: learning The Repeat Function

Post 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
Image
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: learning The Repeat Function

Post by Klaus »

Oh, what an unusual syntax, had to read it several time to validate it! :D

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
:D
mrcoollion
Posts: 744
Joined: Thu Sep 11, 2014 1:49 pm

Re: learning The Repeat Function

Post by mrcoollion »

Think we lost Bidge :shock:
There is much more one needs to know to understand those repeat loops.
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: learning The Repeat Function

Post 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
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: learning The Repeat Function

Post by bogs »

Klaus wrote:Oh, what an unusual syntax, had to read it several time to validate it! :D
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.
Image
MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: learning The Repeat Function

Post by MaxV »

Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
AndyP
Posts: 634
Joined: Wed Aug 27, 2008 12:57 pm
Contact:

Re: learning The Repeat Function

Post by AndyP »

Here is another great learning resource.

http://livecode.byu.edu/indexgeneric.php
Andy .... LC CLASSIC ROCKS!
Post Reply