Tutorial: If Statements
This tutorial will introduce you to If Statements.
What is an If Statement?
An If statement checks a boolean expression to decide whether or not to run code.
To learn about boolean expressions, see our Boolean expressions tutorial.
Single Line If Statements
The format for a single line If statement is :
If booleanExpression Then doSomething
Example :
yourName$ = "Bob"
If yourName$ = "Bob" Then Print "Hello Bob."
Sync
WaitKey
Multi-Line If Statements
All multi-line If statements begin with If and end with EndIf.
The basic format for a multi-line If statement looks like :
If condition
; code
; code
EndIf
Example :
playerName$ = "Bob"
If playerName$ = "Bob"
Print "Hello, Bob."
Print "Nice to see you."
EndIF
Sync
WaitKey
If Else Statements
An If Else statement is a multi-line If statement.
If the boolean expression is true, it executes the code after If.
If the boolean expression is not true, it executes the code after Else.
health = 100
If health > 0
Print "You have " + Str$(health) + " health."
Else
Print "Thou art dead."
EndIf
Sync
WaitKey
If ElseIf Statements
If-ElseIf statements combine two (or more) If statements.
health = 100
If health > 50
Print "You're doing pretty good."
ElseIf health > 0
Print "You're feelin' kinda weak."
EndIf
Sync
WaitKey
More than one ElseIf can be used.
health = 100
If health > 50
Print "You're doing pretty good."
ElseIf health > 0
Print "You're feelin' kinda weak."
ElseIf health = 0
Print "Thou art dead."
EndIf
Sync
WaitKey
If ElseIf Else Statements
You can guess how this works. :)
health = 100
If health > 50
Print "You're doing pretty good."
ElseIf health > 0
Print "You're feelin' kinda weak."
Else
Print "Thou art dead."
EndIf
Sync
WaitKey
Next Steps
| Categories: Beginner Tutorials : Programming Tutorials : Tutorials |