if(var == 1) { // Do something }The conditional statement is read as: "if var is equal to one, then do something." In the example, var is a variable to be tested, '==' is the relation to be tested for, and 1 is the number that will be tested with var for the relation. If var had a value of 1, then the condition would be true, and the action inside the brackets would be performed. If var had not been equal to one, then the condition would be false and the code inside the brackets would be ignored.
Notice that in the above example, the if statement ran a code block (the code inside a pair of brackets) when the condition was true. The if statement can run a single statement or a code block. EG:
if(var == 1) // Do somethingConditional statements do not end with a semicolon like other statements. Putting a semicolon on the end of a conditional statement will end the statement. Then the code after the statement will run regardless of the condition because it is not associated with it.
You may use anything that has a value in a relational test. This includes cog verbs that return a value, arrays, variables, numbers, relational tests, models, cogs, etc. You can even use mathatical expressions like 1+1. Although all of the examples in this text use numbers and variables with integer values, keep in mind that that's not all you can use.
The if statement will be used for all of the examples in this text. Remember, that all of the information provided here (except for the else statement), can be used with any relational test.
Here are some examples with other relational operators and variables used:
if(integer < 5) // Do something
if(myvar <= integer) // Do something
if(5 > integer) // Do somethingHere's a chart of Relational Operators.
Relational tests are NOT always used for conditional statements. Because these relations have a value (as discussed in the next section), they can be assigned to variables. EG:
bool = 5 < 10;Because this condition is true, bool will be given a value of one. This brings us to the next section...
if(var == 1) // Peform actionSuppose var has a value of one. That makes the condition look like this: 1 == 1. And of course, 1 is equal to 1. And because the condition is true, it has a value of one. If var was 2, then the condition would be false and would have a value of zero. If the condition was true, it would look like this:
if(1) // Perform actionAnd if it was false:
if(0) // Perform actionNote that you can use a boolean value instead of a relational test in a conditional statement. Of course, putting a one or zero in place of the relational test is worthless in a functional program. But it is very useful if you have a boolean variable. For example:
bool=1; if(bool) { // Do something }In this example, bool has a boolean value of one and the condition is true. Had bool been zero, the condition would have been false and the code block would have been skipped.
The true/false value used in conditional statements is not strictly the boolean 0 or 1. Any non-zero integer has a value of true.
However, when the engine processes a relational test, it will always return one for true and zero for false.
The "not" operator: This 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. Look at this example:
var=0; if(!var) // Run codeVar 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.
Note that 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: This operator, symbolized by two ampersands ( '&&' ), is used to combine two relational tests into one condition. For example:
if(var1 < 5 && var2 < 10) // Do somethingThe logical "and" operator requires that both relational tests be true for the entire condition to be true. Notice that the relational tests are not enclosed by parentheses. LEC and many cog programmers choose to do this, but it is not necessary.
Because Logical Operators have a lower precedence than Relational Operators, the relational tests are performed first, and then the "and" operators are processed. It is because of this that you don't have to enclose relational tests in parentheses to force them to be processed first. If you did enclose the seperate conditions, they would look like this:
if((var1 < 5) && (var2 < 10)) // Run codeWhen the relational tests in a condition are evaulated, the value of the test is used in place of the relational test. It would look something like this:
if(1 && 0 && 1 && 1 && 1) // Run codeIn 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 codeThe "or" operator: This operator is denoted with two braces like this, ' || '. This operator will make a condition true if either of two relational tests is true. For example:
if(var1 < 3 || var2 == 10) // Run codeIf either relational test was 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.
if(var1 < 2) { if(var2 == 5) { if(var3 > 10) { // Run code } } }That example could also be written without using a new code block for each statement. EG:
if(var1 < 2) if(var2 == 5) if(var3 > 10) // Run codeThe main reason to nest conditional statements and not combine them with logical operators is that you can use a seperate else statement for each condition(which is discussed below). If you don't want to use else for each relational test, you should combine the conditions into one condition to clean up your code. Like this:
if(var1 < 2 && var2 == 5 && var3 > 10) // Run codeElse Statement: Else statements are placed after an if statement. They will run the next line or code block if the statement's condition was false. If it was true, the else statement will be skipped. Look at this example:
if(var == 10) { // Run code } else { var=10; }You can remove the code blocks as in this example:
if(var == 10) // Run code else var=10;Note that the else statement will only work after an if statement. It will not work with other conditional statements such as while(). Here is a modified version of the nesting example:
if(var1 < 2) { if(var2 == 5) { if(var3 > 10) { // Run code } else Print("var3 is less than or equal to 10"); } else Print("var2 is not equal to 5"); } else Print("var1 is greater than or equal to 2");Combined conditions: Occasionally the need arises for some really complicated conditions. If you have read through all of the sections above, then these conditions should be easy to figure out. An example:
if(var1 == 3 && var2 == 6 || var1 > 3 && var2 >= 6) // Run codeBecause the '&&' has a higher precedence than the ' || ', the conditions on either side of the "or" operator are evaluated first. It will be processed something like this: (Suppose that var1 is 3 and var2 is 6)
if(var1 == 3 && var2 == 6 || var1 > 3 && var2 >= 6) if(1 && 1 || 0 && 1) if(1 || 0) if(1)And so, the condition is true and the code is run. Many people, especially beginners, may not know the precedence of the Logical Operators. So, it would probably be better to write that last condition like this:
if((var1 == 3 && var2 == 6) || (var1 > 3 && var2 >= 6)) // Run codeThere is no difference, but the parentheses make the condition easier to read. Here's a chart of Operator Precedence.
Now, sometimes you might want to put a "sub-condition" inside a condition. Lets say that you want a code block to run if a player is alive or dead, but if he is alive, you want a variable to be equal to one. This can easily be done with one condition in this way:
health=GetHealth(player); if(health == 0 || health >= 1 && var1 == 1) // Run codeNow lets say that you want three other variables to all be zero for the code to run:
if(!var2 && !var3 && !var4 && (health == 0 || health >= 1 && var1 == 1)) // Run codeThat is how to put one condition inside another.