Difference between revisions of "Using Logical Operators and Parentheses"

From LongJump Support Wiki
imported>Aeric
imported>Aeric
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
Logical operators combine subexpressions into a larger expression, to specify multiple conditions and complex criteria.
Logical operators combine subexpressions into a larger expression, to specify multiple conditions and complex criteria.


<tt><subexpression1> <Logical_operator> <subexpression2>...</tt>
: <tt><subexpression1> <Logical_operator> <subexpression2>...</tt>


{{:Boolean}}
{{:Boolean}}
Line 9: Line 9:
Expressions are evaluated in this order:
Expressions are evaluated in this order:
#expressions within <tt>(parentheses)</tt>  
#expressions within <tt>(parentheses)</tt>  
#expressions with <tt>&& (Logical AND)</tt>  
#expressions with Logical <tt>AND</tt>  
#expressions with <tt>|| (Logical OR)</tt>
#expressions with Logical <tt>OR</tt>


::''If the expression is not clearly annotated with parentheses, then <tt>&&</tt> (Logical AND) takes precedence over <tt>||</tt> (Logical OR)''
::''If the expression is not clearly annotated with parentheses, then Logical <tt>AND</tt> takes precedence over Logical <tt>OR</tt>''
 
{{Tip|For any complex expression, always use parentheses. That way, you're sure to get what you expect, and the meaning is clear when you or anyone else reads it later.}}


===Examples===
===Examples===


{| border="1" cellpadding="5" cellspacing="0"
:*Find all accounts where the state is either California, Nevada, or Washington
!Expression
::<tt><nowiki>(State = 'CA') OR (State = 'NV') OR (State = 'WA')</nowiki></tt>
!Description
:*Find all accounts where the state is not California, Nevada, or Washington:
|-
::<tt><nowiki>(State != 'CA') AND (State != 'NV') AND (State != 'WA')</nowiki></tt>
|<tt><nowiki>(Number of Employees <50 && Area_Code = '408') || (State = 'CA' && Industry = 'Construction'</nowiki></tt>||This expression finds all accounts that have fewer than 50 employees located in area code 408 or accounts in the construction industry in California
:*Find all accounts in the construction industry where the state is California or Nevada
|-
::<tt><nowiki>((State = 'CA') OR (State = 'NV')) AND (Industry = 'Construction')</nowiki></tt>
|<tt><nowiki>(State = 'CA') || (State = 'NV') || (State = 'WA')</nowiki></tt>||This expression finds all accounts where the state is either California, Nevada, or Washington
:*Finds account in the State of California or accounts in construction in the state of Nevada
|-
::<tt><nowiki>State = 'CA' OR State = 'NV' AND Industry = 'Construction'</nowiki></tt>
|<tt><nowiki>(State != 'CA') && (State != 'NV') && (State != 'WA')</nowiki></tt>||By contrast, this expression finds all accounts where the state is not California, Nevada, or Washington
:*Also finds account in the State of California or accounts in construction in the state of Nevada (the <tt>AND (AND)</tt> operator implies brackets)
|-
::<tt><nowiki>State = 'CA' OR (State = 'NV' AND Industry = 'Construction')</nowiki></tt>
|<tt><nowiki>((State = 'CA') || (State = 'NV')) && (Industry = 'Construction')</nowiki></tt>||This expression finds all accounts in the construction industry where the state is California or Nevada
:*Find accounts in California or Texas where the account name contains "Equipment"
|-
::<tt><nowiki>(Acct_Name contains 'Equipment') AND (state = 'CA' OR state ='TX')</nowiki></tt>
|<tt><nowiki>State = 'CA' || State = 'NV' && Industry = 'Construction'</nowiki></tt>||Taking the example from above, but removing the parentheses finds accounts in the State of California or accounts in construction in the state of Nevada
:*Find accounts in California where the account name contains "Equipment", or any record in Texas
|-
::<tt><nowiki>(Acct_Name contains 'Equipment') AND (state = 'CA')) OR (state ='TX')</nowiki></tt>
|<tt><nowiki>State = 'CA' || (State = 'NV' && Industry = 'Construction')</nowiki></tt>||Note that the <tt>AND (&&)</tt> operator implies parentheses
|-
|<tt><nowiki>contains(Acct_Name, 'Equipment') && (state = 'CA' || state ='TX')</nowiki></tt>||This expression finds accounts in California or Texas where the account name contains "Equipment"
|-
|<tt><nowiki>contains(Acct_Name, 'Equipment') && (state = 'CA') || state ='TX'</nowiki></tt>||This expression finds accounts in California where the account name contains "Equipment", or any record in Texas.
|}

Latest revision as of 17:45, 13 June 2011

Logical operators combine subexpressions into a larger expression, to specify multiple conditions and complex criteria.

<subexpression1> <Logical_operator> <subexpression2>...

LOGICAL_OPERATOR

Logical operators can be used to build more complex expressions.
The logical operators are:
AND
OR
Considerations
  • Two subexpressions joined by a logical operator form a logical expression.
  • Logical expressions resolve to a Boolean value: 1/0 or TRUE/FALSE.
  • Use parentheses--()--to group expressions logically and to join multiple expressions.
  • Parentheses are used in pairs; each open parenthesis "(" requires a closing parenthesis ")".
  • For example:
((<expression1> AND <expression2>) OR (<expression3> AND <expression_4>))


Precedence

Expressions are evaluated in this order:

  1. expressions within (parentheses)
  2. expressions with Logical AND
  3. expressions with Logical OR
If the expression is not clearly annotated with parentheses, then Logical AND takes precedence over Logical OR

Thumbsup.gif

Tip: For any complex expression, always use parentheses. That way, you're sure to get what you expect, and the meaning is clear when you or anyone else reads it later.

Examples

  • Find all accounts where the state is either California, Nevada, or Washington
(State = 'CA') OR (State = 'NV') OR (State = 'WA')
  • Find all accounts where the state is not California, Nevada, or Washington:
(State != 'CA') AND (State != 'NV') AND (State != 'WA')
  • Find all accounts in the construction industry where the state is California or Nevada
((State = 'CA') OR (State = 'NV')) AND (Industry = 'Construction')
  • Finds account in the State of California or accounts in construction in the state of Nevada
State = 'CA' OR State = 'NV' AND Industry = 'Construction'
  • Also finds account in the State of California or accounts in construction in the state of Nevada (the AND (AND) operator implies brackets)
State = 'CA' OR (State = 'NV' AND Industry = 'Construction')
  • Find accounts in California or Texas where the account name contains "Equipment"
(Acct_Name contains 'Equipment') AND (state = 'CA' OR state ='TX')
  • Find accounts in California where the account name contains "Equipment", or any record in Texas
(Acct_Name contains 'Equipment') AND (state = 'CA')) OR (state ='TX')