Section 3.4
Programming In QuickBASIC
3.4 Iteration
Iteration (also called looping or repetition) is a fundamental building block in
structural programming.
3.4.1 FOR NEXT Loops
The FOR NEXT
structure allows looping to take place.
FOR countervariable = startvalue TO endvalue STEP increment
   - - -
   - - -
   Body of loop
   - - -
   - - -
NEXT countervariable
The counter variable is automatically assigned the value of startvalue
the first time that the loop is executed.
The counter variable is automatically increased whenever the NEXT
statement
is reached.
The loop is terminated when the counter variable is increased above the finished value.
Negative and non-integer steps lengths are allowed. If the STEP
statement is
omitted, then the default value is one.
3.4.2 Nested FOR NEXT Loops
FOR countervariable_a = startvalue TO endvalue
   - - -
   - - -
   FOR countervariable_b = startvalue TO endvalue
      - - -
      - - -
   NEXT countervariable_b
   - - -
NEXT countervariable_a
Loops can be nested inside each other as demonstrated above. To improve the readability
of the program, the contents of each loop is tabulated.
Example Program
3.4.3 The READ, DATA And RESTORE Statements
The READ
statement assigns data to a varibale. The data is provided with the
program listing.
READ <variable_name>, <variable_name>, <variable_name>
- - -
- - -
DATA <value 1>, <value 2>, <value 3>
The first time the program is run, the data pointer starts at the first data value at the
first DATA
statement.
The first variable after READ
statement is assigned a value by the data pointer.
The data pointer then advances to the next pointer.
The RESTORE Statement
Whenever QuickBASIC encounters the RESTORE
statement, the data pointer is moved
back to the first value at the first DATA
statement. It is a good idea to place a
RESTORE
statement at the top line of all programs involving a DATA
statement.
FOR NEXT
statements are usually used to read from DATA
statements.
3.4.4 Logical And Boolean Expressions
Any condition will be true or false.
The following are used to create decision structures.
Logical
|
Boolean
|
<
|
Less Than
|
AND
|
Both Conditions Must Be True
|
>
|
Greater Than
|
OR
|
Only One Condition Needs To Be True
|
=
|
Equal To
|
<=
|
Less Than Or Equal To
|
>=
|
Greater Than Or Equal To
|
<>
|
Less Than Or Greater Than (Not Equal To)
|
3.4.5 Conditional Looping Structures
The DO LOOP
enables us to execute a loop an unknown number of times
depending on the given condition.
Pre-Checked Loops
DO WHILE <condition>
   - - -
   - - -
   - - -
LOOP
Hence the block of statements are looped over and over whilst the condition remains
true.
DO UNTIL <condition>
   - - -
   - - -
   - - -
LOOP
Hence the block of statements are looped over until the condition becomes true.
In both these situations, it is possible that none of the code inside the loop will
be executed if the condition fails the first time.
Post Checked Loops
Here, the condition comes at the end of the loop. These are usually used where the one
of the variables in the condition is calculated in the loop.
DO
   - - -
   - - -
   - - -
LOOP WHILE <condition>
Whilst the condition is true, the block of statements is looped.
DO
   - - -
   - - -
   - - -
LOOP UNTIL <condition>
Unlike pre-checked loops, the code will always be executed at least once.