Sheerpower®
A Guide to the Sheerpower Language


Previous Contents Index

10.2.4 ITERATE DO

FORMAT:


        ITERATE DO 

EXAMPLE:

Example 10-17 ITERATE DO Statement

  do
    input 'Your name, please': name$ 
    if  _exit  then exit do
    if  name$ = 'SKIP'  then iterate do
    print 'Hello, '; name$ 
  loop
  end
 
 
Your name, please? FRED 
Hello, Fred 
Your name, please? SKIP 
Your name, please? exit 

PURPOSE:

ITERATE DO is used to repeat a loop, skipping part of the loop.

DESCRIPTION:

ITERATE DO repeats a loop. When Sheerpower executes ITERATE DO, it jumps to the LOOP or END DO statement. Any statements between the ITERATE DO and the end of the DO block statement will be skipped.

If ITERATE DO is used in a nested loop, Sheerpower iterates the innermost loop.

Example 10-18 ITERATE DO Used in a Nested Loop

  do
    let i = i + 1 
    do                              //<----- Sheerpower will iterate 
      input 'Your name, please' : name$ 
      if  name$ = 'SKIP'  then iterate do
      if  _exit  then exit do
      print 'Hello, '; name$ 
    loop                            //<---- this inner loop 
    print 'We now have'; i; 'set(s) of names.' 
  loop
  end

10.3 EXECUTE

FORMAT:


        EXECUTE str_expr 

EXAMPLE:

Example 10-19 EXECUTE Statement

  input 'Enter a video attribute': video$ 
  z$ = 'print ' + video$ + & 
      ': "This will be printed using ' + video$ + '"' 
  execute z$ 
  end
 
 
Enter a video attribute? bold 
This will be printed using bold

EXAMPLE:

Example 10-20 EXECUTE Statement

  nbr_fields = 5 
  dim check$(nbr_fields) 
  check$(1) = & 
    'do \' & 
    + '  if  len(ans$) <> 9  then \' & 
    + '    message error : "SSN must be 9 digits" \' & 
    + '    exit do \' & 
    + '  end if \' & 
    + '  if  not valid(ans$, "number")  then  \' & 
    + '    message error : "SSN must be numeric" \' & 
    + '    exit do \' & 
    + '  end if \' & 
    + '  print "SSN is valid" \' & 
    + 'end do' 
  field_nbr = 1 
  input 'SSN' : ans$ 
  execute check$(field_nbr) 
  end
 
 
SSN? 123456789 
SSN is valid 

DESCRIPTION:

EXECUTE allows new code to be incorporated into the program at runtime. It is used mostly for generalized procedures, utilities, and tools.

A string is built which contains the Sheerpower statements to execute. Multiple Sheerpower statements are separated by either a line feed character [ chr$(10) ] or a "\" character.

When an EXECUTE statement is encountered, Sheerpower compiles the code contained in the string and then runs that code. All program variables are available to the executed code, and any variables established by the executed code are available to the rest of the program.

An executed string is compiled only once. When a string has been compiled, the code contained within that string is processed as efficiently as the main program code.

Note: There can not be any COMMENTS in an execute string because the first "!" or "//" causes the rest of the string to be assumed as part of the comment. The "\" or CHR$(10) is an "end-of-statement" indicator (not "end-of-line").

The EXECUTE statement makes the coding of powerful generalized routines very easy.

10.4 Conditionals

Conditionals are constructs which specific blocks of code to be executed depending on one or more conditions. For instance, suppose you are doing a tax program. You need to use the EZ form if certain conditions are met, the short form if others are met, and the long form otherwise. You can use a conditional to determine which form to use.

There are two types of conditionals: IF and SELECT. The IF construct is useful to check one or more conditions and execute a different block of code depending on the result. For instance, say that you need to print one statement if the user is male and under 20, another if the user is male and between 20 and 40, and still another if the user is male and over 40. The IF construct works well in this kind of situation.

The SELECT CASE construct is useful when comparing one main expression with several values and executing a different block of code for each possible match. For instance, suppose that in the tax program we mentioned before, you need to execute a different block of code depending on the user's tax bracket. SELECT CASE would let you compare a main expression---BRACKET---with all the possible tax brackets (10000-15000, 15000-25000, etc.).

10.5 IF/THEN ... END IF

FORMAT:


        IF cond_expr THEN statement [ELSE statement] 
 
                or 
 
        IF cond_expr1 THEN 
            --- 
            ---  block of code 
            --- 
        [ELSEIF cond_expr2 THEN 
            --- 
            ---  block of code 
            ---   ...] 
        [ELSE 
            --- 
            ---  block of code 
            ---      ] 
        END IF 

EXAMPLE:

Example 10-21 END IF in IF/THEN Statement

find_age_and_sex 
 
routine find_age_and_sex 
  input prompt 'Enter your age: ': age 
  input prompt 'Enter your sex: ': sex$ 
  if  ucase$(sex$[1:1]) = 'M'  then exit routine  
  if  age < 20  then
    print 'Please go to line A.' 
  elseif  age > 19  and age < 40  then 
    print 'Please go to line B.' 
  else
    print 'Please go to line C.' 
  end if
end routine
 
 
Enter your age: 25 
Enter your sex: female 
 
Please go to line B. 

PURPOSE:

The IF construct is used to execute a statement or block of code only under specific conditions.

DESCRIPTION:

The simplest form of the IF construct is a one-line statement:


        IF cond_expr THEN statement 

cond_expr is a conditional expression. Sheerpower evaluates this expression as either TRUE (1) or FALSE (0). If the condition is TRUE, Sheerpower executes the statement following the THEN. If the condition is FALSE, Sheerpower skips the statement following the THEN and goes to the next line.

In the example program, when Sheerpower executes the first IF statement, it evaluates the conditional expression, SEX$[1:1] = 'M'. IF the user is 'Male' the condition is TRUE and Sheerpower executes the statement following the THEN and exits the routine.

IF can be used to execute a block of code. The IF block construct looks like this:


        IF cond_expr THEN 
              ---       
              ---  block of code 
              --- 
        END IF 

If the conditional expression is TRUE, Sheerpower executes the block of code beginning on the next line. END IF marks the end of this block of code. If the expression is FALSE, Sheerpower skips to the statement following the END IF.

10.5.1 ELSE Option

The ELSE option executes a statement if the conditional expression is FALSE. The format of the IF statement with the ELSE option is:


        IF cond_expr THEN statement ELSE statement 

When Sheerpower executes the IF statement, it evaluates the conditional expression. If the expression is TRUE, the statement following the THEN is executed. If the expression is FALSE, the ELSE statement is executed. (Please refer to previous example.)


Enter your age: 19 
Enter your sex: Female 
 
Please go to line A. 

In the above program, when Sheerpower executes the first IF statement, it evaluates the expression, SEX$[1:1] = 'M'. Since the user is Female, the expression is FALSE, so Sheerpower skips the THEN clause and jumps to the ELSE clause. Sheerpower executes the code between the ELSE clause and the END IF.

The ELSE option can be used to execute a block of code if the conditional expression is FALSE. The IF construct with the ELSE option looks like this:


        IF cond_expr THEN 
              --- 
              ---  block of code 
              --- 
        ELSE 
              --- 
              ---  block of code 
              --- 
        END IF 

If the conditional expression is TRUE, Sheerpower executes the block of code between the IF and the ELSE statements. If the expression is FALSE, Sheerpower executes the block of code between the ELSE and the END IF.

Example 10-22 ELSE Option in IF/THEN

find_age_and_sex 
 
routine find_age_and_sex 
  input prompt 'Enter your age: ': age 
  input prompt 'Enter your sex: ': sex$ 
  if  ucase$(sex$[1:1]) = 'M'  then exit routine    
  if  age < 40  then
    print 'Please go to line A.' 
  else
    print 'Please go to line B.' 
  end if
end routine
 
 
Enter your age: 45 
Enter your sex: female 
 
Please go to line B. 

In the above program, when Sheerpower executes the second IF statement, it checks to see if "AGE < 40". Since AGE is not less than 40, the condition is FALSE. Sheerpower skips the code following the THEN and jumps to the ELSE clause. Sheerpower executes the code following the ELSE clause.


Previous Next Contents Index