IB Statements/do

From CometWiki

Jump to: navigation, search

DO statement

Syntax: DO {WHILE | UNTIL} relational-expression

[conditional statement block]

LOOP

(or)

DO

[statement block]

LOOP {WHILE | UNTIL} relational-expression

(or)

DO

[statement block]

LOOP


Discussion: The DO and LOOP statements execute a block of code repeatedly, either (1) while a specified condition is true, or (2) until a specified condition is true.

The relational expression may be any expression containing relational operations such that it can be evaluated as true or false. (For more information, see the IF/THEN discussion.)

DO/WHILE will evaluate the relational expression and execute the statement block if the expression is true. At the LOOP statement, control returns to the expression evaluation.

DO/UNTIL functions in the same manner, except that the code is executed if the expression is not true. In both cases, the evaluation of the expression is at the top of the loop.

DO followed by LOOP/WHILE or LOOP/UNTIL causes the statement block to be executed at least once, no matter whether the expression is true or false. The test occurs at the bottom of the loop, and the statement block is executed again if the expression is true, for the WHILE case, or false, for the UNTIL case.

DO/LOOP with no conditional clause should be used with care. The test that determines whether the loop should be executed or not must occur in the code itself using the BREAK statement.

Example 1:

  DO WHILE TOTQUAN LT LIMIT
  TOTQUAN=TOTQUAN+QUAN
  GOSUB GETNXT
  LOOP

Whenever TOTQUAN is greater than or equal to LIMIT, control will pass immediately to the statement following the LOOP statement. The same result will occur if the first statement is:

DO UNTIL TOTQUAN GE LIMIT


Example 2:

  DO
  TOTQUAN=TOTQUAN+QUAN
  GOSUB GETNXT
  LOOP WHILE TOTQUAN LT LIMIT

(or)

LOOP UNTIL TOTQUAN GE LIMIT

In this example, the statement block is executed at least once.

Example 3:

 DO
 .
 IF X=25 [THEN] BREAK
 .
 LOOP

In this example, the loop is terminated if the variable X equals 25.

Additional discussion: The DO/LOOP structure encompasses several specialized keywords, including DO, LOOP, WHILE, UNTIL, BREAK, and CONTINUE. Of these, only two are required to define a loop: the DO statement at the beginning and the LOOP statement at the ending.

Thus, a simple DO loop would look like this:

 DO
 [statement block]
 LOOP

This structure defines an "endless" loop, and should be used with care. The code to terminate such a loop would be contained in the statement block and would include the BREAK statement as a way to end the loop (more on this below).

A more common approach is to include conditions for the loop. Internet Basic accomplishes this with the WHILE and UNTIL clauses. While most structured languages include these clauses for the DO statement only, we've implemented them for the DO statement and the LOOP statement. If you use a conditional clause on the DO statement, the condition is evaluated at the top of the loop. If you use a conditional clause on the LOOP statement, the condition is evaluated at the bottom of the loop, meaning that the statement block will be executed at least once. Note that a conditional clause may be used either with the DO statement or with the LOOP statement, but not with both statements in the same structure.

For example, the WHILE clause can be used with the DO statement as follows:

 DO WHILE relational-expression
 [conditional statement block]
 LOOP

The conditional statement block will be executed as long as the relational expression is true. When the relational expression is false, the program will branch to the first statement following the LOOP statement. On the other hand, the UNTIL clause will cause the execution of the conditional statement block as long as the relational-expression is false. When the relational expression is true, the program will branch to the first statement following the LOOP statement. The UNTIL syntax is:

 DO UNTIL relational-expression
 [conditional statement block]
 LOOP

As an alternative, either conditional clause may be used on the LOOP statement, as follows:

 DO
 [statement block]
 LOOP WHILE relational-expression
 (or)
 DO
 [statement block]
 LOOP UNTIL relational-expression

In both of these cases, the statement block will be executed at least once; the loop test is performed at the bottom of the loop.

Two program control statements can be used to modify the statement execution sequence in a DO/LOOP structure. These statements are BREAK and CONTINUE. The BREAK statement stops execution of the conditional statement block within and passes control to the statement following the LOOP statement. For example:

 DO WHILE I LE 10
 .
 .
 IF A=I [THEN] BREAK      ! jump out of the loop
 .
 .
 LOOP

The CONTINUE statement passes control to the LOOP statement in a DO/LOOP structure. For example:

 DO WHILE A LT 100
 .
 .
 IF A=B
      CONTINUE     ! branch to the LOOP statement
 ELSE
 .
 .
 ENDIF
 .
 .
 LOOP

Note: The BREAK and CONTINUE statements may also be used in the FOR/NEXT structure. BREAK passes control to the statement following the NEXT statement, while CONTINUE passes control to the NEXT statement. In addition, the BREAK statement may be used in the SELECT/CASE structure

Personal tools