⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch7.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<HTML><HEAD><TITLE>Chapter 7 -- Control Statements</TITLE><META></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910"><H1><FONT SIZE=6 COLOR=#FF0000>Chapter&nbsp;7</FONT></H1><H1><FONT SIZE=6 COLOR=#FF0000>Control Statements</FONT></H1><HR><P><CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER><UL><LI><A HREF="#DecisionStatements">Decision Statements</A><UL><LI><A HREF="#ExampleTheifStatement">Example: The if Statement</A></UL><LI><A HREF="#LoopStatements">Loop Statements</A><UL><LI><A HREF="#ExampleIWhileILoops">Example: <I>While</I> Loops</A><LI><A HREF="#ExampleIUntilILoops">Example: <I>Until</I> Loops</A><LI><A HREF="#ExampleIForILoops">Example: <I>For</I> Loops</A><LI><A HREF="#ExampleIForeachILoops">Example: <I>Foreach</I> Loops</A></UL><LI><A HREF="#JumpKeywords">Jump Keywords</A><UL><LI><A HREF="#ExampleTheIlastIKeyword">Example: The <I>last</I> Keyword</A><LI><A HREF="#ExampleTheInextIKeyword">Example: The <I>next</I> Keyword</A><LI><A HREF="#ExampleTheIredoIKeyword">Example: The <I>redo</I> Keyword</A><LI><A HREF="#ExampleTheIgotoIKeyword">Example: The <I>goto</I> Keyword</A></UL><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>The last chapter, &quot;Statements,&quot; discussed no-action,action, and modified statements. This chapter discusses threemore types of statements: decision statements, loop statements,and jump statements.<P>You see how to use the <TT>if</TT>statement to decide on one or more courses of actions. Loop statementsare used to repeat a series of statements until a given conditionis either true or false. And finally, we'll wrap up the chapterby looking at jump statements, which let you control program flowby moving directly to the beginning or the end of a statementblock.<H2><A NAME="DecisionStatements"><FONT SIZE=5 COLOR=#FF0000>Decision Statements</FONT></A></H2><P><I>Decision statements </I>use the <I>if </I>keyword to executea statement block based on the evaluation of an expression orto choose between executing one of two statement blocks basedon the evaluation of an expression. They are used quite often.For example, a program might need to run one code section if acustomer is female and another code section if the customer ismale.<H3><A NAME="ExampleTheifStatement">Example: The if Statement</A></H3><P>The syntax for the <TT>if</TT> statementis the following:<BLOCKQUOTE><PRE>if (CONDITION) {    # Code block executed    # if condition is true.} else {    # Code block executed    # if condition is false.}</PRE></BLOCKQUOTE><P>Sometimes you need to choose from multiple statement blocks, suchas when you need to execute a different statement block for eachmonth. You use the <TT>if...elsif</TT>statement for this type of decision. The <TT>if...elsif</TT>statement has this syntax:<BLOCKQUOTE><PRE>if (CONDITION_ONE) {    # Code block executed    # if condition one is true.} elsif (CONDITION_TWO) {    # Code block executed    # if condition two is true.} else {    # Code block executed    # if all other conditions are false.}</PRE></BLOCKQUOTE><P>Conditional expressions can use any of the operators discussedin <A HREF="ch4.htm" >Chapter 4</A> &quot;Operators.&quot; Even assignment operatorscan be used because the value of an assignment expression is thevalue that is being assigned. That last senteNCe may be a bitconfusing, so let's look at an example.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign </I><TT><I>$firstVar</I></TT><I>a value of 10.<BR>Subtract five from </I><TT><I>$firstVar</I></TT><I>and if the resulting value is true (for instaNCe, not zero), thenexecute the statement block.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 10;if ($firstVar -= 5) {    print(&quot;firstVar = $firstVar\n&quot;);}</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>firstVar = 5<BR></PRE></BLOCKQUOTE><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>If you're a C or C++ programmer, take heed: The curly braces around the statement block are <I>not</I> optional in Perl. Even one-line statement blocks must be surrounded by curly braces.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>This example, in addition to demonstrating the use of assignmentoperators inside conditional expressions, also shows that the<TT>else</TT> part of the <TT>if</TT>statement is optional. If the <TT>else</TT>part was coded, then it would only be executed when $firstVarstarts out with a value of 5.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign </I><TT><I>$firstVar</I></TT><I>a value of 10.<BR>Subtract five from </I><TT><I>$firstVar</I></TT><I>and if the resulting value is true (in other words, not zero),then print </I><TT><I>$firstVar</I></TT><I>.If not, print &quot;</I><TT><I>firstVar iszero</I></TT><I>.&quot;</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 5;if ($firstVar -= 5) {    print(&quot;firstVar = $firstVar\n&quot;);} else {    print(&quot;firstVar is zero\n&quot;);}</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>firstVar is zero</PRE></BLOCKQUOTE><P>This example shows the use of the <TT>else</TT>clause of the <TT>if</TT> statement.Because the value of <TT>$firstVar</TT>minus 5 was zero, the statements in the <TT>else</TT>clause were executed.<P>You also can use the <TT>if</TT> statementto select among multiple statement blocks. The <TT>if...elsif</TT>form of the statement is used for this purpose.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize </I><TT><I>$month</I></TT><I>to 2.<BR>If the value of </I><TT><I>$month</I></TT><I>is 1, then print January.<BR>If the value of </I><TT><I>$month</I></TT><I>is 2, then print February.<BR>If the value of </I><TT><I>$month</I></TT><I>is 3, then print March.<BR>For every other value of </I><TT><I>$month</I></TT><I>,print a message.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$month = 2;if ($month == 1) {    print(&quot;January\n&quot;);}elsif ($month == 2) {    print(&quot;February\n&quot;);} elsif ($month == 3) {    print(&quot;March\n&quot;);}else {    print(&quot;Not one of the first three months\n&quot;);}</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>February</PRE></BLOCKQUOTE><P>The <TT>else</TT> clause at the endof the <TT>elsif</TT> chain servesto catch any unknown or unforeseen values and is a good placeto put error messages. Frequently, those error messages shouldiNClude the errant value and be written to a log file so thatthe errors can be evaluated. After evaluation, you can decideif the program needs to be modified to handle that unforeseenvalue using another <TT>elsif</TT>clause.<H2><A NAME="LoopStatements"><FONT SIZE=5 COLOR=#FF0000>Loop Statements</FONT></A></H2><P>A loop is used to repeat the execution of a statement block untila certain condition is reached. A loop can be used to iteratethrough an array looking for a value. Loops  also can be usedto count quantities. Actually, the number of uses for loops ispretty much unlimited. There are three types of loops: while loops,until loops, and for loops.<H3><A NAME="ExampleIWhileILoops">Example: <I>While</I> Loops</A></H3><P><TT><I>While</I></TT><I> </I>loopsare used to repeat a block of statements while some conditionis true. There are two forms of the loop: one where the conditionis checked before the statements are executed (the <TT>do..while</TT>loop), and one in which the condition is checked after the statementsare executed (the <TT>while</TT> loop).<P>The <TT>do...while</TT> loop has thissyntax:<BLOCKQUOTE><PRE>do {    STATEMENTS} while (CONDITION);The while loop has this syntax:while (CONDITION) {    STATEMENTS}continue {    STATEMENTS}</PRE></BLOCKQUOTE><P>The statements in the <TT>continue</TT>block of the <TT>while</TT> loop areexecuted just before the loop starts the next iteration. The <TT>continue</TT>block  rarely is used. However, you can see it demonstrated inthe section, &quot;Example: Using the <TT>-n</TT>and <TT>-p</TT> Options,&quot; in<A HREF="ch17.htm" >Chapter 17</A>, &quot;Using Command-Line Options.&quot;<P>Which type you use for any particular task is entirely dependenton your needs at the time. The statement block of a <TT>do...while</TT>loop always will be executed at least oNCe. This is because thecondition is checked after the statement block is executed ratherthan before. Here is an example of the <TT>do...while</TT>loop.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize </I><TT><I>$firstVar</I></TT><I>to 10.<BR>Start the </I><TT><I>do...while</I></TT><I>loop.<BR>Print the value of $firstVar.<BR>INCrement $firstVar.<BR>Check the </I><TT><I>while</I></TT><I>condition; if true, jump back to the start of the statement block.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 10;do {    print(&quot;inside:  firstVar = $firstVar\n&quot;);    $firstVar++;} while ($firstVar &lt; 2);print(&quot;outside: firstVar = $firstVar\n&quot;);</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>inside:  firstVar = 10outside: firstVar = 11</PRE></BLOCKQUOTE><P>This example shows that the statement block is executed even thoughthe condition <TT>$firstVar &lt; 2</TT>is false when the loop starts. This ability occasionally comesin handy while counting down-such as when printing pages of areport.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize </I><TT><I>$numPages</I></TT><I>to 10.<BR>Start the </I><TT><I>do...while</I></TT><I>loop.<BR>Print a page.<BR>Decrement </I><TT><I>$numPages</I></TT><I>and then loop if the condition is still true.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$numPages = 10;do {    printPage();} while (--$numPages);</PRE></BLOCKQUOTE><P>When this loop is done, all of the pages will have been displayed.This type of loop would be used when you know that there alwayswill be pages to process. Notice that because the predecrementoperator is used, the <TT>$numPages</TT>variable is decremented before the condition expression is evaluated.<P>If you need to ensure that the statement block does not get executed,then you need to use the <TT>while</TT>statement.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize </I><TT><I>$firstVar</I></TT><I>to 10.<BR>

⌨️ 快捷键说明

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