Previous | Contents | Index |
Example 10-7 ITERATE FOR Statement in FOR Loop |
---|
for i = 1 to 3 print i input 'Your name, please' : name$ if name$ = 'Skip' then iterate for print 'Hello, '; name$ next i end 1 Your name, please? Toby Hello, Toby 2 Your name, please? Skip 3 Your name, please? Sam Hello, Sam |
ITERATE FOR is used to skip code processing.
When Sheerpower executes ITERATE FOR, it jumps to the NEXT statement. Any statements between the ITERATE FOR and the NEXT statement will be skipped.
If ITERATE FOR is used in a nested loop, Sheerpower iterates the innermost loop.
Example 10-8 ITERATE FOR Used in a Nested Loop |
---|
// count to ten, but skip a few numbers for idx = 1 to 10 if idx = 2 or idx = 6 then iterate for print 'On '; idx next idx end On 1 On 3 On 4 On 5 On 7 On 8 On 9 On 10 |
DO [WHILE expr | UNTIL expr] --- --- block of code --- LOOP [WHILE expr | UNTIL expr] |
Example 10-9 DO LOOP |
---|
a = 3 do until a = 0 input 'Your name, please': name$ print 'Hello, '; name$ a = a - 1 loop end Your name, please? Sam Hello, Sam Your name, please? Sue Hello, Sue Your name, please? Bart Hello, Bart |
A DO LOOP is used to execute a block of code repeatedly until a specified condition is met.
The simplest type of DO LOOP is an infinite DO LOOP:
Example 10-10 Infinite DO/LOOP |
---|
do input 'Your name, please' : name$ print 'Hello, '; name$ loop end |
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. DO begins the loop. LOOP marks the end of the loop. When Sheerpower reaches the LOOP statement, it jumps back to DO and executes the loop again.
The [Alt/B] command or clicking on the STOP icon in the toolbar can be used to break out of an infinite DO loop.
DO loops can be nested. Loops cannot overlap. The inner loop must be completely within the DO and LOOP statements of the outer loop.
start of outer loop --- do a = 5 do until a = 0 / input 'Your name' : name$ inner loop print 'Hello, '; name$ \ a = a - 1 loop end of print 'Done with a loop' outer loop --- loop end |
DO loops can be made conditional with WHILE and UNTIL options. WHILE and UNTIL set up a condition. The loop is executed if the condition is met.
WHILE cond_expr |
Example 10-11 WHILE Option in DO/LOOP |
---|
a = 3 do input 'Your name, please': name$ print 'Hello, '; name$ a = a - 1 loop while a > 0 print 'Finished' end Your name, please? FRED Hello, FRED Your name, please? JOHN Hello, JOHN Your name, please? KATE Hello, KATE Finished |
cond_expr is a conditional expression. When Sheerpower executes the WHILE option, it evaluates the conditional expression as either TRUE (1) or FALSE (0). If the expression is TRUE, the condition is met and Sheerpower executes the loop. Sheerpower continues executing the loop until the expression becomes FALSE. When the expression becomes FALSE, the condition is not met. Sheerpower stops executing the loop and jumps to the statement following LOOP.
UNTIL cond_expr |
Example 10-12 UNTIL Option in DO/LOOP |
---|
a = 3 do until a = 0 input 'Your name, please': name$ print 'Hello, '; name$ a = a - 1 loop print 'Finished' end Your name, please? FRED Hello, FRED Your name, please? JOHN Hello, JOHN Your name, please? KATE Hello, KATE Finished |
cond_expr is a conditional expression. When Sheerpower executes the UNTIL option, it evaluates the conditional expression as either TRUE (1) or FALSE (0). If the expression is FALSE, Sheerpower executes the loop. Sheerpower continues executing the loop until the expression becomes TRUE. When the expression becomes TRUE, Sheerpower stops executing the loop and jumps to the statement following LOOP.
WHILE and UNTIL can be attached to the DO and/or to the LOOP statements. Whenever Sheerpower encounters a WHILE or UNTIL clause, it checks whether to execute the loop. The placement of the WHILE and UNTIL clauses affects the execution of the loop.
If a WHILE or UNTIL is attached to the DO statement, Sheerpower first checks to see whether the condition is TRUE or FALSE before it executes the loop (again). In the case of a WHILE statement, if the condition is still met (i.e., is TRUE), Sheerpower executes the loop. If the condition is not met (i.e., is FALSE or is no longer TRUE), Sheerpower does not execute the loop.
In the case of an UNTIL statement, if the condition has not been met (or is still FALSE), Sheerpower executes the loop once more. If the condition has been met (i.e., is TRUE), Sheerpower does not execute the loop again.
WHILE and UNTIL options can be placed at both ends of the loop. Sheerpower evaluates each expression in turn. When it finds that one of the conditions has or has not been met (depending upon whether it is a WHILE or UNTIL clause), Sheerpower stops executing the loop. For example, when the following program runs, Sheerpower executes the loop until A equals 5 or the user enters EXIT.
Example 10-13 WHILE and UNTIL Options in DO/LOOP |
---|
dim name$(4) a = 1 do until a = 5 input 'Your name, please' : name$(a) a = a + 1 loop while not _exit print 'Finished' end |
EXIT DO |
Example 10-14 EXIT DO statement |
---|
do input 'Your name, please' : name$ if _exit then exit do print 'Hello, '; name$ loop print 'Finished' end Your name, please? Fred Hello, Fred Your name, please? exit <---- type in 'exit' Finished |
EXIT DO is used to exit from a DO loop.
When Sheerpower executes an EXIT DO statement, it jumps to the first statement following the LOOP or END DO statement. If EXIT DO is used within a nested loop, Sheerpower exits the innermost loop.
DO...END DO is a single iteration loop. The code between DO and END DO is processed only once unless conditional code specifies exiting or repeating the DO.
REPEAT DO |
Example 10-15 REPEAT DO Statement |
---|
do input 'Your name, please': name$ if _exit then exit do if name$ = '' then repeat do print 'Hello, '; name$ loop end Your name, please? Fred Hello, Fred Your name, please? Your name, please? exit <---- type in 'exit' |
REPEAT DO is used to repeat part of a DO loop.
REPEAT DO repeats all or part of the body of a loop. When Sheerpower executes REPEAT DO, it jumps to the first statement following the DO statement.
If REPEAT DO is used within a nested loop, Sheerpower repeats the innermost loop.
Example 10-16 REPEAT DO Within a Nested Loop |
---|
do i = i + 1 do //<------Sheerpower will repeat this inner loop input 'Your name, please': name$ if _exit then exit do if name$ = '' then repeat do print 'Hello, '; name$ loop //<------ print 'We now have'; i; 'set(s) of names.' loop end |
Previous | Next | Contents | Index |