home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Similar situations arise in computer programming. For example, you need an answer to the question: “Are X and Y both greater than aero?” Here’s where the logical operators come in, letting you combine and manipulate logical expressions to get the answer you need. The six logical operators (shown in Table 4.6) are each designated by a keyword. As you review this table, assume that X and Y are both logical expressions.

Now we can cast the earlier question using the comparison operators and the logical operators. The expression

(X > 0) And (Y > 0)

will evaluate as True if, and only if, both X and Y are greater than zero. Likewise, if you need to know whether at least one of these two variables is greater than zero, you would write:

(X > 0) Or (Y > 0)
Table 4.6 Logical operators.

Operator Example Evaluation
And X And Y True if both X and Y are True; False otherwise
Or X Or Y True if X or Y, or both of them, are True; False only if both X and Y are False
Xor (exclusive Or) X Xor Y True if X and Y are different (one True and the other False); False if both are True or both are False
Eqv (Equivalence) X Eqv Y True if X and Y are the same (both True or both False); False otherwise
Imp (Implication) X Imp Y False only if X is True and Y is False; True otherwise
Not Not X True if X is False, False if X is True

Of course, you can use the comparison and logical operators to ask questions about object properties, too. Let’s say you have two Check Box controls on a form and want to determine if one or both of them are checked. Here’s the expression to do so:

(Check1.Value = True) Or (Check2.Value = True)

I have been talking about comparison and logical operators for a while now, but I haven’t really demonstrated how they can be useful. Don’t worry; that’s our next topic: how to use logical expressions in conjunction with Basic’s decision and loop structures to control program execution.


TIP:  Basic Code Line Wrapping

Some lines of Basic code can be rather long. While the Visual Basic editor is able to handle long lines, it can be a nuisance to have your lines of code running off the right edge of the editor window where you can’t see them without scrolling. You can avoid this problem by using the line continuation character to break a long line of code into two or more lines. All you need to do is type a space followed by an underscore, then press Enter. All code on the new line will be treated by Visual Basic as part of the first line. The only restriction is that you cannot place the line continuation character in a literal string within double quotation marks.


Decision Structures

Visual Basic’s decision structures control program execution based on whether certain logical conditions are met. In other words, program statements are either executed or not executed, based on the evaluation of logical expressions. These logical expressions typically evaluate the state of program data or user input, so the execution of the program can be controlled according to the specific needs of the application.

If...Then...Else

The If structure executes a block of one or more statements only if a specified logical expression evaluates as True. Optionally, you can include a second block of statements that is executed only if the logical expression is False. An If structure has the following form (in these examples, X stands for any logical expression):

If X Then
   ...
   Statements to be executed if X is TRUE go here.
   ...
Else
   ...
   Statements to be executed if X is FALSE go here.
   ...
End If

The Else keyword and the block of statements between it and the End If keyword are optional. If no statements are to be executed when X is False, you can write as follows:

If X Then
   ...
   Statements to be executed if X is TRUE go here.
   ...
End If

If your blocks of statements are only single statements, you can use the concise single-line form of the If structure:

If X then Statement1 Else Statement2

For more involved situations, you can include the ElseIf keyword to create what are effectively nested If structures:

If X Then
   ...
   Statements to be executed if X is TRUE go here.
   ...
ElseIf Y Then
   ...
   Statements to be executed if Y is TRUE go here.
   ...
Else
   ...
   Statements to be executed if both X and Y are FALSE go here.
   ...
End If

You can have as many ElseIf statements as you like. Keep in mind, however, that, at most, one of the blocks of statements in an If structure will be executed. In the preceding example, if both X and Y are True, only the statements associated with the X condition are executed. The rule is that only the statements associated with the first True condition are executed. Note, however, that for situations that would require more than one or two ElseIf clauses, you are usually better off using the Select Case structure, which I’ll cover next.

You might have noticed the indentation style in the previous code samples; within each block, all statements are indented with respect to the statements that mark the beginning and the end of the block. This is not required by Visual Basic, but in my opinion it makes the code more readable.

Select Case

You will find that the Select Case structure is more appropriate than the If structure when you have more than a couple of conditions to be tested:

Select Case TestExpression
   Case Comparison1
      ...
      Block1
      ...
   Case Comparison2
      ...
      Block2
      ...
   Case Else
      ...
      ElseBlock
      ...
End Select


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.