Game Development Reference
In-Depth Information
Conditional statements, which are essentially the programmed form of
Boolean algebra, cannot operate on their own. They need constructs
around them to assess their value. The simplest form of these is an if-else
statement .
Used quite a lot in simple artificial intelligence programming, if-else
statements make an assessment of a statement and do one thing if that
statement is true and another (or nothing) if it is false. The if-else statement
was used in the logic circuit application mentioned earlier in the chapter.
Think of it as the type of logic you might use in deciding whether or not
to wear a raincoat, thus:
if it is raining
wear a raincoat
otherwise
don't wear a raincoat
The if-else statement in JavaScript looks like that in Listing 1.14 .
Listing 1.14 An if-else Statement
if (test)
{
//if the value of test is true do this bit;
}
else
{
//if the value of test is false do this bit;
}
For example, consider the script in Listing 1.15 .
Listing 1.15 An Example Script Using an if-else Statement
var x:int = 5;
var y:int = 10;
if( x > y)
{
print(“X is greater than Y”);
}
else
{
print(“Y is greater than or equal to X”);
}