vel10.htm

来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 1,437 行 · 第 1/3 页

HTM
1,437
字号
<P>The loop counts up from 1 to 20 by 4s, putting each count into the variable named c and printing a message box each time. The Step value changes how Visual Basic updates the <I>CounterVar</I> each time that the loop iterates.<BR><P>If you specify a negative Step value, Visual Basic counts <I>down</I>. The following loop rings the PC's speaker five times:<BR><PRE><FONT COLOR="#000080">For i = 5 To 1 Step -1 BeepNext i</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning: </B>If you specify a negative Step value, the <I>EndVal</I> must be less than the <I>StartVal</I> or Visual Basic will execute the loop only once.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>You Can Terminate Loops Early: Sometimes, you'll be processing user input or several data values using looping statements, and an exception occurs in the data that requires an immediate termination of the loop. For example, you may be collecting sales values for a company's ten divisions inside a For loop that iterates ten times. However, the user can enter zero for a division's sales value, indicating that there is no sales data for that division. Rather than complete the loop, your program might need to quit the loop at that point because the full divisional report information can't be gathered at the time.<BR>The Exit Do and the Exit For statements automatically terminate loops. No matter what the Do loop's relational test results in, or no matter how many more iterations are left in a For loop, when Visual Basic encounters an Exit Do or Exit For statement, Visual Basic immediately quits the loop and sends execution down to the statement following the loop.<BR>Typically, an If statement triggers one of the Exit statements like this:<BR>For Divisions = 1 To 10<BR> ' Code to get a sales value<BR> If (sales = 0) Then<BR> Exit For ' Quit the loop early<BR> End If<BR> ' Process the rest of the code<BR>Next Divisions<BR>The If ensures that the Exit For executes only under one specific condition (a missing sales value). Without that specific condition triggering the Exit For, the loop cycles normally.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 10.6 contains a fairly comprehensive For loop that computes compound interest for an initial investment of $1,000.00. The code appears inside the Click event procedure for a command button named cmdInt. In case you're not familiar with compound interest, each year the amount of money invested, including interest earned so far, compounds to build more money. Each time period, normally a year, means that another year's interest must be added to the value of the investment. A For loop is perfect for calculating interest. Listing 10.6 uses five compound cycles.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The For loop repeats a block of one or more statements of Visual Basic code. Unlike the Do loops, the For loop iterates for a specified Number of times as controlled by the For statement's control values and variables.<BR><P><FONT COLOR="#000080"><B>Listing 10.6. Using a </B><B>For</B><B> loop to calculate compound interest.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub cmdInt_Click ()2: ' Use a For loop to calculate a final total3: ' investment using compound interest.4: ' Num is a loop control variable5: ' IRate is the annual interest rate6: ' Term is the Number of years in the investment7: ' InitInv is the investor's initial investment8: ' Interest is the total interest paid9: Dim IRate, Interest As Single10: Dim Term, Num As Integer11: Dim InitInv As Currency12:13: IRate = .0814: Term = 515: InitInv = 1000.0016: Interest = 1 ' Begin at one for first compound17: 18: ' Use loop to calculate total compound amount19: For Num = 1 To Term20: Interest = Interest * (1 + IRate)21: Next22:23: ' Now we have total interest,24: ' calculate the total investment25: ' at the end of N years26: lblFinalInv.Caption = InitInv * Interest27: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>This analysis focuses on the loop and not the interest calculation. The most important thing that you can do at this point is to master the For looping statement. Lines 1 through 8 contain fairly extensive remarks. The remarks contain variable descriptions so that anyone looking at the code or changing the code later will know what the variables are for.<BR><P>After the program defines all the variables in lines 9 through 11, the variables are initialized with start-up values in lines 13 through 16. If you use this event procedure, be sure to add a label named lblFinalInv to a form and add a command button to the form named cmdInt. Line 15 will seem to give you trouble as you type it unless you remember Lesson 2's description of data suffix characters. Visual Basic uses the pound sign, #, to indicate double-precision values, and Visual Basic will assume that 1000.00 is a double-precision value (I don't know why) and will convert the 1000.00 to 1000# right after you press Enter at the end of the line! Don't worry about Visual Basic's pickiness here.<BR><P>The most important part of this program appears on lines 19 through 21. Line 19 begins a For loop that iterates through each interest rate period (five of them), compounding the interest on top of the investment to date on line 20. Again, don't let the finance worry you. The calculation is less important than understanding the looping process. After the loop finishes, line 26 completes the event procedure by placing the compounded investment in the label. By the way, if you do implement this event procedure in your own application, the investment will appear in the window as a double-precision Number with several decimal places showing. You'll learn how to format the data into dollars and cents in lesson 7.<BR><BR><A NAME="E68E83"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E67"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>What is a loop?<BR><BR><LI>How many different kinds of looping statements does Visual Basic support?<BR><BR><LI>How many different Do statements does Visual Basic support?<BR><BR><LI>True or false: A <I>block</I> can consist of a single statement.<BR><BR><LI>What is an infinite loop?<BR><BR><LI>How can you utilize a loop for correcting user errors?<BR><BR><LI>How many times does the following loop execute?<BR>I = 10<BR>Do While I &gt; 1<BR> I = I - 1<BR>Loop<BR><BR><LI>How many times does the following loop execute?<BR>I = 10<BR>Do While I &gt;= 1<BR> I = I - 1<BR>Loop<BR><BR><LI>How many times does the following loop execute?<BR>I = 10<BR>Do Until I &gt; 1<BR> I = I - 1<BR>Loop<BR><BR><LI>How many times does the following loop execute?<BR>For I = 1 To 10<BR> Beep<BR>Loop<BR><BR><LI>What Step value does Visual Basic assume if you don't specify any Step value?<BR><BR><LI>Which statement, the Do or For statement, supports a loop that continues for a specified Number of times?<BR><BR><LI>Which statement, the Do or For statement, supports a loop that continues according the a relational test?<BR><BR><LI>What is the difference between a Do While and a Do Until loop?<BR><BR><LI>What is the difference between a Do While and a Do-Loop While loop?<BR><BR><LI>What is an <I>iteration</I>?<BR><BR><LI>How can you force a For loop to count down rather than up?<BR><BR><LI>If a For loop's initial starting value is greater than the ending value, what must be true for the increment value?<BR><BR><LI>What statements terminate Do and For loops early?<BR><BR></OL><BR><A NAME="E69E68"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>Write a program that assigns the value of 34 to a variable and then asks the user to guess the Number using an input box. Use a Do loop to check the user's guess and keep asking for additional guesses until the user guesses the Number.<BR><BR></OL><BR><A NAME="E69E69"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Find the Bug</B></FONT></CENTER></H4><BR><OL><LI>Larry, a fledgling programmer, wrote the following For loop that seems to be in an infinite loop. Can you spot the problem?<BR>For i = 1 To 25<BR> Total = Total * I<BR> i = i - 1<BR>Next i<BR><BR><LI>Kim wants her For loop to loop for 100 iterations but she's having trouble. Tell Kim what's wrong with the following attempt:<BR>For I = 100 To 1 Step 1<BR><BR></OL><BR><A NAME="E69E70"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><P>A <I>nested loop</I> is a loop within a loop. The outer loop determines how many times the inner loop executes. See whether you can determine how many times the following code beeps the user.<BR>For i = 1 To 5 ' The outer loop<BR> For j = 1 To 3 ' The inner loop<BR> Beep<BR> Next j ' Inner loop completes before<BR>Next i ' outer loop iterates again<BR><P ALIGN=LEFT><A HREF="vel09.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="velp05.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?