PDA

View Full Version : HOWTO: Use OR in an Expression



Support
10-30-2002, 01:37 PM
<HTML><HEAD><TITLE>HOWTO: Use OR in an Expression</TITLE></HEAD><BODY><h3>HOWTO: Use OR in an Expression</h3><b>Document ID: IR04043</b><hr>The information in this article applies to:<ul><li>AutoPlay Media Studio 4.0</li><li>Setup Factory 6.0</li><li>TrueUpdate 1.0</li></ul><hr><h3>SUMMARY</h3><p>This article will explain how to use the OR operator in an expression when you want to determine whether a variable is equal to one of two values.</p><h3>DISCUSSION</h3><p>The OR operator is used to combine two expressions, so that if either expression is true, the combined expression will be true.</p><p>It tests whether the value on either side of it is true or false, and says the whole thing is true if either of them is true, and false if they are both false.</p><p>For example, to create an expression that will only be true if either the variable %a% or the variable %b% is currently set to "true", you simply combine them with an OR operator, like this:</p><pre><code>%a% or %b%</code></pre><p>To create an expression that will be true if the value in the variable %pet% is either "cat" or "dog", you would use an expression like this:</p><pre><code>%pet% = "cat" or %pet = "dog"</code></pre><p>You may be wondering why you can't just write:</p><pre><code>%pet% = "cat" or "dog"</code></pre><p>The reason is that the OR operator is used to combine <em>expressions</em>; in this case, <code>%pet% = "cat"</code> is one expression, and <code>"dog"</code> is another. Instead of saying "this will be true if %pet% is equal to 'cat' or 'dog'," you're actually saying "this will be true if %pet% is equal to 'cat', or if 'dog' is true."</p><p>Since all strings other than the word "true" are considered false, the OR operator would just think "okay, well, 'dog' is false," and everything would be left up to the other part of the expression. In practice, this expression would seem to work only half the time; it would be true when %pet% was equal to "cat", but setting %pet% to "dog" would make the expression false.</p><p>You might try to fix the expression using parentheses to group the "cat" or "dog" part together, like so:</p><pre><code>%pet% = ("cat" or "dog")</code></pre><p>Unfortunately, that wouldn't work either. Since the OR operator only understands "true" or "false", the strings "cat" and "dog" would be reduced to their true/false equivalents; in this case, they would both be seen as false. The OR operator would go, "okay, 'cat' is false, and 'dog' is false; neither of them is true, so the whole thing is false!" and then the = operator would compare the value in %pet% with "0", which is how the OR operator writes the value "false".</p><p>If you really wanted to use parentheses, you could use them like this:</p><pre><code>(%pet% = "cat") or (%pet = "dog")</code></pre><p>...which you'll notice is just the first (working) expression with the parentheses added around each "sub-expression" on either side of the OR operator.</p><p>If you're wondering why this expression works with or without the parentheses, it's because the = operator has <em>higher precedence</em> than the OR operator. What this basically means is that, all else being equal, an = will always be done before an OR. So <code>%pet% = "cat"</code> and <code>%pet% = "dog"</code> are both resolved before the OR operator is applied.</p><p><b>Tip:</b> The rules that decide what gets performed first are known as <em>operator precedence and associativity</em>.</p><h4>The Good 'Ole OR Truth Table</h4><p>If you write down all the different combinations for using OR, it looks like this:</p><table border="1" cellpadding="5" cellspacing="0"><tr><th>Expression</th><th>Result</th></tr><tr><td>true or false</td><td>true</td></tr><tr><td>false or true</td><td>true</td></tr><tr><td>true or true</td><td>true</td></tr><tr><td>false or false</td><td>false</td></tr></table><h3>MORE INFORMATION</h3><p>For more information please see the following topics in the AutoPlay Media Studio 4.0 help file:</p><ul><li><b>User's Guide | Expressions | What Are Expressions?</b></li><li><b>User's Guide | Expressions | Boolean Values</b></li><li><b>User's Guide | Expressions | Operators</b></li><li><b>User's Guide | Expressions | Operator Precedence and Associativity</b></li><li><b>User's Guide | Expressions | Logical (Boolean) Operators</b></li></ul><p>KEYWORDS: AutoPlay Media Studio 4.0, Setup Factory 6.0, TrueUpdate 1.0, or, equal, parentheses, IF, WHILE, expression, variable</p><hr><FONT SIZE=1>Last reviewed: October 30, 2002<br>Copyright 2002 <A HREF="http://www.indigorose.com" target="blank">Indigo Rose Corporation</a>. All rights reserved.<br></FONT></BODY></HTML>