Tutorial Details:
Difficulty Level:
Intermediate
Topics Covered: Using
the If statement to make decisions.
Assumed Knowledge:
The Basics, My First Program
Written By: BILL LANE
Print Version
If you've ever programmmed in any other programming language
then you will have probably encountered if.
It is one of the most fundamental programming structure. Without
it a program would be unable to respond to an event in a useful
manner. So for those who've never seen if
before let's have a look at it. If
is all about asking a question and making a decision based
on the answer. The main requirement is that the answer to
the question can be either TRUE
or FALSE. For example,
we may have created a variable to keep track of the current
score (gv_GameScore) and we'd like to test to see if the Spybot
has achieved enough points to win. We would write something
like this:
If gv_GameScore = 6 {
/* commands */ }
With commands being statements to execute if there are enough
points. In place of =
we could also have tested for <
(less than), > (greater
than) or <> (not
equal to). We can also test for a range of values in the following
manner:
If gv_GameScore is 1..6 {
/* commands */ }
Note the two full stops between the two numbers. If gv_GameScore
is in this range of values the commands will be executed.
We could also have used is not
to check if the score isn't in that range. Finally
we could use an else at
the end of the if block to provide commands to execute if
the the statement is false:
If gv_GameScore = 6 {
/* commands */ } Else {
/* commands */ }
Therefore, in this case if the score isn't equal to 6 then
the Else commands will
be executed. That should be all you need to know about the
if statement. It's simple
enough. But it is a very powerful tool for making your missions
more engaging.
|