|
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. Because conditionals control the flow of logic, clarity is essential. 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 ELSEIF to create more complex
logic.
In Sheerpower, a condition inside an `IF` must evaluate to
exactly TRUE (1) or FALSE (0).
There is no implicit truthiness. 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 one value against
many possibilities, a long IF/ELSEIF 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 expressive than the
switch/case statements found in many languages,
making code easier to read and maintain.
SELECT CASE Works
SELECT 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.
When each CASE needs to perform just one simple action, Sheerpower
supports a compact form using a colon (:) on the CASE
line. This is functionally identical to the block form,
but it reads like a clean mapping table.
The compact form is ideal for small dispatch logic, menu handlers, and readable state messages.
Problem: A full block for each CASE can feel heavy when each case only needs a single statement.
Solution: Use CASE expr : statement
to keep simple CASE logic compact and obvious.
Efficiency: This is purely a readability feature. It does not change semantics or performance. Only the first matching CASE executes, and there is still no fall-through.
Takeaway: Use the compact CASE form for one statement per case. When a case grows beyond one statement, switch back to the block form for clarity.
If a CASE needs more than one statement, prefer the standard block layout:
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 TRUEThis pattern allows complex conditional logic without deep nesting.
Since the SELECT CASE construct accepts any expression,
you can use SELECT CASE TRUE as a clear alternative
to a chain of IF/ELSEIF statements. Each CASE
contains a Boolean expression. When that expression evaluates to
TRUE, its block executes.
This pattern preserves the "first match only" behavior while avoiding deeply nested conditional blocks.
SELECT CASE is more expressive than
traditional switch/case constructs. 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.ELSEIF 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.ELSEIF — 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/ELSEIF 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. |