Control Structures: Difference between revisions
Line 92: | Line 92: | ||
} | } | ||
The more values you want to compare, the longer gets this sequence. That is why the simplified <tt>switch</tt>-statement was introduced. | |||
The <tt>switch</tt>-statement compares a variable against different values: | The <tt>switch</tt>-statement compares a variable against different values: |
Revision as of 19:08, 21 December 2006
Control Structures are special sequences of scripting code. They help to control complex procedures. You can use control structures to define code which is only executed under certain conditions or repeated for a couple of times.
Requirements
To fully understand this article you should read the following articles:
Conditional Structures
Conditional structures help to define code which is only executed under certain circumstances. Often you will write code that depends on the game state, on other code or other conditions.
if-Statement
The if-statement defines code that is only executed if a certain condition is met. The syntax of it looks like that:
if (CONDITION) then { STATEMENT; }
- CONDITION is a boolean statement or variable which returns either true or false. The code nested in the following block is only executed if the condition is true, else ignored.
- STATEMENT is a custom sequence of statements. That may be commands, assignments, control structures etc.
Example:
if (_temperature < 0) then { hint "Snow!"; }
In this example, first the value of _temperature is checked:
- If the value is greater than 0, the nested code is ignored.
- If the value is 0 or less than 0, the command hint "Snow!"; is executed.
Alternative Code
You can also define alternative code that is executed, when the condition is not true.
if (CONDITION) then { STATEMENT; } else { STATEMENT; }
Here you have a second statement executed when CONDITION is false.
Nested if-Statements
Since the if-statement is itself a statement, you can also create nested if-statements.
Example:
if (alive player) then { if (someAmmo player) then { hint "The player is alive and has ammo!"; } else { hint "The player is out of ammo!"; } } else { hint "The player is dead!"; }
switch-Statements
(Armed Assault only)
In some cases you may want to check a variable for several values and execute different code depending on the value. With the above knowledge you could just write a sequence of if-statements.
if (_color == "blue") then { hint "What a nice color"; } else { if (_color == "red") then { hint "Don't you get aggressive?"; } }
The more values you want to compare, the longer gets this sequence. That is why the simplified switch-statement was introduced.
The switch-statement compares a variable against different values:
switch (VARIABLE) do { case VALUE1: { STATEMENT; }; case VALUE2: { STATEMENT; }; ... }
The structure compares VARIABLE against all given values (VALUE1, VALUE2, ...). If any of the values matches, the corresponding block of statements is executed.
Example:
switch (_color) do { case "blue": { hint "What a nice color"; }; case "red": { hint "Don't you get aggressive?"; }; }
default-Block
In some cases you may want to define alternative code that is executed, when none of the values matches. You can write this code in a default-Block.
switch (VARIABLE) do { case VALUE1: { STATEMENT; }; case VALUE2: { STATEMENT; }; ... default: { STATEMENT; }; }
Loops
tbc