vel08.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 1,958 行 · 第 1/3 页
HTM
1,958 行
<HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type:</I></B></FONT> The fastest way to learn Select Case is to see an example of it. Listing 8.4 contains a Select Case version of the child grade assignments shown in Listing 8.3. Select Case organizes the multiple-choice selections into a more manageable format.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The Select Case statement is a good substitute for long, nested If-Else conditions when one of several choices are possible. You set up your Visual Basic program to execute one set of Visual Basic statements from a list of statements inside Select Case.<BR><P><FONT COLOR="#000080"><B>Listing 8.4. Using </B><B>Select Case</B><B> to simplify complex nested </B><B>If</B><B>-</B><B>Else</B><B> statements.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Select Case Age2: Case 5: lblTitle.Text = "Kindergarten"3: Case 6: lblTitle.Text = "1st Grade"4: Case 7: lblTitle.Text = "2nd Grade"5: Case 8: lblTitle.Text = "3rd Grade"6: Case 9: lblTitle.Text = "4th Grade"7: Case 10: lblTitle.Text = "5th Grade"8: Case 11: lblTitle.Text = "6th Grade"9: Case Else: lblTitle.Text = "Advanced"10: End Select</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>If the Age variable holds the value 5, the label is assigned "Kindergarten" in line 2. If the Age variable holds the value 6, the label is assigned "1st Grade" in line 3. The logic continues through line 9. If Age holds a value that does not fall within the range of 5 through 11, line 9 assigns "Advanced" to the label.<BR><P>The body of each Case can consist of more than one statement, just as the body of an If or If-Else can consist of more than one statement. Visual Basic executes all the statements for any given Case match until the next Case is reached. Once Visual Basic executes a matching Case value, it skips the remaining Case statements and continues with the code that follows the End Select statement.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Programmers often trigger the execution of complete procedures, such as event procedures, from within a Case statement. As you will learn in Lesson 8, instead of putting several statements in the body of an If-Else or a Case, you can execute a procedure that contains all the statements that execute when a given condition is true.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E68E67"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Two Additional </B><FONT COLOR="#FF8000"><B><I>Select Case</I></B></FONT><B> Formats</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Visual Basic’s Select Case is one of the most powerful selection statements in any programming language. Pascal, C, and C++—all popular programming languages—each contain statements that act like Visual Basic’s Select Case, but Select Case offers two additional powerful formats that enable you to modify the way Case matches are made.<BR><P>The two additional formats differ only slightly from the standard Select Case that you learned in the previous lesson. They enable you to extend the power of Select Case so that Visual Basic can make Case matches on both relational tests and on ranges of values. Here is the first additional format:<BR><PRE><FONT COLOR="#000080">Select Case <I>Expression</I>Case Is <I>relation</I>:<I> One or more Visual Basic statements</I>Case Is <I>relation</I>:<I> One or more Visual Basic statements</I>[Case Is <I>relation</I>:<I> One or more Visual Basic statements</I>][Case Else:<I> One or more Visual Basic statements</I>]End Select</FONT></PRE><P><I>relation</I> can be whatever relational test you want to perform against <I>Expression</I> at the top of the Select Case. The standard Select Case statement, discussed in the previous section, compared the <I>Expression</I> value against an exact Case match. When you use the relational Is Select Case option, each Case can be matched on a relational test.<BR><P>Here is the format of the second additional Select Case format:<BR><PRE><FONT COLOR="#000080">Select Case <I>Expression</I>Case <I>expr1</I> To <I>expr2</I>:<I> One or more Visual Basic statements</I>Case <I>expr1</I> To <I>expr2</I>:<I> One or more Visual Basic statements</I>[Case <I>expr1</I> To <I>expr2</I>:<I> One or more Visual Basic statements</I>][Case Else:<I> One or more Visual Basic statements</I>]End Select</FONT></PRE><P>The Case lines require a range, such as 4 To 6. The To Select Case option enables you to match against a range instead of a relation or an exact match.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>You can combine the extended formats of Select Case with the standard Select Case so that two or more kinds of Case formats appear within the same Select Case statement.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>[ic:Stop&Type]The code in Listing 8.4 contains a minor logic bug. The code handles all Age values over 11, but it does not handle Age values below 5. Therefore, if Age contains a value of 4, Listing 8.4 would assign "Advanced" to the label. However, if you add a relational test for the first Case, as shown in Listing 8.5, the code can handle any Age value.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The additional Select Case options extend the power of Select Case beyond that of any selection statement in other languages. Using the two Case options that you learned in this section, you can code multiple-choice selections based on three kinds of matches:<BR><UL><LI> [lb] An exact Case match to Select Case's <I>Expression</I><BR><BR><LI> [lb] A relational Case match to Select Case's <I>Expression</I><BR><BR><LI> [lb] A range of Case matches to Select Case's Expression<BR><BR></UL><P><FONT COLOR="#000080"><B>Listing 8.5. Using </B><B>Select Case</B><B> to simplify complex nested </B><B>If</B><B>-</B><B>Else</B><B> statements.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Select Case Age2: Case Is <5: lblTitle.Text = "Too young"3: Case 5: lblTitle.Text = "Kindergarten"4: Case 6: lblTitle.Text = "1st Grade"5: Case 7: lblTitle.Text = "2nd Grade"6: Case 8: lblTitle.Text = "3rd Grade"7: Case 9: lblTitle.Text = "4th Grade"8: Case 10: lblTitle.Text = "5th Grade"9: Case 11: lblTitle.Text = "6th Grade"10: Case Else: lblTitle.Text = "Advanced"11: End Select</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>By testing for a value less than 5 in line 1, Listing 8.5 ensures that both younger ages and older ages are covered by the Case selections.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Although the Case Else line is optional, be sure to code one unless you know the exact range of values that can match <I>Expression</I>.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>[ic:Stop&Type]Listing 8.6 contains a similar Select Case problem that uses the To option for some of the Case values. Each Case tests against a range of possible values. The ages fall into categories, and the appropriate titles are updated depending on the category matches.<BR><P><FONT COLOR="#000080"><B>Listing 8.6. Using </B><B>Select Case</B><B> ranges for categorizing multiple matches.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Select Case Age2: Case Is <5: lblTitle.Text = "Too young"3: Case 5: lblTitle.Text = "Kindergarten"4: Case 6 To 11: lblTitle.Text = "Elementary"5: lblSchool.Text = "Lincoln"6: Case 12 To 15: lblTitle.Text = "Intermediate"7: lblSchool.Text = "Washington"8: Case 16 To 18: lblTitle.Text = "High School"9: lblSchool.Text = "Betsy Ross"10: Case Else: lblTitle.Text = "College"11: lblSchool.Text = "University"12: End Select</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Listing 8.6 contains every Select Case option that exists. In the Select Case in line 1, the Age value is compared to its many possibilities throughout the rest of the Select Case. If Age holds a value less than 5, the label is updated in line 2 to reflect the child’s age. Nothing else executes. Visual Basic recognizes that the first Case in line 2 is a one-line Case, and the program continues with the line that follows End Select (line 12) as soon as line 2 finishes.<BR><P>Line 3 compares the Age variable to the value of 5, and assigns "Kindergarten" to the label if Age is equal to 5.<BR><P>Lines 4–9 compare whether Age falls within three different ranges of values and updates two label values accordingly. If all Case statements fail through line 9, the Case in line 10 takes over and assumes that Age holds a value greater than 18. (If Age holds any value less than 18, an earlier Case statement would have taken control.)<BR><BR><A NAME="E68E68"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E55"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>What is a condition?<BR><BR><LI>True or false: Relational tests produce one of three results.<BR><BR><LI>True or false: Visual Basic requires parentheses around a relational test.<BR><BR><LI>What are the six relational operators?<BR><BR><LI>Determine the true or false result of the following relational tests:<BR><BR><OL TYPE=A><LI>"zz" < "ZZ"<BR><BR><LI>161 <= 161<BR><BR><LI>3.4 < 4<BR><BR><LI>(3 <> 4) And (5 = 5)<BR><BR><LI>(3 <> 4) Or (5 = 5)<BR><BR><LI>(3 = 3) And (10 < 9)<BR><BR></OL><LI>What is the name of the table that determines how string values are compared?<BR><BR><LI>What Visual Basic statement makes decisions?<BR><BR><LI>What Visual Basic statement prescribes a true <I>and</I> a false course of action?<BR><BR><LI>True or false: The body of an If can contain more than one statement, but the body of the Else must contain one statement at most.<BR><BR><LI>What is a <I>nested </I><I>If</I> statement?<BR><BR><LI>What statement helps eliminate tedious and convoluted If-Else statements?<BR><BR><LI>How many kinds of Case options are there?<BR><BR><LI>What happens if every Case fails and there is no Case Else option for a particular Select Case statement?<BR><BR><LI>What happens if every Case fails and there is a Case Else option on a particular Select Case statement?<BR><BR><LI>Which Case option tests for ranges of values?<BR><BR><LI>Which Case option tests for relational values?<BR><BR></OL><BR><A NAME="E69E56"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>Rewrite the following nested If statement using a single If with a logical operator:<BR>If (M = 3) Then<BR> If (P = 4) Then<BR> TestIt = "Yes"<BR> End If<BR>End If<BR><BR><LI>Rewrite the following If to eliminate the Not to clarify the code:<BR>If Not(d < 3) Or Not(p >= 9) Then<BR><BR><LI>Rewrite Listing 8.6 so that the message "Age Error" appears in the lblTitle.Text property if Age holds a value less than zero.<BR><BR></OL><BR><A NAME="E69E57"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Find the Bug</B></FONT></CENTER></H4><BR><OL><LI>What is wrong with the following If statement?<BR>If (A < 1) And (C >= 8) Then<BR> lblName.Text = "Overdrawn"<BR>Else<BR> lblName.Text = "Underdrawn"<BR>End Else<BR>End If<BR><BR></OL><BR><A NAME="E69E58"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><P>Given the following If and matching Select Case, which one is preferable and easier to maintain? Remember that you want to keep things simple and clear.<BR>If (grade >= 70) Then<BR> lblLetter.Text = "Passing"<BR>Else<BR> lblLetter.Text = "Failing"<BR>End If<BR>or<BR>Select Case grade<BR> Case Is >= 70: lblLetter.Text = "Pasing"<BR> Case Else: lblLetter.Text = "Failing"<BR>End Select<BR><P ALIGN=LEFT><A HREF="vel07.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="velp04.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><A </BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?