|
ANY-maze Help > The ANY-maze reference > The Protocol page > The elements of a protocol > Testing > Procedures > Elements of a procedure > Statements available to procedures > The Repeat statement The Repeat statement
IntroductionThe Repeat statement allows simple loops to be inserted into a procedure, to enable the procedure to execute a set of statements a specific number of times, or to run continually until the test ends. Within the procedure editor, the Repeat statement visually 'contains' a set of statements, which are the statements that will get repeated. The repeat statement has four options that will determine exactly how it will repeat. These options determine what causes the procedure to 'drop out' of the loop, and are selected using a drop-down list at the bottom of the statement:
ExamplesThe following code will wait for the animal to enter a reward zone, and will then dispense a pellet. However, it will only do this three times:
Figure 1. Repeat 'A number of times' will repeat the statements contained within it a specified number of times.
The following two examples illustrate the subtle (but important!) difference between repeat while and repeat until. The first example will evaluate the condition repeat while the condition is true. This means that it will evaluate the condition before running any of the contents.
Figure 1. Repeat 'While' will evaluate the condition first, and will only execute the contents if the condition is True.
In this case, the 'Count' variable starts as 0; the condition is less than or equal to 5 so the contents will run and the count is incremented. Again, the condition is true, so the contents will run again. The third time, the count is 2; the fourth time 3; the fifth time 4; and the sixth time 5; and each time the contents of the statement will run. The seventh time it is run, the count variable's value is checked - it is now 6, so the contents of the repeat loop will not be run. So altogether, the switch will have been activated six times.
The second example will repeat the contained statements until the condition is true.
Figure 2. Repeat 'Until' will run the contents before evaluating the condition.
Regardless of the value of the 'Count' variable, the contents of the statement will run at least once. The count will then be incremented to 1 and the condition checked - it will be true, so the repeat statement will run again. After this second run, the count will be 2. After the third run it will be 3, after the fourth run it will be 4, and after the fifth run it will be 5. At this point the condition is true so the repeat statement will drop out. Altogether, in this example, the switch will have been activated 5 times.
So you can see that even though the procedure looks very similar in each case, the way it runs is actually subtly different. More informationRepeat statements can 'contain' any statements within the loop. This can include further Repeat statements or If statements, and these in turn can contain Repeat statements or If statements, 'nested' to any level. Within the ANY-maze procedure editor, dragging a Repeat statement will also drag its contents. Similarly, deleting the statement will also delete all its contents. If you want to keep the contents within the statement, but delete the Repeat statement itself, you'll need to drag the contents out of the Repeat statement first, to a different point in the procedure, before deleting it. See also:
© Copyright 2003-2026 Stoelting Co. All rights reserved ANY-maze help topic T0480 |