Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 6 Using Decision Structures 167


Your code has prevented an unauthorized user from using the program, and you’ve
learned a useful skill related to controlling input from the user.


  1. Exit the program.


Using Logical Operators in Conditional Expressions

You can test more than one conditional expression in If... Then and ElseIf clauses if you want
to include more than one selection criterion in your decision structure. The extra conditions are
linked by using one or more of the logical operators listed in Table 6-3.

TABLE 6-3 Visual Basic Logical Operators
Logical Operator Meaning
And If both conditional expressions are True, then the result is True.
Or If either conditional expression is True, then the result is True.
Not If the conditional expression is False, then the result is True. If the
conditional expression is True, then the result is False.
Xor If one and only one of the conditional expressions is True, then the result
is True. If both are True or both are False, then the result is False. (Xor
stands for exclusive Or .)

Tip When your program evaluates a complex expression that mixes different operator types, it
evaluates mathematical operators first, comparison operators second, and logical operators third.

Table 6-4 lists some examples of the logical operators at work. In the expressions, it is
assumed that the Vehicle string variable contains the value “Bike,” and the integer variable
Price contains the value 200.

TABLE 6-4 Using Logical Expressions
Logical Expression Result
Vehicle = "Bike" And Price < 300 True (both conditions are True)
Vehicle = "Car" Or Price < 500 True (one condition is True)
Not Price < 100 True (condition is False)
Vehicle = "Bike" Xor Price < 300 False (both conditions are True)

In the following exercise, you’ll modify the My User Validation program to prompt the user
for a personal identification number (PIN) during the validation process. To do this, you will
add a second text box to get the PIN from the user, and then modify the If... Then clause
in the decision structure so that it uses the And operator to verify the PIN.
Free download pdf