| Previous | Contents | Index |
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.
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.
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 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
|
| Previous | Next | Contents | Index |