Chapter 3 Homework Solutions:

Chapter 3-Variables, Constants, and Calculations


1.          Name and give the purpose of five types of data available in Visual Basic.
The Boolean data type is used for True or False values. The Byte data type is used to represent a single ASCII character (code 0 to 255). Currency is used for decimal fractions, such as dollars and cents. Date is for 8 character dates. Double is used with double-precision floating point numbers with 14 digits of accuracy. Integer is used with whole numbers in the range -32,768 to 32,767. The Long data type is for larger whole numbers. Single is for single-precision floating point numbers with 6 digits of accuracy. String is used for alphanumeric data: letters, digits, and other characters. Variant converts from one type to another, as needed.

2.          What does declaring a variable mean?
Declaration statements establish your project's variables and constants, give them names, and specify the type of data they will hold. When a variable or a named constant is declared, Visual Basic reserves an area of memory and assigns it a name, called an identifier. Although there are several ways to declare a variable, the most commonly used statement is the Dim statement.

3.          What effect does the location of a Dim statement have on the variable it declares?
A variable may exist and be visible for an entire project (global), for only one form (module-level), or for only one procedure (local).  The visibility of a variable is referred to as its scope. The scope is said to be global, module level, or local. You declare the scope of a variable by choosing the location to place the Dim statement. A global variable may be used in all procedures of a project and will be discussed in chapter 6. Module-level variables are declared in the General Declarations section of a form and are accessible from all procedures of a form. A local variable may be used only within the procedure in which it is declared.

4.          Explain the difference between a constant and a variable.
Variables and constants are temporary memory locations that have a name (called an identifier), a data type, and a scope. The value stored in a variable can be changed during the execution of the project; the values stored in constants cannot change.

5.          What is the purpose of the Val function?
The Val function is used to convert text data into a numeric value.  The expression you wish to convert can be the property of a control, a variable, or a constant.

6.          Explain the order of precedence of operators for calculations.
The order of precedence in arithmetic expressions, from highest to lowest is:
1. exponentiation, 2. multiplication and division, and 3. addition and subtraction.
The process of evaluation of an expression is done in this order:
1. All operations within parentheses are evaluated first. If there are multiple operations within the parentheses, the operations are performed according to the rules of precedence.
2. All exponentiation (^) is done. If there are multiple exponentiation operations, they are performed from left to right.
3. All multiplication (*) and division (/) is done. Multiple operations will be performed from left to right.
4. All addition (+) and subtraction (-) is done from left to right.

7.          Which statement(s) can be used to declare a variable?
Variables are declared using the Dim (dimension) statement., The location of the statement determines the scope of the variable. Declaring the variable’s data type is optional, in which case the variable's type defaults to variant. It's best to always declare the type, even when you intend to use variants.

8.          Explain how to make an interest rate stored in sngRate display as a percentage with three decimal digits?
 The interest rate will be formatted with the code, FormatPercent(sngRate,3).  Then, this formatted value will usually be assigned to the property of a control for display.

9.          Should formatting functions be included for all captions and text display in a program? Justify your answer.
The format functions are very useful when you want to control the way the output will look. In most cases, you will use formats for numeric data to control the number of decimal positions.  Formats are not generally needed for integer values. It is not necessary to assign a format function to every caption and text that is displayed because the defaults for displaying these strings and numeric values are perfectly suitable in many cases.