![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Similar situations arise in computer programming. For example, you need an answer to the question: Are X and Y both greater than aero? Heres 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)
Of course, you can use the comparison and logical operators to ask questions about object properties, too. Lets say you have two Check Box controls on a form and want to determine if one or both of them are checked. Heres 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 havent really demonstrated how they can be useful. Dont worry; thats our next topic: how to use logical expressions in conjunction with Basics decision and loop structures to control program execution.
Decision StructuresVisual Basics 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 Ill 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
|
![]() |
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. |