Visual Basic 2005 for Programmers (2nd Edition)

3.7. Decision Making: Equality and Relational Operators

This section introduces Visual Basic's If...Then statement, which allows a program to make a decision based on the truth or falsity of some expression. The expression in an If...Then statement is called a condition. If the condition is met (i.e., the condition is true), the statement in the If...Then statement's body executes. If the condition is not met (i.e., the condition is false), the body statement is not executed. Conditions in If...Then statements can be formed by using the equality operators and relational operators (also called comparison operators), which are summarized in Fig. 3.21. The relational and equality operators all have the same level of precedence and associate from left to right.

Figure 3.21. Equality and relational operators.

Standard algebraic equality operator or relational operator

Visual Basic equality or relational operator

Example of Visual Basic condition

Meaning of Visual Basic condition

Equality operators

=

=

x = y

x is equal to y

<>

x <> y

x is not equal to y

Relational operators

>

>

x > y

x is greater than y

<

<

x < y

x is less than y

>=

x >= y

x is greater than or equal to y

<=

x <= y

x is less than or equal to y

Common Programming Error 3.3

It is a syntax error to reverse the symbols in the operators <>, >= and <= (as in ><, =><, =>).

The next example uses six If...Then statements to compare two numbers entered into a program by the user. If the condition in any of these statements is true, the output statement associated with that If...Then executes. The user enters these values, which are stored in variables number1 and number2, respectively. The comparisons are performed, and the results of the comparison are displayed in the Console window. The program and outputs are shown in Fig. 3.22.

Figure 3.22. Performing comparisons with equality and relational operators.

1 ' Fig. 3.26: Comparison.vb 2 ' Using equality and relational operators. 3 4 Module Comparison 5 6 Sub Main() 7 8 ' declare Integer variables for user input 9 Dim number1 As Integer 10 Dim number2 As Integer 11 12 ' read first number from user 13 Console.Write("Please enter first integer: ") 14 number1 = Console.ReadLine() 15 16 ' read second number from user 17 Console.Write("Please enter second integer: ") 18 number2 = Console.ReadLine() 19 20 If number1 = number2 Then ' number1 is equal to number2 21 Console.WriteLine(number1 & " = " & number2) 22 End If 23 24 If number1 <> number2 Then ' number1 is not equal to number2 25 Console.WriteLine(number1 & " <> " & number2) 26 End If 27 28 If number1 < number2 Then ' number1 is less than number2 29 Console.WriteLine(number1 & " < " & number2) 30 End If 31 32 If number1 > number2 Then ' number1 is greater than number2 33 Console.WriteLine(number1 & " > " & number2) 34 End If 35 36 ' number1 is less than or equal to number2 37 If number1 <= number2 Then 38 Console.WriteLine(number1 & " <= " & number2) 39 End If 40 41 ' number1 is greater than or equal to number2 42 If number1 >= number2 Then 43 Console.WriteLine(number1 & " >= " & number2) 44 End If 45 46 End Sub ' Main 47 48 End Module ' Comparison

Please enter first integer: 1000 Please enter second integer: 2000 1000 <> 2000 1000 < 2000 1000 <= 2000

Please enter first integer: 515 Please enter second integer: 49 515 <> 49 515 > 49 515 >= 49

Please enter first integer: 333 Please enter second integer: 333 333 = 333 333 <= 333 333 >= 333

The If...Then statement in lines 2022 compares the values of the variables number1 and number2 for equality. If the values are equal, the statement in line 21 outputs a string indicating that the two numbers are equal. Note that assignment and the equality operator both use the = symbol. When a condition is expected (such as after the If keyword in an If...Else statement), the = is used as an equality operator.

If number1 contains the value 333 and number2 contains the value 333, the expression in line 21 evaluates as follows: number1 is converted to a string and concatenated with the string "=", then number2 is converted to a string and concatenated with the resulting string from the first concatenation. At this point, the string "333 = 333" is displayed by WriteLine. As the program proceeds through the remaining If...Then statements (lines 2444), additional strings are output by the WriteLine statements. For example, when given the value 333 for number1 and number2, the conditions in lines 37 and 42 also are true. Thus, the output displayed (in the third output of Fig. 3.22) is:

333 = 333 333 <= 333 333 >= 333

The table in Fig. 3.23 shows the precedence of the operators introduced in this chapter. The operators are displayed from top to bottom in decreasing order of precedence.

Figure 3.23. Precedence of the operators introduced in this chapter.

Operators

Type

^

exponentiation

+ -

sign operations (unary)

* /

multiplication and floating-point division

\

Integer division

Mod

modulus

+ -

addition and subtraction (binary)

= <> < <= > >=

equality and relational

Категории