SheerPower® 4GL
A Guide to the SheerPower Language


Previous Contents Index

10.1.3 EXIT FOR

FORMAT:


        EXIT FOR 

EXAMPLE:

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 

PURPOSE:

EXIT FOR is used to exit from a FOR loop.

DESCRIPTION:

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.

10.1.4 REPEAT FOR

FORMAT:


        REPEAT FOR 

EXAMPLE:

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 

PURPOSE:

REPEAT FOR is used to increment the index variable.

DESCRIPTION:

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

10.1.5 ITERATE FOR

FORMAT:


        ITERATE FOR 

EXAMPLE:

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 

PURPOSE:

ITERATE FOR is used to skip code processing.

DESCRIPTION:

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 

10.2 DO LOOP

FORMAT:


        DO [WHILE expr | UNTIL expr] 
                --- 
                ---  block of code 
                --- 
        LOOP [WHILE expr | UNTIL expr] 

EXAMPLE:

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 

PURPOSE:

A DO LOOP is used to execute a block of code repeatedly until a specified condition is met.

DESCRIPTION:

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.

10.2.1 WHILE and UNTIL Options

FORMAT:


        WHILE cond_expr 

EXAMPLE:

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 

DESCRIPTION:

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.

FORMAT:


        UNTIL cond_expr 

EXAMPLE:

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 

DESCRIPTION:

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.

Placement of WHILE and UNTIL

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.


Previous Next Contents Index