vel08.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 1,958 行 · 第 1/3 页
HTM
1,958 行
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel08.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="velp04.htm"><LINK REL="Previous" HREF="vel07.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel08.htm</H2><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="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><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E61" >What You'll Learn</A><LI><A HREF="#E68E62" >The Relational Operators</A><LI><A HREF="#E68E63" >The If Makes Decisions</A><LI><A HREF="#E68E64" >Handling False Conditions</A><LI><A HREF="#E68E65" >Logical Operators</A><LI><A HREF="#E68E66" >Multiple Choice with Select Case</A><LI><A HREF="#E68E67" >Two Additional Select Case Formats</A><LI><A HREF="#E68E68" >Homework</A><UL><LI><A HREF="#E69E55" >General Knowledge</A><LI><A HREF="#E69E56" >Write Code That...</A><LI><A HREF="#E69E57" >Find the Bug</A><LI><A HREF="#E69E58" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E11"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 4, Unit 8</B></FONT></CENTER></H1><BR><A NAME="E67E14"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Data Comparisons</B></FONT></CENTER></H2><BR><BR><A NAME="E68E61"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI> [lb] Relational operators<BR><BR><LI> [lb] If statements<BR><BR><LI> [lb] Handling false conditions<BR><BR><LI> [lb] Logical operators<BR><BR><LI> [lb] Multiple choice with Select Case<BR><BR><LI> [lb] Two additional Select Case formats<BR><BR></UL><P>Computers cannot think on their own, but with your help they can be taught to make decisions based on values contained in controls and variables. Visual Basic’s decision-making capability enables it to calculate sales figures based on certain conditions, to print exception reports, and to check user responses by means of the form’s controls.<BR><P>You have learned only a few statements so far:<BR><UL><LI> [lb] The End statement, which terminates programs<BR><BR><LI> [lb] The Dim statement, which defines program variables<BR><BR><LI> [lb] The assignment statement, which stores data values in controls and variables.<BR><BR></UL><P>In addition, you know about the fundamental math operators. In this unit, you will learn some new programming statements and operators that you can use along with the ones you already know to write programs that make data-based decisions.<BR><BR><A NAME="E68E62"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The Relational Operators</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Visual Basic supports the use of six operators that produce true or false results based on data values. Once you learn the relational operators, you can combine them operators with the If statement to add power to your programs.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: Relational operators</I> compare data values to one another.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Table 8.1 describes the six relational operators that Visual Basic supports. You use the relational operators to compare data values. They are easy to use. If you take any two numbers, one number is always be greater than, equal to, or less than the other.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The mathematical operators that you learned in the previous unit produced numeric answers. The relational operators produce only true and false answers. In other words, one data value is either more than another—a true result—or the data value is <I>not</I> more than the other—a false result.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 8.1. The relational operators.</B></FONT></CENTER><BR><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Operator</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Usage</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Sales > Goal</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The <I>greater than</I> operator. Returns true if the value on the left side of > is numerically or alphabetically greater than the value on the right. Otherwise, false.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Pay < 2000.00</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The <I>less than</I> operator. Returns true if the value on the left side of < is numerically or alphabetically less than the value on the right. Otherwise, false.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>=</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Age = Limit</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The <I>equal to</I> operator (sometimes called the <I>equal</I> operator). Returns true if the values on both sides of = are equal to each other. Otherwise, false.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>>=</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>FirstName >= "Mike"</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The <I>greater than or equal to</I> operator. Returns true if the value on the left side of >= is numerically or alphabetically greater than or equal to the value on the right. Otherwise, false.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><=</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Num <= lblAmt.Caption</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The <I>less than or equal to</I> operator. Returns true if the value on the left side of <= is numerically or alphabetically less than or equal to the value on the right. Otherwise, false.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>txtAns.Text <> "Yes"</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The <I>not equal to</I> operator. Returns true if the value on the left side of <> is numerically or alphabetically unequal to the value on the right. Otherwise, false.</FONT></TABLE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>The <I>ASCII table</I> is a list of characters with corresponding numeric representations.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>All the relational operators work on both numeric and alphabetic values. You can compare any kind of number against another number, or any kind of string against another string. When you compare strings, Visual Basic uses the ASCII table, included in <A HREF="velxa.htm">Appendix A</A>, to determine how to compare the characters. For example, the ASCII table says that the uppercase letter <I>A</I>—whose ASCII numeric value is 65—is less than the uppercase letter <I>B</I>—whose ASCII numeric value is 66. Notice that all uppercase letters are less than lowercase letters. Therefore, the abbreviation <I>ST</I> is less than <I>St</I>.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Pronounce <I>ASCII</I> as <I>ask-ee</I>.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>To understand how relational operators work, you must understand how to use their true or false results. The If statement, introduced in the next section, explains how you can take use true and false results to make decisions in your program. Before you read the next section, make sure that you understand how these operators compare values. For a quick self-test, make sure that you understand the <I>Result</I> column of Table 8.2 before you go any further.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 8.2. Relational results.</B></FONT></CENTER><BR><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Relation</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Result</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>10 > 5</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>True</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>5 > 10</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>False</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>5 < 10</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>True</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>"Apple" <= "Orange"</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>True</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>"Mac Donald" < "Mc Donald"</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>True</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>0 >= 0</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>True</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>0 <= 0</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>True</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>1 <> 2</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>True</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>2 >= 3</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>False</FONT></TABLE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>Keep Each Side a Consistent Data Type: The expressions on both sides of a relational operator must have the same data type or at least compatible data types. In other words, you cannot compare a string to a numeric data type. If you try, you will get a Type mismatch error because the data types don't match.<BR>You can compare any numeric data type against any other numeric data type most of the time. In other words, you can test whether a single-precision value is less than or greater than an integer value. Be careful, however, when you compare non-integer numeric data for equality. Precision numbers are difficult to represent internally. For example, if you assigned 8.3221 to a single-precision variable and assigned 8.3221 to another single-precision variable, Visual Basic might return a false result if you compare the values for equality. Internally, one of the variables might actually hold 8.322100001 because of rounding errors that occur in normally insignificant decimal places. You can safely compare two currency values for equality, however, because Visual Basic maintains their accuracy to two decimal places.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The relational operators are sometimes called the <I>conditional operators</I> because they test conditions that are either true or false.</NOTE><BR><HR ALIGN=CENTER>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?