596 Using Operators In REALbasic, you can declare a constant without the data type declaration, such as Const Pi = 3.14 Using Operators The three types of operators used are mathematical, relational, and logical. Mathematical operators calculate numeric results such as adding, multiplying, or dividing numbers. Table 5-5 lists the mathematical operators used in Visual Basic and REALbasic. Table 5-5 Mathematical Operators Mathematical Operator Purpose Example + Addition 5 + 3.4 - Subtraction 203.9 - 9.12 * Multiplication 39 * 146.7 / Division 45/ 8.41 \ Integer division 35 \ 9 = 3 Mod Modula division (returns the remainder) 35 mod 9 = 8 ^ Exponentiation 2 ^ 4 Relational operators compare two values and return a True or False value. The six relational operators available in Visual Basic and REALbasic are shown in Table 5-6. Table 5-6 Relational Operators Relational Operator Purpose = Equal Not equal < Less than Greater than >= Greater than or equal to Logical operators compare two Boolean values (True or False) and return a single True or False value. Both Visual Basic and REALbasic use the same logical operators but Visual Basic includes two additional operators — AndAlso and OrElse, as shown in Table 5-7.
Branching Statements 597 Table 5-7 Logical Operator And Or Xor Not AndAlso (only in Visual Basic) OrElse (only in Visual Basic) Truth Table Logical Operators True And True = True True And False = False False And True = False False And False = False True Or True = True True Or False = True False Or True = True False Or False = False True Xor True = False True Xor False = True False Xor True = True False Xor False = False Not True = False Not False = True True AndAlso True = True True AndAlso False = False False AndAlso (never evaluated) = False False AndAlso (never evaluated) = False True OrElse (never evaluated) = True True OrElse (never evaluated) = True False OrElse True = True False OrElse False = False Book VI Chapter 5 Visual Basic and REALbasic In Visual Basic, the AndAlso and OrElse operators act as faster versions of the traditional And and Or operators. The And operator must always compare two Boolean values, but if the AndAlso operator determines that the first Boolean value is False, it doesn’t waste time evaluating the second Boolean value because one False Boolean value automatically makes the entire AndAlso operator evaluate to False. The OrElse operator works the same way. If the OrElse operator identifies the first Boolean value as True, it doesn’t waste time evaluating the second Boolean value because it will always evaluate to True anyway. Branching Statements The simplest branching statement is an IF-THEN statement that only runs one or more commands if a Boolean condition is True, such as IF condition THEN Command END IF