If statement

From ZCWiki
Jump to: navigation, search

Use the if statement when you want certain things to happen only once when a particular condition is true.

Syntax and Execution

if...

The simplest form of the if statement has the following syntax:

if(test-expression){ 
  // statements inside the curly braces
  // are executed only if test-expression
  // is true.
}

where test-expression is an expression whose "thruthness" or "falsness" can be determined. If test-expression evaluates to true, then the statements inside the braces are executed; otherwise they are skipped.

Examples of some test expressions include:

  • Link->HP >= 32
True if Link has 32 hitpoints or more.
  • X != 10
True if variable X isn't 10.
  • Y == 10
True if variable Y is equal to 10.
  • (Y > 10) && (X > 10)
True if Y is greater than 10 and X is greater than 10.

Expressions can be as simple or complex as needed, as long as they evaluate to true or false.

Note that you do not end the expression with a semicolon.

if...else

If you need to check for some particular condition, but you also want something to happen if that condition isn't met, then you can add an else block:

if(test-expression){ 
  // statements inside these braces
  // are executed only if test-expression
  // is true.
}
else {
  // statements inside these braces
  // are executed only if test-expression
  // is false.
}

In this case, if test-expression is true the statements inside the first set of braces are executed; otherwise the statements in the second set of braces are executed. Note that due to this behavior, the script will never execute the statements in both sets of braces.

if...else if...else

If you want to check for multiple conditions, you may need something more than the if...else form of the if statement. Adding an else if block gives you another level of flexibility.

if(test-expression-A){ 
  // statements inside these braces
  // are executed only if test-expression-A
  // is true.
}
else if(test-expression-B) {
  // statements inside these braces
  // are executed only if test-expression-A is false
  // and test-expression-B is true.
}
else {
  // statements inside these braces
  // are executed only if test-expression-A is false
  // and test-expression-B is also false.
}

You can have as many (hypothetically) else if blocks as you need. The test-conditions are checked in the order in which they are coded. If one of the test-conditions is met, the statements in the associated braces are executed, and then the entire if statement is exited - that is, any remaining else if and else blocks are automatically skipped.

When you use one or more else if blocks, note that the else block becomes the "default" block of code; if none of the conditions in the if or else if blocks are met, then the else block is executed.

The else is not required - you can use the else if blocks without also having an else block. In this case, if none of the test-conditions are met, then no actions are performed. If you do have an else block, it must be the last block (you cannot, for example, follow an else block with an else if block).

Single Statements

Should you require only one statement to execute with an if, else, or else if statement, curly braces are not required.

if(test-expression) statement;}

where test-expression is an expression whose "thruthness" or "falsness" can be determined, and statement is the command you wish to execute if the test expression evaluates true.

Example:

if( Link->HP <= 0 && Link->Item[EXTRALIFE] ) Link->HP = Link->MaxHP;}

This if statement evaluates as true, if Link's HP is zero, or less, and he has the item defined as EXTRALIFE, then refills his HP.

Use of Not ( ! )

The Not operator ! may be used to evaluate only when a test expression is false.


if(!test-expression) statement;
if(!test-expression){ 
  // statements inside the curly braces
  // are executed only if test-expression
  // is false.
}

This applies equally to if, else, and else if.

Examples:

if(!Link->Item[I_SWORD4]){ 
  // statements inside the curly braces
  // are executed only if Link does not
  // have the master sword.
}

See: Logical operators

Comparison with Loops

By itself, the if statement is only executed once - that is, if test-expression is true, the statements for the true case are executed, and then the script execution continues with the statements after the closing curly brace. Use the while loop if you need to do certain things for as long as test-expression is true. Use the for loop if you need to do certain things a predeterined number of times.

See Also