Difference between revisions of "If"
Jump to navigation
Jump to search
→Combining Comparisons: clarifying OR and AND a bit, changed external into internal link
imported>Thalassicus |
imported>Qazaaq (→Combining Comparisons: clarifying OR and AND a bit, changed external into internal link) |
||
Line 63: | Line 63: | ||
| <nowiki>&&</nowiki> | | <nowiki>&&</nowiki> | ||
| Logical AND | | Logical AND | ||
| <nowiki>if x == 1 && y == 1 ; considered true only if both x and y equal 1. | | <nowiki>if x == 1 && y == 1 ;</nowiki> considered true ''only if'' both x ''and'' y equal 1. | ||
|- | |- | ||
| <nowiki>||</nowiki> | | <nowiki>||</nowiki> | ||
| Logical OR | | Logical OR | ||
| <nowiki>if x == 1 || y == 1 ; considered true unless both x and y equal 0. | | <nowiki>if x == 1 || y == 1 ;</nowiki> considered true ''unless'' both x ''and'' y equal 0. | ||
|} | |} | ||
Line 76: | Line 76: | ||
* In ''Common operator notation'' involving "normal" algebra or boolean algebra, "*" ("&&") is always evaluated before "+" ("||"), it has a higher precedence number than the "+" operator. For example, 3×4+5 = ((3×4)+5), not (3×(4+5)). '''However, this is not the case with Oblivion''''s scripting language, since '''OR ("||") has a higher precedence than AND ("&&")''':<pre>if myVar1 == 1 && myVar2 == 1 || myVar2 == 5</pre>is equivalent to<pre>if myVar1 == 1 && (myVar2 == 1 || myVar2 == 5)</pre>This is true when MyVar1 = 1 AND myVar2 is either 1 or 5.<BR><BR>If you need the "&&" comparision operator to be evaluated before the "||" (OR) operator, you must include its part in-between parentheses. In this case: <pre>if (myVar1 == 1 && myVar2 == 1) || myVar2 == 5</pre>is true when either myVar2 is 5 OR both, myVar1 and myVar2 are 1. | * In ''Common operator notation'' involving "normal" algebra or boolean algebra, "*" ("&&") is always evaluated before "+" ("||"), it has a higher precedence number than the "+" operator. For example, 3×4+5 = ((3×4)+5), not (3×(4+5)). '''However, this is not the case with Oblivion''''s scripting language, since '''OR ("||") has a higher precedence than AND ("&&")''':<pre>if myVar1 == 1 && myVar2 == 1 || myVar2 == 5</pre>is equivalent to<pre>if myVar1 == 1 && (myVar2 == 1 || myVar2 == 5)</pre>This is true when MyVar1 = 1 AND myVar2 is either 1 or 5.<BR><BR>If you need the "&&" comparision operator to be evaluated before the "||" (OR) operator, you must include its part in-between parentheses. In this case: <pre>if (myVar1 == 1 && myVar2 == 1) || myVar2 == 5</pre>is true when either myVar2 is 5 OR both, myVar1 and myVar2 are 1. | ||
The later also explains why one has to be very careful in positioning conditions in a [ | The later also explains why one has to be very careful in positioning conditions in a [[:Category:Conditions#The_Condition_List|condition list]] of an editor item: for the CS/OB's engine, OR has order preference, ''has precedence'' over AND. For example, the condition items (A AND B OR C AND D) are evaluated as (A AND (B OR C) AND D), and not (( A AND B) OR (C AND D)), as opposed to common operator notation for most languages. In general, we call this an ''inversed'' or ''negative'' notation. '''Always keep the later in mind when [http://en.wikipedia.org/wiki/Boolean_algebra_%28logic%29#Basic_operations applying boolean algebra] for evaluating some given expression when scripting''', as standard operator notations will lead you to errors. | ||
=== Oblivion evaluates entire If statement === | === Oblivion evaluates entire If statement === |