Here is the next installment, with this you start learning some important stuff so make sure you understand... everything in game programming (or any programming) revolves around these next few chapters.
PART 5: Selection Statements
Selection Statements are used to make programs have differing outcomes, instead to executing the same way every time they are run. A selection statement checks an expression, sees if it is True or False, then if True executes the rest of the statement otherwise it skips to below the statement and continues with execution.
There are two types of selection statements on the calculator, but one can only be used on models before the AFX series. They are the If/Else statement and the => arrow.
An If/Else statement works like this
1->A
If A=1 //expression to be evaluated
Then ?HI? //result if expression is true
?HOW ARE YOU?
Else ?BYE? //result if expression is false
?SEE YOU LATER?
If End //end of statement
Result:
Since A is 1 the statement evaluates to true, therefore
HI
HOW ARE YOU
Is printed. If you replaced the first line with 0->A then the if statement would evaluate to False, and
BYE
SEE YOU LATER
Would be printed.
An If statement can contain many different things, and can be many lines long, they are the keystone to making a game, and before going on you should feel comfortable using them.
The => arrow is also very useful, it is a single line selection statement that takes up less space but can do less than a normal if/else.
This is the same code as above, except using the => arrow instead of the if statement.
1->A
A=1=>?HI?
A=1=>?HOW ARE YOU?
A<>1=>?BYE? // <> means not equal to
A<>1=>?SEE YOU LATER?
Neat huh? As you can see, the => arrow provides cleaner looking code, and occasionally takes up less space than the if statement. In general, the best time to use a if statement is when you have at least to lines of code to be put inside, otherwise use the => arrow.
PART 6: Advanced Loops
Now that you have a good understanding of both loops and selection statements I will go into the more advanced features of both.
First off we?ll talk about nesting. Nesting is when you take one loop and put it inside another. Here is an example:
0->C
For 1->A To 10 //Step can be omitted instead of using Step 1
For 1->B To 10
A+B+C->C
Next
Next
C_ // the _ represents the output sign, it displays whatever is before it and
Pauses until [EXE] is pressed.
By executing this code you will get this output:
1100
Disp //caused by _
Now we will go through the code and look at what each line does.
Line 1: 0->C
Assigns the value 0 to the variable C
Line 2: For 1->A To 10
Tells the program that you will be looping until A is equal to 10, adding 1 with each loop.
Line 3: For 1->B To 10
Tells the program that you will be looping until B is equal to 10, adding 1 with each loop.
Line 4: A+B+C->C
The current value in A will be added to the current value of B, that value is then added to the current value of C and then assigned to C.
Line 5: Next
Goes to line 3.
Line 6: Next
Goes to line 2.
Line 7: C_
Displays the final value of C.
As you can see, the second loop executes completely for each iteration (loop) of the first loop.
Nesting also applies to selection statements, you can nest if or => inside each other as needed.
Example:
A<1=>A>0=>?HI?
If A<1
Then if A>0
Then ?HI?
End If
End If
This brings us to another point:
Part 7: Logical Operators
As you have just seen, there are times when you will want more than one condition in a selection or loop, you can nest the statements or you can use a logical operator: And, Or, Not.
And and Or are operators which can be used in selection statements to specify additional conditions, therefore the code above could be written:
A<1 and A>0=>?HI?
Instead of:
A<1=>A>0=>?HI?
The Or operator allows you to specify alternate conditions which are evaluated independently and if any are true then the entire statement is considered true. This means you can do this:
If A=1 Or B=1
Then ?TRUE?
End If
When this executes it will print TRUE if A or B equals 1. Handy huh?
The Not operator can also be used in selection statements, though it is not used nearly as often. Not returns False if the statement is true (or 1) and True if the statement is False (or 0) so:
If Not (A=1)
Then ?TRUE?
End If
This will print true if A is any number but 1. In this case the Not statement is used as an <> (not equal to) statement, but it has other uses. We will cover more on this later.