How To Cog - Chapter 2.5 - Conditions

If Statement

The if statement allows you to check something in your cog or in the game and execute code based on the result. Suppose that you want to make sure your player is alive before letting him fire his weapon. For example:

fire:
	if(GetThingHealth(player) < 1) // end firing and return.

	// continue with firing.

Return;

GetThingHealth() is a verb that returns the value of a thing's health. If this value is less than 1, then we can assume that the player has no health and is dead. JK will evaluate this less-than operation and return true if the player's health is less than 1 or false if it's not. When the if statement has a true value, it runs the following line or block of code. Here we have a comment to explain what code would be there.

In some cases you'll need to run code only if the condition was false or you may need to check a few more conditions. To do this, you chain if statements together to look like this:

startup:
	if(var1 == var2) // do this
	else if(var1 == var3) // do this again
	else if(var1 == var4) // do this again
	else if(var1 == var5) // do this again
	else if(var1 == var6) // do this again
	else // do that.
Return;

Or you can use one if statement and the else keyword.

startup:
	if(var1 == var2) // do this
	else // do that.
Return;

The code after an else statement will only run if the last if statement had a false value. In this next example, if statements are used with code blocks:

if(var1 == 1)
{
	// code line 1
	// code line 2
}
else if(var1 == 2)
{
	// code line 1
	// code line 2
}
else
{
	// code line 1
	// code line 2
}

It makes no difference whether you have a single statement or a code block after a conditional statement.

Conditions

Condition is a word used to refer to the expression used by conditional statements. For the if statement, the condition is enclosed by a set of parentheses. This expression can be a lot more complicated than in the examples you've seen.

A value of 1 is a true value, and a value of 0 is a false value. There is a variable type specifically for this kind of value called boolean. Cog does not have this type, but because it is a programming standard, a true or false value can be referred to as a boolean value. All conditions must be evaluated to either true or false.

Most conditions are simple relational tests - one value or variable is compared to another. For example:

if(var1 == 1) // go!
else if(var1 <= 1) // go!
else if(var1 >= 1) // go!
else if(var1 < 1) // go!
else if(var1 > 1) // go!

These are all relational tests. This example shows how a condition is evaluated:

if(var1 == 1) // go!
if(1 == 1) // go!
if(1) // go!

Since 1 is a true value, we will go! If var1 was 5:

if(var1 == 1) // go!
if(5 == 1) // go!
if(0) // go!

Because 5 is not equal to 1, the result was 0. Because 0 is a false value, the following code wasn't executed. Had there been an else statement, the else statement's code would have been executed.

Logical Operators

These operators are used to create more complex conditions. A relational test is just a simple comparision, but one conditional statement can combine many relational tests.

The NOT Operator

The "not" operator is symbolized by an exclamation mark. It's purpose is to reverse the value of a condition. An example:

  var=5;
  if(!var < 1) // Do something.

Five is not less than one, so the relational test is false. But, the not operator reverses the value to true. In this example:

  var=0;
  if(!var) // Run code

Var has a value of zero. And since zero is false, the condition would normally be false and would not run the code. But the not operator changes the value to 1, and the code is run. The not operator must be placed at the beginning of the relational test - either inside or outside a pair of parentheses. Here are a few more examples that are all true:

if(!(0)) // Do stuff
if(!0) // Do stuff
if(!(5 < 5)) // Run code
if(!7 == 6)
{
   // Run code
}

The AND Operator

The "and" operator, symbolized by two ampersands ( '&&' ), is used to combine two relational tests into one condition. For example:

  if(var1 < 5 && var2 < 10) // Do something

The logical "and" operator requires that both relational tests be true for the entire condition to be true. The seperate relational tests do not have to be enclosed by parentheses, but many programmers choose to use them to make their code easier to read. Another reason might be to force the relational tests to be performed first (parentheses have a high precedence).

Because Logical Operators have a lower precedence than Relational Operators, the relational tests are performed first, and then the "and" operations are evaluated. This is why you don't have to enclose relational tests in parentheses to force them to be evaluated first. If you did enclose the seperate tests, it would look like this:

  if((var1 < 5) && (var2 < 10)) // Run code

When the relational tests in a condition are evaulated, the boolean result of the test is found. For example:

  if(1 && 0 && 1 && 1 && 1) // Run code

In that example, 1 out of five relational tests was false. Because all tests had to be true for the entire condition to be true, the condition was false and the code was not run. Here are two more examples:

  if(var1 == 5 && var2 == 3 && var3 == 0) // Run code
  if(!var1 && var2 && var3 == 10) // Run code

The OR Operator

The "or" operator is symbolized with two braces: ' || '. This operator will make a condition true if either of two relational tests is true. For example:

  if(var1 < 3 || var2 == 10) // Run code

If either relational test is true, then the condition would be true and the code would be run. As with the "and" operator, the relational tests are usually enclosed by parentheses, but don't have to be.


Previous: Cog Code Up: How To Cog Next: Loops
  • Create:
This page was last modified 15:46, 14 April 2006.   This page has been accessed 1,207 times.