Difference between revisions of "If"
imported>Simetrical m (IF moved to If) |
imported>Gblues (Moving the comparison operator list from the "commands" topic.) |
||
Line 1: | Line 1: | ||
Oblivion's <tt>if</tt> command is very powerful and comparable to "real" programming languages. | The <tt>if</tt> statement allows you to execute (or not execute) a block of script commands based on one or more comparisons that you specify. Oblivion's <tt>if</tt> command is very powerful and comparable to "real" programming languages. | ||
Assuming "a = 17", "b = 20" and "c = a - b", all of the following expressions work as expected. Parentheses are only needed when they're necessary for mathematical reasons. | == Comparison Operators == | ||
An if statement may contain one or more comparison operators. Below is a table of valid comparison operators: | |||
{|border="1" cellpadding="5" cellspacing="0" | |||
|- | |||
! style="background:#efefef;" | Operator | |||
! style="background:#efefef;" | Description | |||
|- | |||
| <nowiki>==</nowiki> | |||
| Exactly equal to | |||
|- | |||
| <nowiki>!=</nowiki> | |||
| Not equal to | |||
|- | |||
| <nowiki>></nowiki> | |||
| Greater than | |||
|- | |||
| <nowiki>>=</nowiki> | |||
| Greater than or equal to | |||
|- | |||
| <nowiki><</nowiki> | |||
| Less than | |||
|- | |||
| <nowiki><=</nowiki> | |||
| Less than or equal to | |||
|} | |||
It is important to note that there are no bitwise comparisons available in Oblivion's scripting language. | |||
== Combining Comparisons == | |||
Comparisons can be linked together using the following logical operators: | |||
{|border="1" cellpadding="5" cellspacing="0" | |||
|- | |||
! style="background:#efefef;" | Operator | |||
! style="background:#efefef;" | Description | |||
! style="background:#efefef;" | Example | |||
|- | |||
| <nowiki>&&</nowiki> | |||
| Logical AND | |||
| <nowiki>if x == 1 && y == 1 ; considered true only if both x and y equal 1.</nowiki> | |||
|- | |||
| <nowiki>||</nowiki> | |||
| Logical OR | |||
| <nowiki>if x == 1 || y == 1 ; considered true unless both x and y equal 0.</nowiki> | |||
|} | |||
== Comparisons and Expressions == | |||
The comparison operators can be used with any expression that can be evaluated into a number. Assuming "a = 17", "b = 20" and "c = a - b", all of the following expressions work as expected. Parentheses are only needed when they're necessary for mathematical or logical reasons. | |||
IF c == -3 && b == 20 | IF c == -3 && b == 20 | ||
Line 11: | Line 62: | ||
IF a - b == c | IF a - b == c | ||
IF a *4 - b * 4 == c * 4 | IF a *4 - b * 4 == c * 4 | ||
IF a * ( 5 + c ) - 14 == b | |||
IF 2*(a*(5+c)-14)==b - -b | |||
[[Category:Commands]] | [[Category:Commands]] |
Revision as of 00:41, 15 April 2006
The if statement allows you to execute (or not execute) a block of script commands based on one or more comparisons that you specify. Oblivion's if command is very powerful and comparable to "real" programming languages.
Comparison Operators
An if statement may contain one or more comparison operators. Below is a table of valid comparison operators:
Operator | Description |
---|---|
== | Exactly equal to |
!= | Not equal to |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
It is important to note that there are no bitwise comparisons available in Oblivion's scripting language.
Combining Comparisons
Comparisons can be linked together using the following logical operators:
Operator | Description | Example |
---|---|---|
&& | Logical AND | if x == 1 && y == 1 ; considered true only if both x and y equal 1. |
|| | Logical OR | if x == 1 || y == 1 ; considered true unless both x and y equal 0. |
Comparisons and Expressions
The comparison operators can be used with any expression that can be evaluated into a number. Assuming "a = 17", "b = 20" and "c = a - b", all of the following expressions work as expected. Parentheses are only needed when they're necessary for mathematical or logical reasons.
IF c == -3 && b == 20 IF c == -3 && b == 20 && a == 17 IF c - 1 == -4 && b == 20 && a == 17 IF a - 20 == 17 - b IF a - 20 == 17 - b && c + 3 == 0 IF a + 3 == b IF a - b == c IF a *4 - b * 4 == c * 4 IF a * ( 5 + c ) - 14 == b IF 2*(a*(5+c)-14)==b - -b