![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
expression, you force operators inside parentheses to be evaluated first regardless of precedence. If you write A + B * C the precedence rules will cause the multiplication to be performed first, and the result will not be what you want. If, however, you write the expression like this (A + B) * C the parentheses force the addition to be performed first, and the expression evaluates properly. You can use as many parentheses in an expression as you need, as long as they always come in pairs; each left parenthesis must have a matching right parenthesis. If you create an expression with an unmatched parenthesis, Visual Basic displays an error message when you try to move the editing cursor off the line. When parentheses are nested (one set inside another set), execution starts with the innermost set and proceeds outward. You can use parentheses in an expression even when they are not needed to modify the order of operator precedence. Particularly with long, complex expressions, parentheses can help make the expression easier to read and understand. A Need To ControlYouve seen that Basic code consists of a series of statements, one to a line. When a chunk of Basic code executes, the normal execution order begins with the first statement and then executes all the statements in order, top-to-bottom. Sometimes, however, this just wont do. One of the most powerful Basic programming tools available to you is the ability to control program executionto determine which Basic statements execute, when they execute, and how many times they execute. Before I show you how to do this, however, you need to know about logical expressions and Visual Basics comparison and logical operators. Logical ExpressionsComputer programs often need to deal with yes/no questions. When a question or an ex-pression has only these two possible outcomes, it is called a logical expression. In computer programming, the two possible outcomes are referred to as True and False. As an example, consider the question, Is the value stored in the variable X larger than the value stored in the variable Y? Clearly, either it is (answer = True) or it is not (answer = False). Because computers use numbers for everything, it has become standard practice to use 0 for False and -1 for True. Now lets take a look at how to construct and manipulate logical expressions. Visual Basic has a special data type, Boolean, designed specifically to hold logical values. Such variables are sometimes referred to as flags. For example, if your program needed a way to track whether the current data has been saved to disk, you could declare a flag variable as follows: Dim DataSaved As Boolean When the user saves the data to disk, assign a value like this: DataSaved = True Likewise, when the data has been modified since the last save: DataSaved = False Even though Visual Basic uses numbers internally to represent the Boolean values True and False, you can (and should) use the predefined identifiers True and False in your code. Comparison Operators Visual Basic provides a number of operators you can use to construct logical expressions by asking questions about the data in your programs. Specifically, these comparison operators perform comparisons between expressions, returning a value of True or False, depending on the result of the comparison. Table 4.5 lists the comparison operators. We can see that if X is equal to 10 and Y is equal to 5, then the expression X < Y will evaluate as False and X <> Y will evaluate as True. Assuming both Q and Z have been declared as type Integer, we can use the numerical value of a logical expression like this Q = X < Y Z = X <> Y which would result in the variable Q having the value 0 and Z having the value -1. More often, however, logical expressions are used in program control, as youll see soon. But first, we need to take a look at how to combine two or more logical expressions to arrive at a single True/False answer.
Logical Operators The logical operators work to combine two or more logical expressions into a single True or False answer. Why would we want to do this? Heres an example from everyday life. You receive a call inviting you to join a group of friends for dinner. If Mary is coming along, youd like to go, because youre a bit sweet on her; otherwise, youll pass. This is a single logical condition, and easy to understand. Now, assume for a moment that you also like Helen; if shes coming with the group, youd also like to go. But if both Mary and Helen are coming, the situation will get a bit too complicated, so you again will pass. In this situation, we have two True/False questions (Is Mary coming? Is Helen coming?) that you need to combine in some way before answering the True/False question: Are you going?
|
![]() |
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. |