vel10.htm

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

HTM
1,437
字号
<NOTE><B>Note: </B>Use the loop that makes for the cleanest and clearest <I>relational test</I>. Sometimes, the logic makes the Do While clearer, whereas other loops seem to work better when you set them up with the Do Until loop.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The Do Until loop continues executing a block of Visual Basic statements as long as a <I>relational test</I> is false. As soon as the <I>relational test</I> becomes true (the loop is said to <I>Do a loop until the condition becomes </I><I>false</I>), the loop terminates and the program continues on the line that follows the closing Loop statement.<BR><P><FONT COLOR="#000080"><B>Listing 10.2. The </B><B>Do Until</B><B> loops until the </B><FONT COLOR="#FF8000"><B><I>relational test</I></B></FONT><B> becomes true.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Dim StrAge As String2: Dim Age As Integer3: ' Get the age in a string variable4: StrAge = InputBox$(&quot;How old are you?&quot;, &quot;Age Ask&quot;)5: ' Check for the Cancel command button6: If (StrAge = &quot;&quot;) Then7: End8: End If9: ' Cancel was not pressed, so convert Age to integer10: Age = Val(StrAge)11: ' Loop if the age is not in the correct range12: Do Until ((Age &gt;= 10) And (Age &lt;= 99))13: ' The user's age is out of range14: Beep15: MsgBox &quot;Your age must be between 10 and 99&quot;, MB_ICONEXCLAMATION, &quot;Error!&quot;16: StrAge = InputBox(&quot;How old are you?&quot;, &quot;Age Ask&quot;)17: ' Check for the Cancel command button18: If (StrAge = &quot;&quot;) Then19: End20: End If21: Age = Val(StrAge)22: Loop</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 12 is the only line that marks the difference between Listing 10.1 and 10.2. The age must now fall within the valid range for the loop to terminate.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>There is really no advantage of using Do While or Do Until. Use whichever one seems to flow the best for any given application.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E68E81"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The Other </B><B>Do</B><B> Loops</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>There is another pair of Do loops that works almost exactly like the two previous sections' loops. Do-Loop While and the Do-Loop Until look very much like their counterparts that you learned earlier. Nevertheless, these new loop formats check their <I>relational test</I>s at the bottom of the loop rather than at the top.<BR><P>If a loop begins with a single Do statement, the loop ends with either Loop While or Loop Until. Here is the format of the Do-Loop While:<BR><PRE><FONT COLOR="#000080">Do <I>Block of one or more Visual Basic statements</I>Loop Until (relational test)</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>The dash in Do-Loop While serves to remind you that the body of the loop comes before the Loop While statement. The dash in the Do-Loop Until performs the same purpose.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>That Do looks lonely by itself, doesn't it? Figure 10.4 illustrates the flow of the Do-Loop While loop's execution.<BR><P><B> <A HREF="10vel04.gif">Figure 10.4. The </B><B>Do</B><FONT COLOR="#FF8000"><B><I>-</I></B></FONT><B>Loop While</B><B> loop doesn't check for the </B><FONT COLOR="#FF8000"><B><I>relational test</I></B></FONT><B> until the </B><B>bottom of the loop body.</A></B><BR><P>Notice that the Do-Loop While loop's <I>relational test</I> appears at the bottom of the loop instead of at the top of the loop. You'll use the Do-Loop While loop when you want the body of the loop to execute at <I>least one </I><I>time</I>. Often, by placing the <I>relational test</I> at the bottom of the loop, you can eliminate redundant code that otherwise might be required if you used Do While.<BR><P>To complete the loop statements, Visual Basic also supports a Do-Loop Until statement. Like the Do-Loop While, the Do-Loop Until statement tests the <I>relational test</I> at the bottom of the loop. Therefore, the body of the loop executes at least once no matter what the <I>relational test</I> turns out to be. The loop continues as long as the <I>relational test</I> remains <I>false</I>. Figure 10.5 illustrates the action of the Do-Loop Until.<BR><P><B> <A HREF="10vel05.gif">Figure 10.5. The </B><B>Do</B><FONT COLOR="#FF8000"><B><I>-</I></B></FONT><B>Loop Until</B><B> loop checks for a false </B><FONT COLOR="#FF8000"><B><I>relational test</I></B></FONT><B> at the bottom of </B><B>the loop body.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 10.3 contains the age-checking event procedure that's much shorter than the previous versions. The <I>relational test</I> appears at the bottom of the loop, so the extra InputBox$() function call is not needed.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The <I>relational test</I> appears at the bottom of the loop if you use the Do-Loop While loop statement. The body of the loop always executes at least once. The body of the loop executes more than once as long as the <I>relational test</I> stays true. There is a corresponding Do-Loop Until statement that checks for a false condition at the bottom of the loop's body.<BR><P><FONT COLOR="#000080"><B>Listing 10.3. Use the </B><B>Do</B><B>-</B><B>Loop While</B><B> to check the relation at the bottom of the loop.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Dim StrAge As String2: Dim Age As Integer3: Do4: StrAge = InputBox(&quot;How old are you?&quot;, &quot;Age Ask&quot;)5: ' Check for the Cancel command button6: If (StrAge = &quot;&quot;) Then7: End8: End If9: Age = Val(StrAge)10: If ((Age &lt; 10) Or (Age &gt; 99)) Then11: ' The user's age is out of range12: Beep13: MsgBox &quot;Your age must be between 10 and 99&quot;, MB_ICONEXCLAMATION, &quot;Error!&quot;14: End If15: Loop While ((Age &lt; 10) Or (Age &gt; 99))</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>The loop begins almost immediately in line 3. The loop's body will <I>always</I> execute at least once, so the InputBox$() appears right inside the loop. By placing the InputBox$() function inside the loop, you eliminate the need to put this function in the code twice (once before the loop and once inside the loop, as was necessary using the previous looping statements in Listings 10.1 and 10.2).<BR><P>Line 10 must check to make sure that the InputBox$() value is in or out of the age range so that the message box error can be displayed.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>In this simple application of the looping statements that you've seen here, the Do-While loop required less code than the Do While and Do Until loops. By changing line 15's <I>relational test</I>, a Do Until would also work. These last two loops will not, in every case, produce less code. The logic of the application determines which loop works best.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E68E82"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The </B><B>For</B><B> Loop</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The For loop (sometimes called the For-Next loop) also creates a loop. Unlike the Do loops, however, the For loop repeats for a specified number of times. The format of the For loop looks a little more daunting than the Do loops, but after you master the format, you'll have little trouble implementing For loops when your code needs to repeat a section of code for a specified number of times.<BR><P>There isn't one correct loop to use in all situations. The For statement provides the mechanism for the fifth Visual Basic loop construction that you'll learn. A For loop always begins with the For statement and ends with the Next statement. Here is the format of the For loop:<BR><PRE><FONT COLOR="#000080">For CounterVar = StartVal To EndVal [Step IncrementVal] Block of one or more Visual Basic statementsNext CounterVar</FONT></PRE><P>The loop in Listing 10.4 computes the total of the numbers from 1 to 10.<BR><P><FONT COLOR="#000080"><B>Listing 10.4. Add the numbers from 1 to 10.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sum = 02: For Number = 1 To 103: Sum = Sum + Number4: Next Number</FONT></PRE><P>Number is the <I>CounterVar</I> in the For's format (line 2). The <I>CounterVar</I> must be a variable. 1 is the <I>StartVal</I> (line 2). The <I>StartVal</I> can be either a Number, expression, or variable. 10 is the <I>EndVal</I> (still in line 2). The <I>EndVal</I> can be either a Number, expression, or variable. There is no Step specified here. In the For statement's format, the Step <I>IncrementVal</I> is optional. If you don't specify a Step value, Visual Basic assumes a Step value of 1. Therefore, both of the following For statements are exactly the same:<BR><BR><PRE><FONT COLOR="#000080">For Number = 1 To 10</FONT></PRE><P>and<BR><BR><PRE><FONT COLOR="#000080">For Number = 1 To 10 Step 1</FONT></PRE><P>Listing 10.4's summing For loop initially assigns to the <I>CounterVar</I> the <I>StartVal</I> in line 2. Therefore, Number is assigned 1 at the top of the loop. Visual Basic then executes the body of the loop using the value of 1 for Number. With Number being equal to 1, line 3 works as follows the first time through the loop:<BR><BR><PRE><FONT COLOR="#000080">Sum = Sum + 1</FONT></PRE><P>When Visual Basic executes the Next Number statement, Visual Basic returns to the top of the loop (the For statement), adds the Step value of 1 to Number, and continues the loop again using 2 as Number in the loop's body. Therefore, the second time through the loop, line 3 works as follows:<BR><BR><PRE><FONT COLOR="#000080">Sum = Sum + 2</FONT></PRE><P>The loop continues, adding the default Step value of 1 to Number each time that the loop executes. When Number becomes 10 (the <I>EndVal</I>), the loop finishes and the statement following the Next statement continues.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Remember, the For loop terminates when the <I>CounterVar</I> becomes larger than the <I>EndVal</I>. There's an exception to this: If you code a negative Step value, the loop terminates when the <I>CounterVar</I> becomes smaller than the <I>EndVal,</I> as you'll see a little later in this section.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>You don't need the For statement to sum the values of 1 through 10. You could code one long assignment statement like this:<BR><BR><PRE><FONT COLOR="#000080">Sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10</FONT></PRE><P>You could also code back-to-back assignment statements like this:<BR><PRE><FONT COLOR="#000080">Sum = Sum + 1Sum = Sum + 2Sum = Sum + 3Sum = Sum + 4Sum = Sum + 5Sum = Sum + 6Sum = Sum + 7Sum = Sum + 8Sum = Sum + 9Sum = Sum + 10</FONT></PRE><P>Neither of these approaches is extremely difficult, but what if you needed to add together the first one hundred integer Numbers? The previous assignments could become tedious indeed, but the For loop to add the first one hundred integers is just as easy to code as for the first 10 integers, as Listing 10.5 demonstrates.<BR><P><FONT COLOR="#000080"><B>Listing 10.5. Add the Numbers from 1 to 100.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sum = 02: For Number = 1 To 100 ' Only this line changes3: Sum = Sum + Number4: Next Number</FONT></PRE><P>The following loop displays five message boxes:<BR><PRE><FONT COLOR="#000080">For c = 1 To 20 Step 4 MsgBox &quot;This is a message box&quot;Next c</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A loop <I>iteration</I> is one full loop cycle.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE>

⌨️ 快捷键说明

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