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)
/* command 1*/; /* command
2*/;
With commands being statements to execute if there are enough
points. I have shown two commands here but there may be more.
In place of == we could
also have tested for <
(less than), > (greater
than) or <= (less than
or equal to) or >=
(greater than or equal to ) or !=
( not equal to ). We could also use an optional else
at the end of the if block to provide commands to execute
if the the statement is false:
If (gv_GameScore == 6)
/* command 1*/; else /*
else command*/;
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.
|