![]() |
Conditionals -- IF/THEN/ELSE/ELSEIF, SELECT CASE |
So far, our programs have run from top to bottom. But what if we want our code to make choices? That's where conditionals come in. They allow your program to ask questions and perform different actions based on the answers. Since conditionals control the flow of logic, their clarity of implementation is very important. Let's learn how by checking the status of a video game character.
The most basic conditional is the IF/THEN
statement. It
checks if a condition is true, and if it is, it runs a block of
code. Let's check if our hero's health is critically low.
Because `health` is less than 10, the message is printed. If you changed `health` to 25, nothing would be printed.
What if the condition is false? An ELSE
statement lets
you run a different block of code in that case.
Sometimes you have more than two possibilities. You can chain
conditions together using ELSE IF
to create more complex
logic.
In Sheerpower, a condition inside an `IF` must evaluate to
exactly true
(1) or false
(0). Unlike
some languages, you can't use just any non-zero number.
Wrong: x = 5
(This will fail)
if x then ...
Correct: x = 5
(This works because
if x <> 0 then ...x <> 0
evaluates to `true`)
SELECT CASE
When you need to check a single condition against
many possibilities, a long IF/ELSE IF
chain can quickly
become hard to follow. The SELECT CASE
statement offers a cleaner
and more efficient way to handle this. In Sheerpower,
SELECT CASE
is more flexible than the
switch/case
statements found in many other languages,
making code easier to read and maintain.
SELECT CASE
WorksSELECT CASE main_expr CASE expr1[, expr2,...] block of code [CASE expr3[, expr4,...] block of code ...] [CASE IS {relational operator} exprN block of code ...] [CASE ELSE block of code ...] END SELECT
CASE
block extends from its
CASE
line down to the next
CASE
, CASE ELSE
,
or END SELECT
.
There is no END CASE
.
Only the first matching CASE
executes —
there is no fall-through.
CASE
may hold
multiple expressions and ranges, separated by commas.expr1 TO expr2
to cover
numeric or string ranges.<
, <=
, =
,
>
, >=
.main_expr
. Mismatched types raise an exception.CASE
statements from
most specific to most general.
Since Sheerpower stops at the first match, a broad
range (like 1 TO 10
) will prevent later,
more specific cases (like 5
) from ever running.
Always place exact matches or narrower conditions before
wider ranges.
SELECT CASE TRUE
Since the SELECT CASE
construct accepts any expression,
you can use SELECT CASE TRUE
as a powerful alternative
to a chain of IF/ELSE IF
statements. Each CASE
then holds a Boolean expression. When that expression evaluates to
TRUE
, its block executes.
SELECT CASE
is far more powerful than a
simple switch. It supports:
CASE
, separated by commas.expr1 TO expr2
, for numbers and strings.CASE IS
with <, <=, =, >, >=.SELECT CASE TRUE
.CASE
block runs until the next CASE
(or CASE ELSE
/ END SELECT
), and only the
first matching case is executed. This combination makes
SELECT CASE
both safer and more expressive than
traditional switch/case statements in other languages.
IF
,
CASE
, or any other expression — run at extremely
high speed, millions per second. The order of CASE
statements only affects which block executes first; the timing
difference is so small (a few billionths of a second) that it will rarely
impact performance. Prioritize clarity over micro-optimization —
conditionals are almost never the bottleneck.
IF
for a single condition.ELSE
to handle the "false" path.ELSE IF
for multiple, related conditions.SELECT CASE
for checking one main value
against many different values. It's cleaner and more readable.
IF/THEN
— run code only if a condition is true.ELSE
— provide an alternate path when the condition is false.ELSE IF
— chain conditions together for more complex branching.true
(1) or false
(0); you can’t rely on “non-zero is true.”SELECT CASE
— cleaner alternative to long IF/ELSE IF
chains.SELECT CASE TRUE
), type safety, and no fall-through.
Hide Description
|
|
Enter or modify the code below, and then click on RUN |
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |