| Previous | Contents | Index |
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
|
EXIT DO
|
| 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
|
EXIT DO is used to exit from a DO loop.
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.
REPEAT DO
|
| 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'
|
REPEAT DO is used to repeat part of a DO loop.
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
|
ITERATE DO
|
| 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
|
ITERATE DO is used to repeat a loop, skipping part of the loop.
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
|
EXECUTE str_expr
|
| 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 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
|
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.
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.).
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 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 |