Sheerpower Logo

Conditionals -- IF/THEN/ELSE/ELSEIF, SELECT CASE


Making Decisions with Conditionals

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.

Tip: Clear conditionals make code easier to read and maintain. Messy or deeply nested ones often lead to bugs, because they control the program’s flow of logic. Always write conditionals so their purpose is obvious — future you (and other programmers) will thank you.

1. The Simple `IF/THEN` Statement

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.

health = 9 if health < 10 then print "Health is critical! Use a potion!" end if

Because `health` is less than 10, the message is printed. If you changed `health` to 25, nothing would be printed.

2. Adding an `ELSE` for the Other Path

What if the condition is false? An ELSE statement lets you run a different block of code in that case.

health = 55 if health < 10 then print "Health is critical! Use a potion!" else print "You are healthy enough to continue." end if

Flowchart: How `IF/ELSE` Works

Start
health < 10?
→ TRUE
Print "Critical!"
→ FALSE
Print "Healthy."
End

3. Handling Multiple Conditions with `ELSE IF`

Sometimes you have more than two possibilities. You can chain conditions together using ELSE IF to create more complex logic.

health = 45 if health < 10 then print "Health is critical! Use a potion!" else if health < 50 then print "Warning: Your health is getting low." else print "You are in good shape." end if

Important Sheerpower Rule!

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
if x then ...
(This will fail)

Correct: x = 5
if x <> 0 then ...
(This works because x <> 0 evaluates to `true`)

4. Handling Many States with 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.

// Using IF/ELSE IF IF score >= 90 THEN grade$ = "A" ELSE IF score >= 80 THEN grade$ = "B" ELSE IF score >= 70 THEN grade$ = "C" ELSE grade$ = "F" END IF
// Using SELECT CASE SELECT CASE score CASE is >= 90 : grade$ = "A" CASE is >= 80 : grade$ = "B" CASE is >= 70 : grade$ = "C" CASE ELSE : grade$ = "F" END SELECT

Flowchart: How SELECT CASE Works

Start
Evaluate main_expr
→ "Poisoned"
Print "Taking damage"
→ 2 TO 6
Print "Range matched"
→ ELSE
Print "No match"
End

Format

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
Important: A 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.

Key Features

  • Main expression: Any valid expression, not just a variable.
  • Multiple matches: Each CASE may hold multiple expressions and ranges, separated by commas.
  • Ranges: Use expr1 TO expr2 to cover numeric or string ranges.
  • CASE IS: Compare with a relational operator. All standard operators are supported: <, <=, =, >, >=.
  • CASE ELSE: Executes only if no other case matches.
  • Type safety: All expressions must be the same data type as main_expr. Mismatched types raise an exception.

Examples

// Example 1: Multiple expressions per CASE 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 print "Procedure must be: add, del or exit" end select loop end
// Example 2: Range checking 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 // Output: range / range
// Example 3: CASE IS with relational operators do input "Your income per year": income if _back or _exit then exit do select case income case is < 0 print "A negative income? You are in debt!" case 0 print "No income?" case is >= 100000 print "Wow, high income!" case is > 0 print "A positive income." end select loop end
// Example 4: First match only (numeric) x = 5 select case x case 1 to 10 print "Matched range 1 to 10" case 5 print "Matched exactly 5" case else print "No match" end select // Output: Matched range 1 to 10 // Example 5: First match only (string) letter$ = "c" select case letter$ case "a" to "f" print "Matched range a to f" case "c" print "Matched exactly c" case else print "No match" end select // Output: Matched range a to f
Best Practice: Order your 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.

❌ Bad Order

x = 5 select case x case 1 to 10 print "Matched range 1 to 10" case 5 print "Matched exactly 5" end select // Output: Matched range 1 to 10

✅ Good Order

x = 5 select case x case 5 print "Matched exactly 5" case 1 to 10 print "Matched range 1 to 10" end select // Output: Matched exactly 5

Advanced Pattern: 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.

len = 12 a = 5 b = 10 select case true case a > b print "greater" case len = 12 print "len was 12" end select
Takeaway:
Sheerpower’s SELECT CASE is far more powerful than a simple switch. It supports:
  • Any expression — not just a single variable.
  • Multiple values per CASE, separated by commas.
  • Ranges with expr1 TO expr2, for numbers and strings.
  • Relational tests via CASE IS with <, <=, =, >, >=.
  • Boolean-driven patterns using SELECT CASE TRUE.
  • No fall-through — only the first match executes.
  • Type safety — all expressions must match the type of the main expression.
Each 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.

Performance Note: In Sheerpower, comparisons — whether in 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.
Summary: You now know how to control the flow of your program!
  • Use IF for a single condition.
  • Add ELSE to handle the "false" path.
  • Use ELSE IF for multiple, related conditions.
  • Use SELECT CASE for checking one main value against many different values. It's cleaner and more readable.
(Show/Hide Sheerpower Conditionals Takeaways)
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.