Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
This chapter is from the book

Making Tests and Comparisons

Next to getting and setting values, the most common thing to do with values is compare them. Comparisons are highly useful in programming to control the order of execution of program statements. The result of any comparison—whether of strings, numbers, or other types of data—is always of Boolean type (true or false).

Using Boolean values

Boolean values can enter your code in many ways. A Boolean might be the value of a user-interface property that you set in the IDE, the result of a computation, or a directly assigned value. Booleans have their own kind of arithmetic by which they can be combined. There are three important operators for combining Booleans: not, and, and or. The result of any combination of Booleans is a Boolean.

To negate a Boolean value:

  • Use the not operator.

    The result is true if, and only if, the argument is false (Figure 4.30).

    Figure 4.20Figure 4.30 Boolean operations.

    An operator's arguments are the values to which it is being applied.

To perform the logical andof two Boolean values:

  • Use the and operator.

    The result is true if, and only if, both the arguments are true (see Figure 4.30).

To perform the logical or of two Boolean values:

  • Use the or operator.

    The result is true if, and only if, at least one of the arguments is true (see Figure 4.30).


  • Booleans can be combined in expressions of great complexity. Use parentheses to group elements of complex expressions to help improve clarity. The expression b1 and b2 and b3 and b4 is unambiguous, for example, but (b1 and b2) and not (b2 or b3) would be hard for either a human or the REALbasic compiler to understand without the parentheses.

Comparing data items

You can use Booleans to compare the sizes of numbers or compare strings for alphabetic priority. Surprisingly, the syntax is often the same.

To compare two numbers:

  • Use any of the comparison operators in Table 4.2.

Table 4.2 Comparison Operators






A = B



A <> B


Greater than

A > B


Less than

A < B


Greater than or equal to

A >= B


Less than or equal to

A <= B

The result of comparing two numbers is a Boolean. Here's an example:

dim b as boolean
dim m,n as integer
m = 4
n = 8
b = m > n

Because m is not greater than n, this code assigns the Boolean value false to b (Figure 4.31).

Figure 31Figure 4.31 Comparing numbers.


  • Generally, the values you are comparing must be of the same data type. REALbasic, however, is forgiving enough to allow you to compare two numbers of different numeric types, such as an integer and a single.

To compare two strings:

  • For case-insensitive comparison, use any of the comparison operators in Table 4.2.

    When these operators are used to compare strings, they test alphabetic priority rather than size. These comparisons all return true:

    house > horse
    then > the
    ford = Ford
  • For case-sensitive comparison, use the strComp function:


    strComp returns an integer: -1, 0, or 1. It returns:

    1 if s1 > s2,
    -1 if s1 < s2
    0 if s1 = s2

    The mode parameter should be set to 0 for case-insensitive comparison. See the REALbasic documentation for details on its use.


  • The strComp function differs from the other comparison operators. For one thing, it is a function, not an operator; for another, it returns an integer value rather than a Boolean.

  • The equals sign (=) performs two quite different functions: test for equality and value assignment. The REALbasic compiler always knows which meaning is correct in any context, and you, the programmer, should always know what you intended (Figure 4.32).

    Figure 4.32Figure 4.32 The equals sign (=) performs two functions: equality testing and value assignment. Here, the equals sign on the left side assigns the value to its right to the variable on its left, and the equals sign on the right side tests for the equality of two values.

Using tests to control the sequence of instruction execution

Although a handler or method is a sequence of instructions, sometimes you want to change the order in which those instructions are carried out. If you want to select among instructions to execute or want to execute a set of instructions repeatedly, you are manipulating the sequence of the instructions. The next sections show you how to do this.

  • + Share This
  • 🔖 Save To Your Account