 # Jonathan E. Sisk's Pick/BASIC: A Programmer's Guide

## Chapter 11 - Extending the FOR-NEXT Construct

Program example 8 in the last chapter introduced the FOR-NEXT construct. This chapter's example illustrates the various extentions that are available to this loop construct. The principal statement covered is FOR- NEXT-STEP

Enter Program Example 9, shown in Fig. 11-1.

### THE STEP FUNCTION IN FOR-NEXT

The FOR-NEXT construct, in its simplest form, has the general format:

`FOR counter.variable = starting.expression TO ending.expression `

For example:

`FOR I = 1 TO 10`

Several additional features may be included in the FOR-NEXT construct. These features are the STEP function, and WHILE or UNTIL conditional expressions.

Fig. 11-1. Program Example 9.

```001 * EX.009
002 * USING "STEP" IN THE FOR ... NEXT FUNCTION
004 * JES: author's initials
005 *
006   PROMPT "-"
007 *
008 * GET BEGINNING RANGE NUMBER
009 *
010 LOOP
011   PRINT "ENTER A NUMBER BETWEEN 1 AND 10" :
012   INPUT START
013 UNTIL (START > 0 AND START < 11) AND START # "QUIT" DO REPEAT
014   IF START = "QUIT" THEN STOP
015 *
016 * GET ENDING RANGE NUMBER
017 *
018 LOOP
019   PRINT "ENTER A NUMBER BETWEEN 100 AND 200" :
020   INPUT FINISH
021 UNTIL (FINISH > 99 AND FINISH < 201) DO REPEAT
022   IF FINISH = "QUIT" THEN STOP
023 *
024 * GET STEP FACTOR
025 *
026 LOOP
027   PRINT "ENTER A NUMBER BETWEEN 1 AND 5" :
028   INPUT FACTOR
029 UNTIL (FACTOR > 0 AND FACTOR < 6) DO REPEAT
030 *
031 * HAVE ALL DATA. SHOW INSTRUCTION TO BE EXECUTED.
032 *
033 PRINT
034 PRINT "HERE'S WHAT HAPPENS WHEN WE ISSUE THE INSTRUCTION : "
035   PRINT "FOR I =" : START: "TO" : FINISH : "STEP" : FACTOR
036 *
037 * NOW DO IT
038 *
039 FOR I = START TO FINISH STEP FACTOR
040   PRINT I "L#4" :
041 NEXT I
042 PRINT
043 PRINT
044 *
045 * NOW, DO IT BACKWARDS.
046 *
047 PRINT
048 PRINT "HERE'S WHAT HAPPENS WHEN WE ISSUE THE INSTRUCTION"
049 PRINT "FOR I =" : FINISH : "TO" : START :
050 PRINT "STEP": (FACTOR * (-1)) ; * NEGATE FACTOR
051 *
053 *
054 FOR I = FINISH TO START STEP -FACTOR
055   PRINT I "L#4" :
056 NEXT I
057 *
058 * ALL DONE
059 *
060 PRINT
061 PRINT
062 END
```

Normally, when the FOR-NEXT construct reaches the bottom of the loop, where the NEXT counter.variable statement is encountered, the counter.variable is incremented by 1 (one).

The STEP feature is used to change the value by which the counter variable is incremented. For instance, if the following loop were executed:

```FOR I = 1 TO 10 STEP 2
NEXT I```

The loop would iterate five times. The first time through the loop, I is 1. When the NEXT I instruction is executed, I is incremented by 2, so its value becomes 3. The next time, I is 5, then 7, then 9, then 11, where the loop is terminated.

The first portion of this chapter's example program simply captures the value for three variables, START is the variable which serves as the starting.expression, a number between I and 9. The variable FINISH functions as the ending.expression, which in this case is a number between 100 and 200. The FACTOR variable is then captured, which is a number between 1 and 5.

`035 PRINT "FOR I =" :START :" TO" :FINISH :" STEP" :FACTOR`

Line 35 displays the instruction to be executed on line 39. Suppose you entered 1 into START, 150 into FINISH, and 5 into FACTOR. Line 35 then displays:

`FOR I = 1 TO 150 STEP 5`

Line 40 simply displays the current value of I, left-justified in a field of four blanks. Using the previous variables and values, the following output appears:

```1   6   11   16   21   26    31   36  41  46  51  56
61  66  71   76   81   86    91   96  101 106 111 116
121 126 131  136  141  146```

### DOING BACKWARD LOOPS

There are occasions where the loop may need to be decremented. This is accomplished when the STEP factor appears as a negative number. Line 50 displays the effect of having taken the previous STEP factor and multiplying it by -1, which effectively negates the previous contents of FACTOR:

`050 PRINT" STEP": (FACTOR * (-1))    ; * NEGATE FACTOR`

Lines 54 through 56 are where the decrementing loop is performed. Again using the previous variables, the following values display:

`150  145  140  135  130... down to 5`

A note on FOR-NEXT efficiency. Each argument in this example was entered from the keyboard, so the character is stored in its ASCII value. Under this circumstance, or when the arguments originate from a file, a binary conversion takes place with each reference to any of the arguments. This slows down program execution. It is actually more efficient to add zero (0) to the original ASCII value of each argument and store it. This forces the conversion to numeric values prior to using the arguments in the FOR-NEXT construct.

# REVIEW QUIZ 9

1) What function does the STEP factor serve in the FOR-NEXT statement?

2) Write a FOR-NEXT loop that counts from I TO 100, in increments of 2:

3) Write a FOR-NEXT loop that counts backwards from 100 to 1, in increments of 3: Previous chapter Next chapter Top 