Appendix C: Conditions
Rapid-Q Documentation by William Yu (c)1999 |
Appendix C: Conditions |
|
Introduction to Condition Statements
Conditional statements offer the ability to execute code for which
the condition is satisfied. This is probably one of the first things
you learn in programming, besides the usual.
IF .. THEN .. ELSE
There are 2 ways to use IF .. THEN .. ELSE. You can place your statement
on one single line like so:
IF I > 100 THEN PRINT "I is greater than 100"
Or split it up like so:
IF I > 100 THEN
PRINT "I is greater than 100"
END IF
As you can see, I > 100 is a condition, as long as this is satisfied, the code
gets executed, if not then that piece of code is skipped.
A condition is satisfied whenever the expression evaluates to any non-zero number.
Which also means that this is valid:
I = 100
IF I THEN
PRINT "Condition satisfied"
ELSE
PRINT "Condition is not satisfied"
END IF
Use ELSEIF whenever you have multiple conditions that you want satisfied:
IF I > 10 AND I < 20 THEN
'' do stuff
ELSEIF I > 55 THEN
'' do stuff
ELSE
'' do stuff
END IF
Sometimes it helps to use SELECT CASE instead of all these ELSEIFs, which we'll
cover next.
SELECT CASE .. END SELECT
Select cases are usually implemented when you have a lot of conditions that you
want to test for. It's really not useful if you only have 2 or 3 conditions,
but no one's stopping you from using it. There are no special cases in using
SELECT CASEs in Rapid-Q, so everything you learned in QBasic, can be implemented
here as well.
SELECT CASE Expression
CASE 1
'' Do stuff
CASE 5
'' Do stuff
CASE ELSE
'' No conditions satisfied, do stuff
END SELECT
It probably involves less typing, but there are many other useful purposes,
for example, range testing becomes so much easier:
SELECT CASE Expression
CASE 1 TO 4, 10 TO 20
'' Do stuff
CASE 100 TO 150
'' Do stuff
CASE IS > 200
'' Do stuff
END SELECT
As you can see, you can have multiple test conditions by separating each with a comma.
I think you can figure out how the IS keyword is used.
|