Welcome back! Last chapter, we talked about conveyors—the logic that moves data around and makes decisions. But we didn't really dig into how those decisions work. Time to fix that!
Picture a railroad yard. Trains come rumbling in, and they need to go to different destinations. Coal trains go to the power plant. Passenger trains go to the station. Freight trains go to the warehouse.
But trains can't decide where to go on their own. They just roll forward. So how do they end up in the right place?
Switches.
A railroad switch is a simple mechanism. When a train approaches, someone (or something) asks a question: "Is this a coal train?" If yes, flip the switch one way. If no, flip it the other way. The train rolls through, and it ends up on the right track—all because someone asked a question.
Your code works the exact same way.
The IF Statement: Your First Switch
The most basic switch in Crash BASIC is the IF statement. It asks a question, and if the answer is "yes" (or TRUE, in computer-speak), it does something.
age = 16
IF age >= 16 THEN PRINT "You can drive!"
PAUSELet's break this down:
age >= 16is the question (Is age greater than or equal to 16?)THENis the switch (If the answer is yes, do this next part)PRINT "You can drive!"is what happens if the answer is yes
If age was 14, the question would be FALSE, and the PRINT would never happen. The train just rolls past that switch without turning.
Questions You Can Ask
Before we go further, let's talk about the kinds of questions you can ask. These are called comparison operators, and they're how you compare two things:
| Operator | Meaning | Example |
|---|---|---|
= |
Equal to | score = 100 |
<> |
Not equal to | name$ <> "Crash-tan" |
< |
Less than | health < 0 |
> |
Greater than | speed > 10 |
<= |
Less than or equal to | lives <= 0 |
>= |
Greater than or equal to | age >= 18 |
Each of these questions has only two possible answers: TRUE or FALSE. Yes or no. That's it!
PRINT 5 > 3 ' Prints: TRUE
PRINT 5 < 3 ' Prints: FALSE
PRINT 5 = 5 ' Prints: TRUE
PRINT 5 <> 5 ' Prints: FALSE
PAUSETwo-Way Switches: IF/THEN/ELSE
Sometimes you want the train to go one way if the answer is yes, and a different way if the answer is no. That's what ELSE is for!
health = 0
IF health > 0 THEN
PRINT "You're still alive!"
ELSE
PRINT "Game Over!"
END IF
PAUSESee that structure? When you need multiple lines, you use:
IF ... THENto start- Your "yes" code
ELSEfor the alternative- Your "no" code
END IFto close it out
Think of it like a fork in the railroad. The train HAS to go somewhere—either left or right. There's no stopping in the middle.
playerX = 160
IF playerX < 160 THEN
PRINT "You're on the left side!"
ELSE
PRINT "You're on the right side!"
END IF
PAUSEChaining Switches: ELSEIF
What if you have more than two tracks? What if you need to sort trains into five different destinations?
You chain switches together with ELSEIF!
score = 85
IF score >= 90 THEN
PRINT "Grade: A"
ELSEIF score >= 80 THEN
PRINT "Grade: B"
ELSEIF score >= 70 THEN
PRINT "Grade: C"
ELSEIF score >= 60 THEN
PRINT "Grade: D"
ELSE
PRINT "Grade: F"
END IF
PAUSEThe train (your score) comes down the track. First switch: "Is score >= 90?" No, 85 is not >= 90, so keep rolling. Second switch: "Is score >= 80?" Yes! 85 IS >= 80, so we take that track and print "Grade: B".
Once a train takes a track, it's done—it doesn't check the other switches. This matters! If we'd asked "Is score >= 60?" first, that would've been TRUE, and we'd have printed "Grade: D" even though the score deserved a B.
Order matters. Put your most specific questions first.
Asking Multiple Questions: AND, OR, NOT
Sometimes one question isn't enough. You need to ask TWO questions at once.
AND: Both Must Be True
age = 25
hasLicense = TRUE
IF age >= 16 AND hasLicense = TRUE THEN
PRINT "You can drive!"
END IF
PAUSEThe train only takes this track if BOTH questions are TRUE. Age must be >= 16 AND they must have a license. If either one is FALSE, the whole thing is FALSE.
Think of it like two switches in a row—the train has to get through both to reach the destination.
OR: Either Can Be True
day$ = "Saturday"
IF day$ = "Saturday" OR day$ = "Sunday" THEN
PRINT "It's the weekend!"
END IF
PAUSEWith OR, the train takes the track if EITHER question is TRUE. Saturday? Weekend! Sunday? Also weekend! Monday? Nope, keep rolling.
NOT: Flip the Answer
isGameOver = FALSE
IF NOT isGameOver THEN
PRINT "Keep playing!"
END IF
PAUSENOT flips TRUE to FALSE and FALSE to TRUE. So NOT isGameOver asks "Is it NOT game over?" If isGameOver is FALSE, then NOT FALSE is TRUE, so we keep playing!
This one twists your brain a little at first. That's normal. You'll get used to it!
Switches in Action: A Real Example
Let's put it all together! Here's a little game where you dodge left or right:
SCREEN 13
playerX = 160
enemyX = 160
enemyY = 0
score = 0
DO
' --- CONVEYORS: Process the logic ---
k$ = INKEY$
IF k$ = "a" AND playerX > 10 THEN
playerX = playerX - 5
END IF
IF k$ = "d" AND playerX < 310 THEN
playerX = playerX + 5
END IF
' Move enemy down
enemyY = enemyY + 2
' Did enemy reach the bottom?
IF enemyY > 200 THEN
enemyY = 0
enemyX = INT(RND * 300) + 10
score = score + 1
END IF
' Did enemy hit player?
IF enemyY > 180 AND enemyY < 200 THEN
IF enemyX > playerX - 15 AND enemyX < playerX + 15 THEN
PRINT "Game Over! Score: "; score
END
END IF
END IF
' --- MAGICIANS: Create the illusion ---
BEGIN FRAME
CLS
CIRCLE (playerX, 190), 10, 14 ' Player (yellow)
CIRCLE (enemyX, enemyY), 8, 4 ' Enemy (red)
LOCATE 1, 1: PRINT "Score: "; score
END FRAME
DELAY 16
LOOPLook at all those switches!
IF k$ = "a" AND playerX > 10— Only move left if pressing A and not at the edgeIF enemyY > 200— Reset enemy when it goes off screenIF enemyY > 180 AND enemyY < 200— Only check collision when enemy is near playerIF enemyX > playerX - 15 AND enemyX < playerX + 15— Check if enemy overlaps player
Each switch routes the logic down a different track. Together, they create a game!
Putting It All Together
Switches (IF statements) are how your program makes decisions:
- IF/THEN — Do something when a condition is true
- IF/THEN/ELSE — Do one thing or another
- ELSEIF — Chain multiple conditions together
- AND — Both conditions must be true
- OR — Either condition can be true
- NOT — Flip true to false (and vice versa)
Your conveyors from Chapter 1? They're made of switches. Every decision in your game—"Should the player jump? Did we hit an enemy? Is the game over?"—is just a switch asking a question and routing the train down the right track.
Try It Yourself!
Take the dodge game above and modify it:
- Add lives instead of instant game over
- Make the enemy speed up as the score increases
- Add a second enemy
- Make the player change color when they're in danger
Every change you make will involve adding or modifying switches. That's the power of logic!
See you in Chapter 3!
Next Chapter: We'll make things repeat with loops—because sometimes you need the train to go around in circles!