Previous | Contents | Index |
The ELSEIF option sets up a new condition to check. The format of the IF construct with the ELSEIF option is:
IF cond_expr1 THEN --- --- block of code --- ELSEIF cond_expr2 THEN --- --- block of code --- ELSE --- --- block of code --- END IF |
ELSEIF establishes a new condition. Sheerpower evaluates this condition. If the condition is TRUE (1), Sheerpower executes the code following the ELSEIF. If the condition is FALSE (0), Sheerpower jumps to the next clause in the IF construct.
Example 10-23 ELSEIF 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 < 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. |
In the above program when Sheerpower executes the second IF statement, it checks to see if "AGE < 20". Since AGE is not less than 20, the first condition is FALSE. Sheerpower skips the code following the THEN and checks the condition set up by the ELSEIF. Since "AGE > 19" and "AGE < 40", the second condition is TRUE and Sheerpower executes the code following the ELSEIF and prints 'Please go to line B.', then exits the conditional.
SELECT CASE main_expr CASE expr1[, expr2,...] --- --- block of code --- [CASE expr3[, expr4,...] --- --- block of code --- ...] [CASE IS {numeric operator | boolean operator} expr5 --- --- block of code --- ...] [CASE ELSE --- --- block of code --- ...] END SELECT |
Example 10-24 SELECT CASE/END SELECT |
---|
do input 'Your income per year': income if _back or _exit then exit do select case income case 0 print 'No income?' case is < 0 print 'A negative income? You are in debt!' case is > 0 print 'A positive income.' end select loop end Your income per year? 0 No income? Your income per year? -15000 A negative income? You are in debt! Your income per year? 30000 A positive income. Your income per year? exit |
Sometimes it is necessary to compare one main expression with several values and execute a different block of code for each possible match. SELECT CASE is used to check a set of conditions and execute code depending on the results.
The SELECT CASE statement begins the construct and gives the main expression (main_expr). In the example, the main expression is INCOME. The CASE statements are compared with the main expression. The first CASE expression (expr1) is 0. Following this CASE is a block of code. If INCOME = 0 the block of code following CASE 0 is executed.
Each CASE statement can include several expressions separated by commas. Sheerpower compares each of the expressions in a CASE statement. If any of them match, the block of code following the CASE statement is executed.
Example 10-25 CASE Statement |
---|
do input 'Procedure (add, del, exit)': pro$ if _exit then exit do pro$ = ucase$(pro$) select case pro$ case 'ADD' print 'Adding...' case 'DEL', 'delete' print 'Deleting...' end select loop end Procedure (add, del, exit)? add Adding... Procedure (add, del, exit)? del Deleting... Procedure (add, del, exit)? exit |
Example 10-26 CASE Statement - Checking for a Range of Values |
---|
a = 5 select case a case 1 : print 'one' case 2 to 6 : print 'range' case else : print 'else' end select b$ = 'c' select case b$ case 'a' : print 'a' case 'b' to 'e' : print 'range' case else : print 'else' end select end range range |
The CASE ELSE option executes a block of code only if none of the CASE expressions match. SELECT CASE with the CASE ELSE option looks like this:
SELECT CASE main expr [CASE expr1, expr2... --- --- block of code --- ...] [CASE IS {numeric operator| boolean operator} expr3 --- --- block of code --- ...] CASE ELSE --- --- block of code --- END SELECT |
CASE ELSE must follow the last CASE statement. If none of the CASE expressions match, Sheerpower executes the block of code following the CASE ELSE statement.
Example 10-27 CASE ELSE Statement |
---|
do input 'Procedure (add, del, exit)' : pro$ if _exit then exit do pro$ = ucase$(pro$) select case pro$ case 'ADD' print 'Adding...' case 'DEL', 'DELETE' print 'Deleting...' case else message error: 'Procedure must be: add, del or exit' repeat do end select loop end Procedure (add, del, exit)? add Adding... Procedure (add, del, exit)? del Deleting... Procedure (add, del, exit)? funny Procedure must be add, del, or exit Procedure (add, del, exit)? EXIT |
CASE IS is used to form a conditional expression to be checked against the main expression. The format of the CASE IS option is:
CASE IS {relational operator} expr |
When the CASE IS statement executes, Sheerpower compares expr to the main_expr using the relational operator.
Example 10-28 CASE IS Statement |
---|
do input 'Your income per year': income if _back or _exit then exit do select case income case 0 print 'No income?' case is < 0 print 'A negative income? You are in debt!' case is > 0 print 'A positive income.' end select loop end Your income per year? -15000 A negative income? You are in debt! Your income per year? 0 No income? Your income per year? 25000 A positive income. Your income per year? exit |
CHAIN 'file_spec' |
Example 10-29 CHAIN Statement |
---|
line input 'Your name (last, first)': name$ open #1: name 'storage.txt', access output print #1: name$ close #1 input 'Add to CLIENT structure (Y/N)': reply$ if reply$ = 'Y' then chain 'ADD' end Your name (last, first)? Woods, Jack Add to CLIENT structure (Y/N)? N |
CHAIN exits the current program and runs the Sheerpower program specified.
file_spec is the specification for the program being chained to. The file specification can be any string expression. Sheerpower searches for the file specified, then exits the current program and runs the program named. Control does not return to the current program when the chained program is finished.
When Sheerpower executes the CHAIN statement, it:
To run other types of programs such as .EXE, use:
pass noreturn: string_expr |
Where string_expr is a string containing the name of the non-Sheerpower program to run. See Section 10.8.1.2, PASS NORETURN.
Previous | Next | Contents | Index |