And and Or (SelectionCriterion functions): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 1: Line 1:
{{Template:SelectionCriterion:And and Or subtitle}}
{{Template:SelectionCriterion:And and Or subtitle}}


These shared methods each create a new SelectionCriterion object
These shared methods each create a new <var>SelectionCriterion</var> object
that is an expression used to select the items in a collection.
that is an expression used to select the items in a collection.
Each constructor uses a different logical operator
Each constructor uses a different logical operator
to form an expression that combines one or more SelectionCriterion objects.
to form an expression that combines one or more <var>SelectionCriterion</var> objects.


An OR criterion returns true for a collection item if any of the
An <var>Or</var> criterion returns true for a collection item if any of the
component SelectionCriterion expressions are true for the item;
component <var>SelectionCriterion</var> expressions are true for the item;
otherwise it returns false.
otherwise it returns false.
An AND criterion returns true if all of the
An <var>And</var> criterion returns true if all of the
component SelectionCriterion expressions are true for the item;
component <var>SelectionCriterion</var> expressions are true for the item;
otherwise it returns false.
otherwise it returns false.


The collection searching method ([[??]] refid=srchcol.)
The [[Collections#Searching a collection|collection searching method]]
that makes use of a selection criterion specifies:
that makes use of a <var>SelectionCriterion</var> specifies:
<ul>
<ul>
<li>Whether to return the first item or item number or all items that satisfy the
<li>Whether to return the first item or item number or all items that satisfy the
Line 23: Line 23:
{{Template:SelectionCriterion:And syntax}}
{{Template:SelectionCriterion:And syntax}}
{{Template:SelectionCriterion:Or syntax}}
{{Template:SelectionCriterion:Or syntax}}
  %selc = [%(selectionCriterion for itemtype):]  -
 
            OR(criterion1 [,&nbsp;criterion2] ... [,&nbsp;criterionN])
===Syntax terms===
===Syntax terms===
<dl>
<table class="syntaxTable">
<dt><i>%selc</i>
<tr><th>%selectionCriterion</th>
<dd>A SelectionCriterion object variable to contain the new object instance.
<td>A <var>SelectionCriterion</var> object variable to contain the new object instance. </td></tr>
<dt><i>%(selectionCriterion for itemtype)</i>
<tr><th>%(selectionCriterion for itemtype)</th>
<dd>This optional specification of the class and collection item type
<td>This optional specification of the class in parentheses indicates that the method is shared and does not operate on a specific instance. If you use this option, however, you must include the collection item type to which the selection expression will be applied, like this: <code>%(selectionCriterion For <i>itemtype</i>):</code> </td></tr>
in parentheses indicates
<tr><th>list</th>
that the method is shared and does not operate on a specific instance.
<td>A comma-separated list of one or more <var>SelectionCriterion</var> objects. </td></tr>
<dt><i>criterion</i>
<dd>A SelectionCriterion object.


</dl>
</table>
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>All OR and AND SelectionCriterion conditions
<li>All <var>Or</var> and <var>And</var> <var>SelectionCriterion</var> conditions
are short-circuiting conditions.
are short-circuiting conditions.
That is, if any of the conditions in
That is, if any of the conditions in
an OR return True, the subsequent conditions are not evaluated and the
an <var>Or</var> return True, the subsequent conditions are not evaluated and the
OR returns True.
<var>Or</var> returns True.
Similarly, if any of the conditions in an AND return
Similarly, if any of the conditions in an <var>And</var> return
False, the subsequent conditions are not evaluated, and the AND returns a
False, the subsequent conditions are not evaluated, and the <var>And</var> returns a
False.
False.


Therefore, it is wise to put the most likely conditions first
Therefore, it is wise to put the most likely conditions first
in an OR, and it is wise to put the least likely first in an AND.
in an <var>Or</var>, and it is wise to put the least likely first in an <var>And</var>.
For a mix
For a mix
of conditions where some are simply variable references and others require
of conditions where some are simply variable references and others require
Line 59: Line 56:
<li>The following criterion
<li>The following criterion
matches numbers less than 70 and greater than or equal to 95:
matches numbers less than 70 and greater than or equal to 95:
<pre>
<p class="code">%sel = OR(lt(this,70), ge(this,95))
    %sel = OR(lt(this,70), ge(this,95))
</p>
</pre>
<li>The following criterion matches numbers that are
<li>The following criterion matches numbers that are
less than or equal to 33 but not 30:
less than or equal to 33 but not 30:
<pre>
<p class="code">%sel = AND(le(this, 33), ne(this,30)))
    %sel = AND(le(this, 33), ne(this,30)))
</p>
</pre>
</ol>
</ol>


==See also==
==See also==
{{Template:SelectionCriterion:And and Or footer}}
{{Template:SelectionCriterion:And and Or footer}}

Revision as of 21:25, 23 June 2011

Create selection expression based on logical operator (SelectionCriterion class)


These shared methods each create a new SelectionCriterion object that is an expression used to select the items in a collection. Each constructor uses a different logical operator to form an expression that combines one or more SelectionCriterion objects.

An Or criterion returns true for a collection item if any of the component SelectionCriterion expressions are true for the item; otherwise it returns false. An And criterion returns true if all of the component SelectionCriterion expressions are true for the item; otherwise it returns false.

The collection searching method that makes use of a SelectionCriterion specifies:

  • Whether to return the first item or item number or all items that satisfy the selection criterion.
  • Where in the collection to begin searching.

Syntax

%selectionCriterion = [%(SelectionCriterion For itemType):]And( list)

%selectionCriterion = [%(SelectionCriterion For itemType):]Or( list)

Syntax terms

%selectionCriterion A SelectionCriterion object variable to contain the new object instance.
%(selectionCriterion for itemtype) This optional specification of the class in parentheses indicates that the method is shared and does not operate on a specific instance. If you use this option, however, you must include the collection item type to which the selection expression will be applied, like this: %(selectionCriterion For itemtype):
list A comma-separated list of one or more SelectionCriterion objects.

Usage notes

  • All Or and And SelectionCriterion conditions are short-circuiting conditions. That is, if any of the conditions in an Or return True, the subsequent conditions are not evaluated and the Or returns True. Similarly, if any of the conditions in an And return False, the subsequent conditions are not evaluated, and the And returns a False. Therefore, it is wise to put the most likely conditions first in an Or, and it is wise to put the least likely first in an And. For a mix of conditions where some are simply variable references and others require method evaluation, it probably is best to put the variable references first, as these are probably much cheaper to evaluate.

Examples

  1. The following criterion matches numbers less than 70 and greater than or equal to 95:

    %sel = OR(lt(this,70), ge(this,95))

  2. The following criterion matches numbers that are less than or equal to 33 but not 30:

    %sel = AND(le(this, 33), ne(this,30)))

See also