THEN: Your IF Statement's Right-Hand Man
Ever wondered how your Commodore 64 knows what to do when a condition is met in an IF
statement? That's where THEN
comes in, the trusty sidekick to IF
! While not a standalone command, this keyword acts as the bridge between your logical condition and the action you want your C64 to take if the condition is true. Think of it as the "go" signal for your code!
Syntax
IF <condition> THEN <statement>
Where:
- <condition>
: A logical expression that evaluates to either true
(-1 in C64 BASIC) or false
(0).
- <statement>
: The line of code (or multiple lines separated by colons) to execute if the condition is true.
Applications
The THEN
keyword is the key to:
- Conditional Execution: Make your programs respond intelligently to different situations by choosing specific actions based on the truth value of a condition.
- Branching Logic: Create different paths of execution in your code, allowing for personalized responses, game logic, or decision-making processes.
- Data Validation: Ensure that user input or other data meets certain criteria before proceeding.
- Error Handling: Trigger specific actions or messages when errors occur in your program.
Code Examples
1. Simple IF...THEN Statement:
10 INPUT "Enter a number: "; N
20 IF N > 0 THEN PRINT "Positive number"
This example prints "Positive number" only if the input N
is greater than zero.
2. Multiple Statements with THEN:
10 INPUT "Enter your name: "; N$
20 IF LEN(N$) > 0 THEN PRINT "Hello, "; N$; "!": GOTO 40
30 PRINT "You didn't enter a name."
40 REM Continue with the rest of the program
Here, THEN
is followed by multiple statements separated by colons. If the condition is true (the name is not empty), both statements after THEN
are executed.
THEN in the Wild: The Interactive Storyteller
Imagine you're creating a text-based adventure game. The IF...THEN
statement, powered by the THEN
keyword, would be your storytelling engine, allowing players to make choices that determine the course of the narrative.
Don't let your code be a one-trick pony! With the THEN
keyword by your side, you can transform your Commodore 64 programs into dynamic, responsive, and interactive experiences. It's like having a built-in decision-maker that can guide your code down different paths based on the situation. So embrace the power of THEN
and let your imagination run wild!
Key Points to Remember:
THEN
is always used in conjunction withIF
. It cannot be used on its own.- You can have multiple statements following
THEN
, separated by colons. - If you need to execute alternative actions when a condition is not met, use the
ELSE
keyword along withIF...THEN
.