SheerPower® 4GL
A Guide to the SheerPower Language


Previous Contents Index

Creating Two Conditions

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

10.2.2 EXIT DO

FORMAT:


        EXIT DO 

EXAMPLE:

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 

PURPOSE:

EXIT DO is used to exit from a DO loop.

DESCRIPTION:

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.

10.2.3 REPEAT DO

FORMAT:


        REPEAT DO 

EXAMPLE:

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' 

PURPOSE:

REPEAT DO is used to repeat part of a DO loop.

DESCRIPTION:

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

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. 


Previous Next Contents Index