User`s guide

E-Prime User’s Guide
Chapter 4: Using E-Basic
Page 147
When the loop begins, the counter is set to start. When the Next statement is executed, the
counter is incremented by one. When the counter is equal to the end value supplied, the loop
terminates and the next line of code outside the loop structure is executed.
Tips:
Keep it simple. Unless there is a specific reason to start the counter at another value,
use 1 to n.
Although the counter variable is not required to follow the Next statement, it is good
practice to include it. It may seem verbose, but makes parsing through the code a bit
easier.
Avoid changing the value of the counter within the loop structure manually. Let the Next
statement increment the counter unless there is a specific reason to change the counter
(e.g., set it to the end value to terminate early).
For…Next loops are particularly useful when working with arrays. An array is similar to a
storage bin with numbered slots. Arrays are covered in more detail in section 4.7-
Programming: Advanced.
Exit For
Similar in concept to Exit Do, the Exit For statement allows the loop to terminate early. This is
typically used in conjunction with If..Then and Select Case statements within the For…Next loop.
For Each…Next Loop
This version of the For…Next Loop is similar to the previous version. However, the primary
distinction is that it is used to perform a block of code for each element within a set, rather than
for a specified number of times. For Each…Next requires an element or variable that
corresponds to the object types within the collection.
For Each variable In collection
<block of statements to be executed>
Next variable
Notice a counter is not specifically used in this version of a For Loop. Instead, E-Basic figures out
how many times to repeat the block of code based on the items in the collection specified. This is
particularly useful when debugging. If a problem is suspected with perhaps one of the
TextDisplay objects within a block, try 'stepping' through the processing of each object displaying
a marker to the screen to help track down the problem.
4.5.2.3 Interrupting the Flow
GoTo Label
When a design calls for jumping to another place within the script based on a specific flag, the
Goto statement is useful. In behavioral research, this is often required for contingent branching
experiments. For example, perhaps the execution flow should continue uninterrupted until a
subject responds by pressing "a” key. If the subject presses any other letter, the execution flow
should jump, or Goto a specific location within the code. In the example below, a Label is placed
prior to an input statement (i.e., AskBox). The input is examined, and if it is not the required
input, the execution of the program jumps back to the Label at the beginning of the script in order