Previous | Contents | Index |
A LOOP is a section of code that can be repeated. There are two types of loops:
A FOR loop is used to repeat a block of code a specific number of times. FOR loops also inform the user how many times the loop was executed. A FOR loop might be used to input 10 similar data items or as a counter; for example, to count from 1 to 1000, or to make calculations from each of the 10 data items entered.
DO loops are used to execute a block of code until a specific condition is met. For instance, a DO loop might be useful to enter numbers until a 0 is entered. Additionally, DO loops are used to do calculations until two numbers match, or to continue a process until either the user chooses to stop or until a final result is reached.
Loops are constructs--they are created by using several statements which can only be used in conjunction with one another (FOR/NEXT, DO/LOOP). The statements which make up the constructs are described together.
FOR index_var = num_expr1 TO num_expr2 [STEP num_expr3] --- --- block of code --- NEXT index_var |
Example 10-1 FOR/NEXT Loop |
---|
dim name$(4) for j = 1 to 4 input 'Enter name': name$(j) print j; ' '; name$(j) next j print 'Finished' print 'Final value:'; j end Enter name? Jack 1 Jack Enter name? Tom 2 Tom Enter name? Sue 3 Sue Enter name? Toby 4 Toby Finished Final value: 5 |
The FOR loop executes a block of code a specific number of times. This construct can be used to repeat a section of code a certain number of times.
In the above example, the INPUT and PRINT statements make up the body of the loop. This block of code is executed each time the loop is repeated. (For clarity, the body of the loop is indented two spaces.) The FOR statement marks the beginning of the loop and determines how many times the loop is repeated. The NEXT statement marks the end of the loop.
index variable | V for J = 1 to 4 <-- limit expression ^ | initial expression |
The index variable keeps track of how many times the loop has been executed. The initial expression is the number Sheerpower begins counting at. The limit expression is the number Sheerpower stops counting at. In the example, Sheerpower counts from 1 to 4, so the loop executes four times.
When Sheerpower runs the example program, it executes the loop four times. The first time the FOR statement is executed, the variable J is set to 1. Sheerpower executes the body of the loop. Since J = 1, Sheerpower inputs NAME$(1), Jack, and prints NAME$(J).
J = 1 Enter name? Jack 1 Jack |
When Sheerpower reaches the NEXT J, it adds one to J and jumps back to the beginning of the loop. J is now 2. Sheerpower checks to see if J is greater than 4. Since J isn't greater than 4, Sheerpower repeats the loop. When Sheerpower executes the loop for the last time, it jumps back to the beginning of the loop and checks to see if J is greater than 4. Since J is greater than 4, Sheerpower jumps to the statement following the NEXT J (PRINT 'Finished') and continues normal program execution.
J = 1 Enter name? Jack 1 Jack J = 2 Enter name? Tom 2 Tom J = 3 Enter name? Sue 3 Sue J = 4 Enter name? Toby 4 Toby Finished Final value: 5 |
By default, when a FOR loop is executed, Sheerpower increments the index variable by one. The increment can be changed with the STEP option. The format of the FOR statement with the STEP option is:
FOR index_var = num_expr1 TO num_expr2 STEP num_expr3 --- --- block of code --- NEXT index_var |
num_expr3 is a numeric expression specifying the increment. Each time the FOR statement is executed, Sheerpower adds the increment to the index variable. Sheerpower stops executing the loop when the index variable is greater than the limit.
Example 10-2 STEP Option in FOR/NEXT Loop |
---|
dim name$(4) for j = 1 to 4 step 3 input 'Enter name': name$(j) print j; ' '; name$(j) next j print 'Finished' print 'Final value:'; j end Enter name? Fred 1 Fred Enter name? John 4 John Finished Final value: 7 |
FOR loops can be nested. A nested loop is a loop which begins and ends inside of another loop. Loops cannot overlap. The inner loop must begin and end completely within the outer loop.
Example 10-3 Nesting FOR/NEXT Loops |
---|
dim name$(4) for j = 1 to 4 //<--- start of outer loop input name$(j) for k = 1 to j print name$(k); ' '; //<--- inner loop next k print next j //<--- end of outer loop print 'Finished' end ? FRED <--- type in FRED FRED ? JOHN <--- type in JOHN FRED JOHN ? MARY <--- type in MARY FRED JOHN MARY ? KATE <--- type in KATE FRED JOHN MARY KATE Finished |
EXIT FOR |
Example 10-4 EXIT FOR Statement in FOR Loop |
---|
for i = 1 to 5 input 'Your name, please': name$ if _exit then exit for print 'Hello, '; name$ next i print 'Finished' end Your name, please? James Hello, James Your name, please? Marian Hello, Marian Your name, please? exit |
EXIT FOR is used to exit from a FOR loop.
When Sheerpower executes an EXIT FOR statement, it jumps to the first statement following the matching NEXT statement. EXIT FOR can be used only within FOR loops. If EXIT FOR is used within a nested loop, Sheerpower exits the innermost loop.
REPEAT FOR |
Example 10-5 REPEAT FOR Statement in FOR Loop |
---|
for i = 1 to 3 print i input 'Your name, please': name$ if name$ = '' then repeat for print 'Hello, '; name$ next i end 1 Your name, please? George Hello, George 2 Your name, please? 2 Your name, please? Sam Hello, Sam 3 Your name, please? Tom Hello, Tom |
REPEAT FOR is used to increment the index variable.
REPEAT FOR repeats all or part of the body of a loop. REPEAT FOR can be used only in FOR loops. When Sheerpower executes REPEAT FOR, it jumps to the first statement following the FOR statement.
If REPEAT FOR is used within a nested loop, Sheerpower repeats the innermost loop.
Example 10-6 REPEAT FOR Used Within a Nested Loop |
---|
for i = 1 to 10 for j = 1 to 5 print j //Sheerpower will input 'Your name, please': name$ //repeat this if name$ = '' then repeat for //inner loop print 'Hello, '; name$ next j print 'We now have'; i; 'set(s) of names.' next i end |
ITERATE FOR |
Previous | Next | Contents | Index |