Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 6 Using Decision Structures 161


Using Conditional Expressions


One of the most useful tools for processing information in an event procedure is a conditional
expression. A conditional expression is a part of a complete program statement that asks
a True-or-False question about a property, a variable, or another piece of data in the program
code. For example, the conditional expression

Price < 100

evaluates to True if the Price variable contains a value that is less than 100, and it evaluates
to False if Price contains a value that is greater than or equal to 100.

You can use the following comparison operators shown in Table 6-1 within a conditional
expression.

TABLE 6-1 Visual Basic Comparison Operators
Comparison Operator Meaning
= Equal to
< > Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Table 6-2 shows some conditional expressions and their results. You’ll work with conditional
expressions several times in this chapter.

TABLE 6-2 Using Conditional Expressions
Conditional Expression Result
10 <> 20 True (10 is not equal to 20)
Score < 20 True if Score is less than 20; otherwise False
Score = Label1.Text True if the Text property of the Label1 object
contains the same value as the Score variable;
otherwise False
TextBox1.Text = "Bill" True if the word “Bill” is in the TextBox1 object;
otherwise False

If Then Decision Structures


When a conditional expression is used in a special block of statements called a decision
structure, it controls whether other statements in your program are executed and in what
order they’re executed. You can use an If... Then decision structure to evaluate a condition
Free download pdf