• Keine Ergebnisse gefunden

FOB. ABO NEXT STATEMENTS

Im Dokument PICK BASIC reference manual (Seite 59-65)

GOTO 75 Incorrect Use

A- C/2 END CASE

4.4 NO OPERATIONS .1 NULL STATEMEHr

4.5.1 FOB. ABO NEXT STATEMENTS

The FOR and NEXT statements are used to specify the beginning and ending points of a program loop. A loop is a portion of a program written in such a way that it will execute repeatedly until some test condition is met. The general form:

FOR variable - expression TO expression {STEP expression}

I I I

NEXT variable

I I I

---

---

---I

initial

I I

limiting

I I

increment

I I

value

I I

value

I I

value

I

- - - - -

---

---A FOR and NEXT loop causes execution of a set of statements for successive values of a variable until a limiting value is encountered. Such values are specified by establishing: 1) an initial value for a variable, 2) a limiting value for the variable, and 3) an increment value to be added to the value of the variable at the end of each pass through the loop. When the limit is exceeded, program control proceeds to the body of the program following the loop.

The expression preceding TO specifies the initial value of the variable, the expression following TO gives the limiting value, and the optional expression following STEP gives the increment. If STEP is omitted, the increment value is assumed to be +1. The initial value expression is evaluated only once (when the FOR statement is executed). The other two expressions are evaluated on each iteration of the loop.

The function of the NEXT statement is to return program control to the beginning of the loop after a new value of the variable has been computed.

Note that the variable in the NEXT statement must be the same as the variable in the FOR statement. Gonsider the execution of the following statements:

150 FOR J-2 TO 11 STEP 3 160 PRINT J+5

170 NEXT J

Statement 150 sets the initial value of J to 2 and specifies that J thereafter will be incremented by 3 each time the loop is performed, until J exceeds the limiting value 11. Statement 160 prints out the current value of the expression

When J attains the limiting value of 11, statement 160 will again be executed and control will pass to 170. J will again be incremented (i.e., J-11+3-14), and since 14 is greater than the li~iting value of 11, the program will "fall through" statements 160 and 170, and control will pass to the next sequential statement following statement 170.

Examples of the use of FOR and NEXT:

Correct Use FOR A-1 TO 2+X-Y NEXT A

FOR K-10 TO 1 STEP -1 NEXT K

FOR VAR- 0 TO 1 STEP .1 NEXT VAR

Incorrect Use FOR 1 TO 50 STEP 5 FOR X-1 STEP 2 FOR J-5 TO 1 STEP 2 FOR Y-l TO 10 STEP -1

Explanation

Limiting value 1s current value of expression 2+X-Y; increment value is +1.

Increment value is -1 (variable K will decrement by a value -1 for each of 10 passes through the loop).

Increment value is .1 (variable VAR WILL increment by a value of .1 for each of 11 passes through the loop).

Explanation

Variable is missing.

Lim1~ing value is missing.

Increment value must be negative.

Increment value must be positive.

4.5.1.1 WHILE and UNTIL Clauses

The condition clauses WHILE and UNTIL may be used in the FOR statement. The FOR statement may be used in the following extended forms:

FOR variable - expression TO expression {STEP expression}{WHlLE expression}

FOR variable - expression TO expression {STEP expression}{UNTIL expression}

The extended form of the FOR statement functions in the same way as the basic FOR statement with the following additions.

If the WHILE clause is used, the expression specified in the clause will be evaluated for each iteration of the loop. If it evaluates to false (i.e., zero), then program control will pass to the statement immediately following the accompanying NEXT statement. If it evaluates to true (i.e., non-zero), the loop will reiterate.

If the UNTIL clause is used, the expression specified in the clause will be evaluated for each iteration of the loop. If it evaluates to true (i.e., non-zero), then program control will pass to the statement immediately following the accompanying NEXT statement. If it evaluates to false (i.e., zero), the loop will reiterate.

The following FOR and NEXT loop, for example, will execute until 1-10 or until the statements within the loop cause variable A to exceed the value 100:

FOR 1-1 TO 10 STEP .5 UNTIL A)100

NEXT I

4.5.1.2 Nesting

"TO expression" is missing.

Expression is missing after "UNTIL".

4.5.2 LOOP STAlEKENTS

Program loops may also be constructed by using the LOOP statement. The LOOP statement may be used in either of the following two general forms:

LOOP {statements} WHILE expression DO {statements} REPEAT LOOP {statements} UNTIL expression DO {statements} REPEAT

Execution of a LOOP statement proceeds as follows. First the statements (if any) following "LOOP" will be executed. Then the expression is evaluated. One of the following is then performed depending upon the form used:

When the "WHILE" form is used, the statements following "DO" (if any)

will be executed and program control will loop back to the beginning of the loop if the WHILE expression evaluates to TRUE (non-zero). Otherwise,

program control will proceed with the next sequential statement following

"REPEAT" (control passes out of the loop if the expression evaluates to FALSE, i.e., zero).

When the "UlnIL" form is used, the statements following "DO" (if any)

will be executed and program control will loop back to the beginning of the loop if the UNTIL expression evaluates to FALSE (zero). Otherwise,

program control will proceed with the next sequential statement following

"REPEAT" (control passes out of the loop if the expression evaluates to TRUE, i.e., non-zero).

Statements used within. the LOOP statement may be placed on one line separated by semicolons, or may be placed on multiple lines. Consider the following example:

LOOP UNTIL A-4 DO A-A+1; PRINT A REPEAT

Assuming that the value of variable A is 0 when the LOOP statement is first executed, this statement will print the sequential values of A from 1 through 4 (i.e., the loop will execute 4 times). As a further example, consider the statement:

LOOP X-X-10 WHILE X>40 DO PRINT X REPEAT

Assuming, for example, that the value of variable X is 100 when the above LOOP statement is first executed, this statement will print the values of X from 90 down through 50 in increments of -10 (i.e., the loop will execute 5 times).

Examples of the use of LOOP:

Correct Use J-O

LOOP PRINT J

J-J+1

WHI LE J

<

4 DO REPEAT Q-6

LOOP Q-Q-1 WHILE Q DO PRINT Q REPEAT Q-6

LOOP PRINT Q WHILE Q DO Q-Q-1 REPEAT

B-1

LOOP UNTIL 8-6 DO B-B+1

PRINT 8 REPEAT

Explanation

Loop will execute 4 times (i.e., sequential values of variable J from 0 through 3 will be printed) •

Loop will execute 5 times (i.e., values of variable Q will be printed in the following order: 5, 4, 3, 2, and 1).

Loop will execute 7 times (i.e., values of variable Q will be printed in the following order: 6, 5, 4, 3, 2, 1, and 0).

Loop will execute 5 times (i.e., sequential values of variable B from 2 through 6 will be printed) •

Incorrect Use Explanation

LOOP UNTIL 8-5 DO B-B+1 "REPEAT" is missing.

LOOP DO K-K*K;PRINT K REPEAT "UNTIL" or "WHILE" (followed by an expression) is missing.

A-5 Loop will execute indefinitely.

LOOP WHILE A)O DO PRINT A

REPEAT

Im Dokument PICK BASIC reference manual (Seite 59-65)