| Chapter 3- Data Types and Operators | Boolean Expressions Page 2 3 4 |
| The If Statement Page 2 3 4 5 6 7 8 9 | |
| Arithmetic Operations Page 2 3 4 5 6 | Boolean Operators and Nested If Statements Page 2 3 4 5 6 7 |
| Events and Sequential Processing Page 2 3 4 5 | More Examples Page 2 3 4 5 6 7 8 9 10 11 12 |
| Datatypes and Conversions Page 2 3 4 5 6 7 | Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10 |
| Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Exercises Page 2 3 4 5 6 7 8 |
| Chapter 4- Selection Statements | Review Questions |
| Introduction | |
4-2: Boolean Expressions
Any decision whether or not to pursue a course of action, or whether to adopt one course of action instead of another, is essentially the result of a true/false consideration. In your personal life you might simply ask "Is this what I want to do?" or "Do I have enough money to do this?" etc. In a computer program you also set up an expression that results in a true/false answer!
Often the expression involves comparing two values, as is often the case in real life. For example, "Do I have enough money to buy this?" is in fact the comparison of "how much money you have" and "the price", which we could write as "Is my money greater than the price?" Written in the language of a computer program this is often expressed as
myMoney > priceOfItem
which results in a true or a false value depending on the values of the two variables.
This is called a boolean expression and the operator ">" is called a comparison operator. A boolean expression has one of two values - true or false. Of course there are many types of comparisons you might want to perform in order to make such a decision. Are two things equal? is one less than or equal to another? are two things not equal? etc. Table 4-1 contains a list of the comparison operators in Visual Basic.
Table 4-1: A partial list of Comparison Operators.
| Operator | Description |
|---|---|
| < | less than |
| <= | less than or equal to |
| > | greater than |
| >= | greater than or equal to |
| = | equal to |
| <> | not equal to |
In general you must take care when comparing values that the data types of those values are the same. If one value is a string and the other a number Visual Basic will attempt to convert the string to a number in order to make the comparison. If the string cannot be converted to a number a InvalidCastException error occurs. Two values that are different numeric data types (eg Double and Integer) will often compare with anticipated results. For example, the expression 2 = 2.3 will have the value false, as expected. However, in comparing Single and Double values the Single value is converted to a Double with usually an imprecise result. In fact testing for equality between floating point numbers is to be avoided entirely because of round-off errors, which mean that numbers that you expect to be equal are not exactly equal in the binary representation used by the computer.