6.4. Examples Using the For...Next StatementThe following examples demonstrate different ways of varying the control variable in a For...Next statement. In each case, we write the appropriate For...Next header.
Application: Summing the Even Integers from 2 to 100The next two examples demonstrate simple applications of the For...Next repetition statement. The program in Fig. 6.5 uses the For...Next statement to sum the even integers from 2 to 100. Remember that to use the MessageBox class you must add a reference to System.Windows.Forms.dll, as explained in Section 3.8. Figure 6.5. For...Next statement used for summation.
Message Dialog Buttons and IconsThe version of method MessageBox.Show called in Fig. 6.5 (lines 1416) is different from the version discussed in earlier examples in that it takes four arguments instead of two. The dialog in Fig. 6.5 is labeled to emphasize the effects of the four arguments. The first two arguments are Strings displayed in the dialog and the dialog's title bar, respectively. The third argument indicates which button(s) to display, and the fourth argument indicates an icon that appears to the left of the message. The documentation provided with the IDE includes the complete listing of MessageBoxButtons and MessageBoxIcon constants. The message dialog icon options are described in Fig. 6.6; the message dialog button options are described in Fig. 6.7, including how to display multiple buttons. Note that there are multiple icon constants that display the same icon. Sometimes using a different constant name improves program readability. For instance, it may make more sense to use the constant MessageBoxIcon.Warning to indicate that a warning is being displayed, while the constant MessageBoxIcon.Exclamation would be used to indicate that an unexpected result occurred. Both constants display the same icon, but the names used in the code are clearer based on context.
Application: Compound Interest CalculationsThe next example computes compound interest using the For...Next statement. Consider the following problem statement:
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. 6.8. Figure 6.8. For...Next statement used to calculate compound interest.
Lines 78 declare two Decimal variables. Type Decimal is used for monetary calculations. Line 9 declares rate as type Double. Variable principal is initialized to 1000.00 and rate is initialized to 0.05, (i.e., 5%). Variable output (line 12) will be used to store the output that we will eventually display in a message dialog. The For...Next statement (lines 1519) iterates 10 times, varying control variable year from 1 to 10 in increments of 1. Line 16 performs the calculation from the problem statement a = p (1 + r)n where a is the amount, p is the principal, r is the rate and n is the year. Formatting Currency OutputLines 1718 append additional text to the end of String output. The text includes the current year value, a tab character (vbTab) to position to the second column, the result of the method call String.Format("{0:C}", amount) and, finally, a line feed (vbCrLf) to start the next output on the next line. Method Format of class String takes a formatted string and the values to be formatted as arguments, and returns the resulting text with the formatting applied. The first argument passed to Format is the format string. In Chapter 5, we used the format string "{0:F}" to print a floating-point number with two digits after the decimal. Line 18 uses the format string "{0:C}". The C (for "currency") format specifier indicates that its corresponding value (amount) should be displayed in monetary formatwith a dollar sign to the left and commas in the proper locations. For example, the value 1334.50, when formatted using the C format specifier, will appear as "$1,334.50." Type Decimal vs. Type DoubleVariables amount and principal are declared as type Decimal. We do this because we are dealing with fractional parts of dollars and need a type that allows precise calculations with monetary amountsSingle and Double, because they only approximate values, do not. Using floating-point types, such as Single or Double, to represent dollar amounts (assuming that dollar amounts are displayed with two digits to the right of the decimal point) can cause errors. For example, two Double dollar amounts stored in the machine could be 14.234 (normally rounded to 14.23) and 18.673 (normally rounded to 18.67). When these amounts are added together, they produce the internal sum 32.907, which normally rounds to 32.91. Thus, the output could appear as 14.23 + 18.67 _______ 32.91 but a person adding the individual numbers as displayed would expect the sum to be 32.90. Therefore, it is inappropriate to use Single or Double for dollar amounts. You have been warned! Error-Prevention Tip 6.3
Variable rate, of type Double, is used in the calculation 1 + rate, which appears as the left 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 6.1
|