Publishers of technology books, eBooks, and videos for creative people

# Writing REALbasic Code

• Print
This chapter is from the book

## Writing Code that Repeats

Often, you want to repeat a section of code several times. You may know exactly how many times, or the number of times that you want the code to be executed may be a function of some variable quantity that you can put a name to. Or you may want to keep executing a section of code until something happens. REALbasic provides three tools for executing a section of code repeatedly: the For...Next structure, the While structure, and the Do...Loop structure.

### The For...Next structure

You use the For...Next structure when you know, or can figure out at run time, exactly how many times you want the code section to repeat (Figure 4.35).

Figure 4.35 The For...Next structure.

#### To repeat a section of code N times:

• Use the For...Next structure:

```Dim N,counter as integer
For counter = 1 To N
[statements]
Next ```

N is an integer. counter is an integer variable, which can be named anything and which gets incremented by 1 each time through the loop—that is, the block of statements repeats. After N repetitions, execution continues with the first statement after the Next.

#### To repeat a section of code N times with a countdown variable:

• Use the For...Next structure with the DownTo keyword:

```Dim N,counter as integer
For counter = N-1 DownTo 0
[statements]
Next ```

counter is decremented by 1 each time through the loop until it reaches 0. The integers on either side of the To or DownTo keyword can be any integer constant, variable, or expression, but the value to the right of a To or to the left of a DownTo must be the larger of the two; otherwise, no statements will get executed.

#### To repeat a section of code, skipping every other repetition:

• Use the For...Next structure with the Step keyword:

```Dim N1,N2,counter as integer
For counter = N1 To N2 Step 2
[statements]
Next```

The integer value following the Step keyword can be a constant, variable, or expression. For a Step value of 2, the structure executes the statements the first time through the loop and then skips the second and every even-numbered pass through the loop. This variation of the For...Next structure is used when the starting and ending values (N1 and N2 here) are particularly meaningful variables. Otherwise, it usually is simpler to use a value for N2 that is half the size.

#### To break out of a For...Next loop:

• Use the Exit keyword:

```Dim N1,N2,counter as integer
For counter = N1 To N2 Step 2
[statements]
Exit
[statements]
Next```

Although doing so is not good coding practice, it can be useful, especially when you are debugging your code, to put an Exit inside a loop. When the Exit is encountered, execution immediately transfers to the first statement after the Next.

### The While and Do...Loop structures

Sometimes, you don't know how many times you need to repeat a section of code; you just know that you want to keep on doing it until something happens or while some condition holds. For these cases, REALbasic provides the While and Do...Loop structures (Figure 4.36).

Figure 4.36 The While and Do...Loop structures.

#### To repeat a section of code while or until some condition holds:

• Use one of the following:

```While [condition]
[statements]
Wend```

or

```Do Until [condition]
[statements]
Loop```

or

```Do
[statements]
Loop Until [condition]```

The While structure executes the statements while the condition holds true. The Do...Loop structure executes the statements until the condition becomes true. Placing the test at the end of the Do...Loop structure causes the condition to be evaluated after each time through the loop, so the statements will be executed at least once.

### Tip

• In any While or Do...Loop structure, the statements that are executed must at some point cause the test condition to change to false (for a While) or true (for a Do...Loop). Otherwise, the loop will never terminate.