- Introduction
- Control Structures
- If/Then Selection Structure
- If/Then/Else Selection Structure
- While Repetition Structure
- DoWhile/Loop Repetition Structure
- DoUntil/Loop Repetition Structure
- Do/LoopWhile Repetition Structure
- Do/LoopUntil Repetition Structure
- Assignment Operators
- For/Next Repetition Structure
- Example: Using the For/Next Structure to Compute Compound Interest
- SelectCase Multiple-Selection Structure
- Using the Exit Keyword in a Repetition Structure
- Logical Operators
- Introduction to Windows Application Programming
- Summary
3.12 Example: Using the For/Next Structure to Compute Compound Interest
The next example computes compound interest, using the For/Next structure. Consider the following problem:
A person invests $1000.00 in a savings account that yields 5% interest. Assuming that all interest is left on deposit, calculate and print the amount of money in the account at the end of each year, over a period of 10 years. To determine these amounts, use the following formula:
a = p (1 + r) n where p is the original amount invested (i.e., the principal) r is the annual interest rate (e.g., .05 stands for 5%) n is the number of years a is the amount on deposit at the end of the nth year.
This problem involves a loop that performs the indicated calculation for each of the 10 years that the money remains on deposit. The solution is shown in Fig. 3.4.
Line 9 declares two Decimal variables. Type Decimal is used for monetary calculations. Line 10 declares rate as type Double, and lines 1415 initialize principal to 1000.00 and rate to 0.05 (i.e., 5%).
The body of the For/Next structure is executed 10 times, varying control variable year from 1 to 10 in increments of 1. Line 21 performs the calculation from statement of the problem, that is,
a = p (1 + r) n
where a is amount, p is principal, r is rate and n is year.
Fig. 3.4 For/Next structure used to calculate compound interest.
Lines 2223 append additional text to the end of Stringoutput. The text includes the current value of year, a tab character (vbTab) to position the cursor to the second column, the result of the method call String.Format("{0:C}",amount) and, finally, a newline character (vbCrLf) to start the next output on the next line. The first argument passed to Format is the format string. Previously in the text, we have seen Strings containing {0}, {1} and so on, where the digit within the braces indicates the argument being displayed. In more complicated format strings, such as "{0:C}", the first digit (0) serves the same purpose. The information specified after the colon (:) is called the formatting code. The C (for "currency") formatting code indicates that its corresponding argument (amount) should be displayed in monetary format. Figure 3.5 explains several formatting codes; a complete list can be found in the MSDN documentation "Standard Numeric Format Strings." All formatting codes are case insensitive. Note that format codes D and X can be used only with integer values. [Note: Method Format uses .NET's string formatting codes to represent numeric and monetary values according to the user's localization settings.1 For example, in the United States, an amount would be expressed in dollars (e.g., $634,307.08), while in Malaysia, the amount would be expressed in ringgits (e.g., R634.307,08).]
Fig. 3.5 Formatting codes for Strings.
Variables amount and principal are of type Decimal. We use this type because we are dealing with fractional parts of dollars and need a type that allows precise calculations with monetary amounts; Single and Double do not.
Good Programming Practice 3.1
Do not use variables of type Single or Double to perform precise monetary calculations. cause errors that result in incorrect monetary values. Use the data type Decimal for monetary calculations. 3.1
Variable rate is also of type Double, because it is used in the calculation 1.0 + rate, which appears as the right operand of the exponentiation operator. In fact, this calculation produces the same result each time through the loop, so performing the calculation in the body of the For/Next loop is wasteful.
Performance Tip 3.1
Avoid placing inside a loop the calculation of an expression whose value does not change each time through the loop. Such an expression should be evaluated only once, prior to the loop.
The FCL and Visual Basic .NET allow multiple procedures with the same name to be defined. Programmers often use this technique, known as procedure overloading, to define several procedures that perform similar actions, but take a different set of arguments. One example of the use of procedure overloading is method MessageBox.Show. In a previous example, we have provided only one argument to this methoda String to be displayed in a message dialog. Figure 3.4 uses a version of method MessageBox.Show (lines 2728) that takes four arguments. The dialog in the output of Fig. 3.4 illustrates the four arguments. The first argument is the message to display. The second argument is the string to display in the dialog's title bar. The third argument is a value indicating which button(s) to display. The fourth argument indicates which icon to display to the left of the message. Figures 3.6 and 3.7 provide a listing of the MessageBoxButtons and MessageBoxIcon choices. Information about other versions of method MessageBox.Show can be found in the MSDN documentation provided with Visual Studio .NET. We discuss procedure overloading in more detail in Chapter 4, Procedures and Arrays.
Fig. 3.6 Message-dialog icon constants.
Fig. 3.7 Message-dialog button constants.