Or

From REALbasicWiki

Jump to: navigation, search

The Or operator operates on either Booleans or Integers (support for Integer was added in REALbasic 2007r2).

When operating on Booleans, or is the standard Boolean operation, defined on values as follows.


true or true = true
true or false = true
false or true = true
false or false = false


When operating on Integers, or operates bitwise on the operands. To see what this means, we first provide a table for operation on a single bit.

1 or 1 = 1
1 or 0 = 1
0 or 1 = 1
0 or 0 = 0

That is, 1 corresponds to true and 0 corresponds to false.

On arbitrary integers, or operates on the bits in the base 2 representations of the operands. Here is an example.

3 or 5 = 0011 or 0101
        = (0 or 0)*8 + (0 or 1)*4 + (1 or 0)*2 + (1 or 1)*1
        = 7

[edit] Short-Circuit Evaluation

The Or operator performs short-circuit evaluation. This means that if the first operand evaluates to true, then Or returns true without evaluating the second operand. For example, consider the following code.

dim f as FolderItem = GetFolderItem("/path/to/ruin", FolderItem.PathTypeShell)
if f is nil or not f.Exists then
//skip
else
print f.ShellPath
end if

If the expression f is nil evaluates to true, then Or returns true without evaluating the second expression not f.Exists, and so you will not get a NilObjectException when f is nil. But beware that the following code is not exception-safe.

if GetFolderItem("/path/to/ruin", FolderItem.PathTypeShell) is nil or not GetFolderItem("/path/to/ruin", FolderItem.PathTypeShell).Exists then

This code can result in the raising of a NilObjectException because GetFolderItem is a function that may return Nil the second time you call it.

Personal tools
related