vel10.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 1,437 行 · 第 1/3 页
HTM
1,437 行
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel10.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="velp05.htm"><LINK REL="Previous" HREF="vel09.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel10.htm</H2><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="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><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E78" >What You'll Learn</A><LI><A HREF="#E68E79" >The Do While Loop</A><LI><A HREF="#E68E80" >The Do Until Loop</A><LI><A HREF="#E68E81" >The Other Do Loops</A><LI><A HREF="#E68E82" >The For Loop</A><LI><A HREF="#E68E83" >Homework</A><UL><LI><A HREF="#E69E67" >General Knowledge</A><LI><A HREF="#E69E68" >Write Code That...</A><LI><A HREF="#E69E69" >Find the Bug</A><LI><A HREF="#E69E70" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E14"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 5, Unit 10</B></FONT></CENTER></H1><BR><A NAME="E67E17"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Looping</B></FONT></CENTER></H2><BR><BR><A NAME="E68E78"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>The Do While Loop<BR><BR><LI>The Do Until Loop<BR><BR><LI>The Other Do Loops<BR><BR><LI>The For Loop<BR><BR></UL><P>Now you're <I>really</I> ready to write powerful programs! You've learned some controls, you've defined some variables, and you've written programs that make decisions. It's now time to learn how to write programs that perform repetitive data processing. When a computer does something over and over, the computer is said to be <I>looping</I>.<BR><P>Computers don't get bored. The primary strength of computers is their capability to loop through a series of calculations over and over very quickly. Computers can process every customer balance, calculate sales averages among many divisions, and display data for each company employee.<BR><P>This unit describes how you can add looping to Visual Basic programs so that the programs can process several data values using looping statements. Loops don't just help when you have large amounts of data to process. Loops also enable you to correct user errors and repeat certain program functions when the user requests a repeat.<BR><BR><A NAME="E68E79"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The </B><B>Do While</B><B> Loop</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Do statement supports several different loop formats. The Do While loop is perhaps the most common looping statement that you'll put in Visual Basic programs.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>block</I> consists of one or more program statements.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The Do While statement works with relational expressions just as the If statement does. Therefore, the six relational operators that you learned about in the previous lesson work as expected here. Rather than control the one-time execution of a single block of code, however, the relational expression controls the looping statements.<BR><P>The code that you've seen so far inside event procedures has been sequential code that executed one statement following another in the order that you typed the statements. Looping changes things a bit. Many lines of your programs will still execute sequentially, but a loop will cause blocks of code to repeat one or more times.<BR><P>Like the If statement that ends with an End If statement, a loop will always be a multiline statement that includes an obvious beginning and ending of the loop. Here is the format of the Do While loop:<BR><PRE><FONT COLOR="#000080">Do While (relational test) Block of one or more Visual Basic statementsLoop</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>An <I>infinite loop</I> repeats forever.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The block of code continues looping as long as the <I>relational test</I> is true. Whether you insert one or several lines of code for the block doesn't matter. It's vital, however, that the block of code somehow change a variable used in the <I>relational test</I>. The block of code keeps repeating as long as the Do While loop's <I>relational </I><I>test</I> continues to stay true. Eventually, the <I>relational test</I> must become false or your program will enter an infinite loop and the user will have to break the program's execution through an inelegant means, such as pressing the Ctrl+Break key combination.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning: </B>Even if you provide an Exit command button as you've seen used in this book's applications, the program will often ignore the user's Exit command button click if the program enters an infinite loop.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Figure 10.1 illustrates how the Do While loop works. As long as the <I>relational test</I> is true, the block of code in the body of the loop continues executing. When the <I>relational test</I> becomes false, the loop terminates. After the loop terminates, Visual Basic begins program execution at the statement following the Loop statement because Loop signals the end of the loop.<BR><P><B> <A HREF="10vel01.gif">Figure 10.1. The </B><B>Do While</B><B> loop's action continues while the </B><FONT COLOR="#FF8000"><B><I>relational test</I></B></FONT><B> is true.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>As soon as the <I>relational test</I> becomes false, the loop terminates and doesn't execute even one more time. The Do While's <I>relational test</I> appears at the top of the loop. Therefore, if the <I>relational test</I> is false the first time that the loop begins, the body of the loop will never execute.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Throughout this book, you'll see indention used for the body of the loop code. By indenting the body of the loop to the right of the loop's introduction and terminating statements, you'll make it easier to spot where the loop begins and ends.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 10.1 contains a section of an event procedure that contains a Do While loop that asks the user for an age. If the user enters an age less than 10 or more than 99, the program beeps at the error and displays another input box asking for the age. The program continues looping, asking for the age, as long as the user enters an age that's out of range.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The Do While loop continues executing a block of Visual Basic statements as long as a <I>relational test</I> is true. As soon as the <I>relational test</I> becomes false, the loop terminates.<BR><P><FONT COLOR="#000080"><B>Listing 10.1. The </B><B>Do While</B><B> loop executes as long as the </B><FONT COLOR="#FF8000"><B><I>relational test</I></B></FONT><B> is 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$("How old are you?", "Age Ask")5: ' Check for the Cancel command button6: If (StrAge = "") 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 While ((Age < 10) Or (Age > 99))13: ' The user's age is out of range14: Beep15: MsgBox "Your age must be between 10 and 99", MB_ICONEXCLAMATION, "Error!"16: StrAge = InputBox("How old are you?", "Age Ask")17: ' Check for the Cancel command button18: If (StrAge = "") Then19: End20: End If21: Age = Val(StrAge)22: Loop</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Output:</I></B></FONT> Figure 10.2 shows the message box error displayed in line 15 if the user enters an age value that's less than 10 or more than 99.<BR><P><B> <A HREF="10vel02.gif">Figure 10.2. The user sees this message box as long as the age is out of range.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Lines 1 and 2 define two variables, a string and an integer. The code uses the string variable to capture the return value from the InputBox$() function. Use a string variable so that you can test for the Cancel button because, as you learned in the previous lesson, InputBox$() returns a null string if the user presses Cancel. If the user presses Cancel, the code terminates the entire program with an End statement (lines 7 and 19). If the user enters an age (and did not press Cancel), the code converts the string age to an integer and checks to make sure that the age is within the range of 10 to 99.<BR><P>Line 12 begins a loop if the age is less than 10 or more than 99. The loop continues executing from line 13 to the end of the loop in line 22. If the age is out of range, the body of the loop executes. Line 14 beeps, thus sending an audible signal to the user that something is wrong. Line 15 displays an error message box (the one shown in Figure 10.2) and after the user presses OK at the message box, the user is once again asked for an age with an InputBox$() function shown on line 16. The loop then checks to make sure that the user didn't press Cancel (lines 17 through 20) and, if not, the code converts the string age to an integer and the loop begins once again. If the age entered in the previous pass of the loop falls within the valid age range, the program finishes (any code that exists past line 22 executes). Otherwise, the loop begins once again.<BR><P>The code contains some redundancy. For example, lines 4 and 16 contain almost the same InputBox$() function, and the same check for a Cancel command button press appears twice in the program. There are other looping statements that you'll learn about later in this chapter; those statements can help simplify this code by removing some of the redundancy.<BR><P>Perhaps the most important thing to note about the Do While loop in Listing 10.1 is that the body of the loop provides a way for the <I>relational test</I> to terminate. Line 12's <I>relational test</I> uses the Age variable that the body of the loop reassigns each time the loop's block of code executes. Therefore, assuming that the user enters a different value for the age, the loop will test against a different set of relational values in line 12 and, it is hoped, the relational test will fail (which would mean that the age is inside the range) and the program will stop looping. If the loop body did nothing with the <I>relational test</I> variable, the loop would continue forever.<BR><BR><A NAME="E68E80"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The </B><B>Do Until</B><B> Loop</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Whereas the Do While loop continues executing the body of the loop as long as the <I>relational test</I> is true, the Do Until loop executes the body of the loop as long as the <I>relational test</I> is false. The program's logic at the time of the loop determines which kind of loop works best in a given situation.<BR><P>The Do Until loop works almost exactly like the Do While except that the Do Until loop continues executing the body of the loop <I>until</I> the <I>relational test</I> is true. Like the Do While, the Do Until is a multiline looping statement that can execute a block of code that's one or more lines long.<BR><P>Here is the format of the Do Until:<BR><PRE><FONT COLOR="#000080">Do Until (relational test) Block of one or more Visual Basic statementsLoop</FONT></PRE><P>Again, keep in mind that the <I>relational test</I> must be <I>false</I> for the loop to continue. Figure 10.3 illustrates how the Do Until works.<BR><P><B> <A HREF="10vel03.gif">Figure 10.3. The </B><B>Do Until</B><B> loop's action continues while the </B><FONT COLOR="#FF8000"><B><I>relational test</I></B></FONT><B> is false.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>You can use the Do While or the Do Until for almost any loop. Listing 10.2 contains the age-checking event procedure that contains a Do Until loop. The loop ensures that the age falls between two values. As you can see, the <I>relational test</I> for the Do Until is the opposite of that used in Listing 10.1's Do While loop.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?