Think of something that you wish Authorware could do but it doesn't?  Let the our good friends at Macromedia know via the wishlist.

Please let us know if you find any of the materials on this site inappropriate or offensive. Please include the url and why the material should be reviewed.

Comments and questions about the site are also welcome. Please no Authorware questions, use the AWARE list.

Back

1070 - I don't understand repeat loops. Help!

by - Joseph Ganci


I don't understand what the variable i is doing in the repeat loop line

repeat with i := 1 to 10

Can you explain?

The i in the repeat loop is like a counter. Take a look at the following script, which you can place in a Calc icon and run. It will draw 10 boxes on the screen, each of which starts at location 100, 100 at its upper left and ends at a different location at the bottom right and each of which has a different line thickness.

repeat with i := 1 to 10
 
  Box(i, 100, 100, 100 + 20 * i, 100 + 20 * i)
    SyncPoint(0)
    SyncWait(i)
end repeat

Notice that this is a repeat loop that uses i as a conter. The purpose of the repeat loop is to do the same thing over and over but perhaps with some change each time. In this case, we're using the user variable called i. Every time we loop, i will be increased by 1 until it reaches 10, for a total of 10 times through the loop.

The first time through the loop, i will be set to 1. There are three functions in the repeat loop, so they will now execute. Let's look at each one.

The first function, Box, has five arguments. The first argument is the line thickness with which the line will be drawn. If the value of this argument is 1, the line thickness will be 1. If it is 2, the line thickness will be 2, and so on. In this case, the argument is set to i, which is our loop counter. This means that the first time through the loop, the line thickness will be 1, the second time 2, and so on, on up to the last iteration, the last box that will be drawn, which will have a thickness of 10 pixels.

The other four arguments to the function, which are (note the commas):

100
100
100 + 20 * i
100 + 20 * i

refer to the starting location of the box (100, 100) and the ending location of the box (100 + 20 * i, 100 + 20 * i). That means the top left corner of all 10 boxes will start in the same place, but the bottom right will depend on the value of i, multiplied by 20, and added to 100. So the first box will be drawn from 100, 100 to 120, 120, the second box will be drawn from 100, 100 to 140, 140, and so on until the tenth box will be drawn at 100, 100 to 300, 300.

The next two functions, SyncPoint and SyncWait, work together to pause a certain amount of time. The amount of time is normally placed between the parentheses following SyncWait. If we had placed SyncPoint(3) there, it would always wait 3 seconds between each box draw. In this case, we put i again, which means after the first box is drawn, we will wait one second, after the second box is drawn, we will wait two seconds, and so on.

Can you see the power of the loop counter i here? By using it (i) in both the Box and SyncWait functions, we changed the outcome of the box and the amount of time we waited every time we looped. The end repeat line, by the way, indicates the end of the repeat loop and automatically increments the variable i to the next number up. If it determines that this number is higher than the end number we placed at the beginning of the repeat (10), then we stop executing the repeat loop, otherwise we continue with i having been incremented. I hope this helps.

There are 0 reviews
Add your review
Back