HOWTO: Use OR in an Expression

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Support
    Forum Member
    • Jan 2000
    • 204

    HOWTO: Use OR in an Expression

    HOWTO: Use OR in an Expression

    HOWTO: Use OR in an Expression

    Document ID: IR04043
    The information in this article applies to:
    • AutoPlay Media Studio 4.0
    • Setup Factory 6.0
    • TrueUpdate 1.0

    SUMMARY

    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.

    DISCUSSION

    The OR operator is used to combine two expressions, so that if either expression is true, the combined expression will be true.

    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.

    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:

    %a% or %b%

    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:

    %pet% = "cat" or %pet = "dog"

    You may be wondering why you can't just write:

    %pet% = "cat" or "dog"

    The reason is that the OR operator is used to combine expressions; in this case, %pet% = "cat" is one expression, and "dog" 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."

    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.

    You might try to fix the expression using parentheses to group the "cat" or "dog" part together, like so:

    %pet% = ("cat" or "dog")

    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".

    If you really wanted to use parentheses, you could use them like this:

    (%pet% = "cat") or (%pet = "dog")

    ...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.

    If you're wondering why this expression works with or without the parentheses, it's because the = operator has higher precedence than the OR operator. What this basically means is that, all else being equal, an = will always be done before an OR. So %pet% = "cat" and %pet% = "dog" are both resolved before the OR operator is applied.

    Tip: The rules that decide what gets performed first are known as operator precedence and associativity.

    The Good 'Ole OR Truth Table

    If you write down all the different combinations for using OR, it looks like this:

    ExpressionResult
    true or falsetrue
    false or truetrue
    true or truetrue
    false or falsefalse

    MORE INFORMATION

    For more information please see the following topics in the AutoPlay Media Studio 4.0 help file:

    • User's Guide | Expressions | What Are Expressions?
    • User's Guide | Expressions | Boolean Values
    • User's Guide | Expressions | Operators
    • User's Guide | Expressions | Operator Precedence and Associativity
    • User's Guide | Expressions | Logical (Boolean) Operators

    KEYWORDS: AutoPlay Media Studio 4.0, Setup Factory 6.0, TrueUpdate 1.0, or, equal, parentheses, IF, WHILE, expression, variable


    Last reviewed: October 30, 2002
    Copyright © 2002 Indigo Rose Corporation. All rights reserved.
Working...
X