📄 ch02.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//--> </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself Borland Delphi 4 in 21 Days -- Ch 2 -- More on Pascal</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"></H1><H1><BR>Teach Yourself Borland Delphi 4 in 21 Days</H1></CENTER><CENTER><P><A HREF="../ch01/ch01.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch03/ch03.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><CENTER><H1>- 2 -</H1></CENTER><CENTER><H1>More on Pascal</H1></CENTER><UL> <LI><A HREF="#Heading1">if, then, else</A> <UL> <LI><A HREF="#Heading2">Executing Multiple Instructions</A> <LI><A HREF="#Heading3">Adding else</A> <LI><A HREF="#Heading4">Nested if Statements</A> </UL> <LI><A HREF="#Heading5">Using Loops</A> <LI><A HREF="#Heading6">The for Loop</A> <UL> <LI><A HREF="#Heading7">The while Loop</A> <LI><A HREF="#Heading8">The repeat Loop</A> <LI><A HREF="#Heading9">The goto Statement</A> <LI><A HREF="#Heading10">Continue and Break Procedures</A> </UL> <LI><A HREF="#Heading11">The case Statement</A> <LI><A HREF="#Heading12">Scope</A> <LI><A HREF="#Heading13">Records</A> <UL> <LI><A HREF="#Heading14">The with Statement</A> <LI><A HREF="#Heading15">Arrays of Records</A> <LI><A HREF="#Heading16">Include Files</A> </UL> <LI><A HREF="#Heading17">Functions, Procedures, and Methods</A> <UL> <LI><A HREF="#Heading18">Declaration and Definition</A> <LI><A HREF="#Heading19">Value, Constant, and Reference Parameters</A> <LI><A HREF="#Heading20">Local Functions and Procedures</A> </UL> <LI><A HREF="#Heading21">Method Overloading</A> <UL> <LI><A HREF="#Heading22">Default Parameters for Functions</A> </UL> <LI><A HREF="#Heading23">Summary</A> <LI><A HREF="#Heading24">Workshop</A> <UL> <LI><A HREF="#Heading25">Q&A</A> <LI><A HREF="#Heading26">Quiz</A> <LI><A HREF="#Heading27">Exercises</A> </UL></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>You now have a pretty good start on learning Object Pascal. In this chapter, youwill continue to learn about the Object Pascal language by examining more of thefundamentals of Object Pascal. Today you will learn about</P><UL> <LI>The if, then, and else keywords <P> <LI>Loops: for, while, and repeat <P> <LI>The case statement <P> <LI>Scope <P> <LI>Records <P> <LI>Functions and procedures</UL><H2><A NAME="Heading1"></A>if, then, else</H2><P>There are some aspects of programming that are common to all programming languages.One such item that Object Pascal has in common with other programming languages isthe if statement. The if statement is used to test for a condition and then executesections of code based on whether that condition is True or False. Here's an example:</P><P><PRE>var X : Integer;begin X := StrToInt(Edit1.Text); if X > 10 then Label1.Caption := `You entered a number greater than 10.';end;</PRE><P>This code gets the contents of an edit control and stores it in an integer variable.If the number is greater than 10, the expression x > 10 evaluates to True andthe message is displayed; otherwise, nothing is displayed. Note that when the conditionalexpression evaluates to True, the statement immediately following the if...then expressionis executed. The conditional part of an if statement is always followed by then.</P><P><strong>New Term:</strong> The if statement is used to test for a condition and executeone or more lines of code when that condition evaluates to True.</P><P><H3><A NAME="Heading2"></A>Executing Multiple Instructions</H3><P>Let's say you have multiple lines of code that should be executed when the conditionalexpression is True. In that case, you would need begin and end keywords to blockthose lines:</P><P><PRE>if X > 10 then begin Label1.Caption := `You entered a number greater than 10.'; DoSomethingWithNumber(X);end;</PRE><P>When the conditional expression evaluates to False, the code block associatedwith the if expression is ignored and program execution continues with the firststatement following the code block.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Object Pascal contains a few shortcuts. One of those shortcuts involves using just a Boolean variable's name to test for True. Look at this code:</P> <PRE>if FileGood then ReadData;</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE> <P>This method is shortcut for the longer form, which is illustrated with this line:</P> <PRE>if FileGood = True then ReadData;</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE> <P>This shortcut only applies to Boolean variables. You can test for False by applying the not keyword to a variable name:</P> <PRE>var FileGood : Boolean;begin FileGood := OpenSomeFile; if not FileGood then ReportError;end;</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE> <P>Learning the Object Pascal shortcuts helps you write code that contains a degree of elegance. Knowing the shortcuts will also help you understand Object Pascal code that you read in examples and sample listings. <HR></BLOCKQUOTE><P><H3><A NAME="Heading3"></A>Adding else</H3><P>In some cases, you might want to perform an action when the conditional expressionevaluates to True and perform some other action when the conditional expression evaluatesto False. You accomplish this by implementing the else statement:</P><P><PRE>if X = 20 then DoSomething(X)else DoADifferentThing(X);</PRE><PRE><strong>New Term:</strong> The else statement is used in conjunction with the if statement and identifies a section of code that is executed when the if statement fails (that is, evaluates to False).</PRE><P>In this example, one of the two functions will be called based on the value ofX, but not both.</P><P>I want you to notice something about the preceding example. The line followingthe if statement does not end in a semicolon. This is because the entire if...then...elsesequence is viewed as a single statement. You omit the semicolon on the first linefollowing the if statement <I>only</I> if it's a single line of code (that is, youare not using begin and end following the if statement). Here are a couple of examplesof legal if...then...else syntax:</P><P><PRE>if X = 20 then DoSomething(X) { no semi-colon here because }else { it's a single line of code } DoADifferentThing(X);if X = 20 then begin DoSomething(X); { semi-colon needed here because }end else begin { of the begin/end block } DoADifferentThing(X);end;if X = 20 then begin DoSomething(X); { Multiple lines, use semi-colons } X := 200; { at the end of each line. } Y := 30;end else begin DoADifferentThing(X); X := 100; Y := 15;end;</PRE><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> It doesn't matter where you put the then, begin, and else keywords. The following two blocks of code are identical as far as the compiler is concerned:</P> <PRE>{ One way to do it... }if X = 20 then begin DoSomething(X); X := 200; Y := 30;end else begin DoADifferentThing(X); X := 100; Y := 15;end;{ Same code, different layout... }if X = 20 then begin DoSomething(X); X := 200; Y := 30;end else begin DoADifferentThing(X); X := 100; Y := 15;end;</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE> <P>Ultimately it's up to you to decide on the coding style that you will use. While coding style is largely a matter of preference, be sure you settle on a style that makes your code easy to read. <HR></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Remember that the equality operator is the equal sign (=) and that the assignment operator is colon-equal (:=). A common coding mistake is to use the assignment operator where you mean to use the equality operator. Fortunately, the compiler will issue an error when you do this. <HR></BLOCKQUOTE><H3></H3><H3><A NAME="Heading4"></A>Nested if Statements</H3><P>You can nest if statements when needed. Nesting is nothing more than followingan if statement with one or more additional if statements.</P><P><PRE>if X > 10 then if X < 20 then Label1.Caption := `X is between 10 and 20';</PRE><P>Keep in mind that these are simplified examples. In the real world, you can getlost in the maze of begin and end statements that separate one code block from thenext. Take a look at this code snippet, for instance:</P><P><PRE>if X > 100 then begin Y := 20; if X > 200 then begin Y := 40; if X > 400 then begin Y := 60; DoSomething(Y); end; end;</PRE><PRE>end else if X < -100 then begin<B></B> Y := -20; if X < -200 then begin Y := -40; if X < -400 then begin Y := -60; DoSomething(Y); end; end;end;</PRE><P>Even this is a fairly simple example, but you get the idea.</P><BLOCKQUOTE> <P><HR>[BEGTIP:</strong> When a section of code contains more than two or three consecutive
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -