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

📄 ch7.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 4 页
字号:
Start the </I><TT><I>while</I></TT><I>loop and test the condition. If false, don't execute the statementblock.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.<BR>INCrement </I><TT><I>$firstVar</I></TT><I>.<BR>Jump back to the start of the statement block and test the conditionagain.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 10;while ($firstVar &lt; 2) {    print(&quot;inside:  firstVar = $firstVar\n&quot;);    $firstVar++;};print(&quot;outside: firstVar = $firstVar\n&quot;);</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>outside: firstVar = 10</PRE></BLOCKQUOTE><P>This example shows that the statement block is never evaluatedif the condition is false when the <TT>while</TT>loop starts. Of course, it's more common to use <TT>while</TT>loops that actually execute the statement block-like the following:<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>while</I></TT><I>loop and test the condition.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.<BR>INCrement </I><TT><I>$firstVar</I></TT><I>.<BR>Jump back to the start of the statement block and test the conditionagain.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 10;while ($firstVar &lt; 12) {    print(&quot;inside:  firstVar = $firstVar\n&quot;);    $firstVar++;};print(&quot;outside: firstVar = $firstVar\n&quot;);</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>inside:  firstVar = 10inside:  firstVar = 11outside: firstVar = 12</PRE></BLOCKQUOTE><P>It's important to note that the value of <TT>$firstVar</TT>ends up as 12 and not 11 as you might expect upon casually lookingat the code. When <TT>$firstVar</TT>is still 11, the condition is true, so the statement block isexecuted again, thereby iNCrementing <TT>$firstVar</TT>to 12. Then, the next time the condition is evaluated, it is falseand the loop ends with <TT>$firstVar</TT>equal to 12.<H3><A NAME="ExampleIUntilILoops">Example: <I>Until</I> Loops</A></H3><P><TT><I>Until</I></TT> loops are usedto repeat a block of statements while some condition is false.Like the previous <TT>while</TT> loop,there are also two forms of the <TT>until</TT>loop: one where the condition is checked before the statementsare executed (the <TT>do...until</TT>loop), and one in which the condition is checked after the statementsare executed (the <TT>until</TT> loop).<P>The <TT>do...until</TT> loop has thissyntax:<BLOCKQUOTE><PRE>do {    STATEMENTS} until (CONDITION);The until loop has this syntax:until (CONDITION) {    STATEMENTS}</PRE></BLOCKQUOTE><P>Again, the loop type you use is dependent on your needs at thetime. Here is an example of the <TT>do...until</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..until</I></TT><I>loop.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.<BR>INCrement </I><TT><I>$firstVar</I></TT><I>.<BR>Check the </I><TT><I>until</I></TT><I>condition; if false, 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++;} until ($firstVar &lt; 2);print(&quot;outside: firstVar = $firstVar\n&quot;);</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>inside:  firstVar = 10inside:  firstVar = 11inside:  firstVar = 12inside:  firstVar = 13inside:  firstVar = 14...</PRE></BLOCKQUOTE><P>This loop continues forever because the condition can never betrue. <TT>$firstVar</TT> starts outgreater than 2 and is iNCremented inside the loop. Therefore,this is an <I>endless </I>loop.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>If you ever find it hard to understand a conditional expression in a loop statement, try the following: Wrap the entire condition expression inside paren-theses and add == 1 to the right-hand side. The above loop then becomes</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><BLOCKQUOTE><PRE>do {    ...} until (($firstVar &lt; 2) == 1);</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. The next example shows the <TT>until</TT>loop in action, which does not execute the statement block whenthe conditional expression is false when the loop starts.<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>until</I></TT><I>loop and test the condition. If true, don't execute the state-mentblock.<BR>print the value of </I><TT><I>$firstVar</I></TT><I>.<BR>INCrement </I><TT><I>$firstVar</I></TT><I>.<BR>Jump back to the start of the statement block and test the conditionagain.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 10;until ($firstVar &lt; 20) {    print(&quot;inside:  firstVar = $firstVar\n&quot;);    $firstVar++;};print(&quot;outside: firstVar = $firstVar\n&quot;);</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>outside: firstVar = 10</PRE></BLOCKQUOTE><P>This example shows that the statement block is never evaluatedif the condition is true when the <TT>until</TT>loop starts. Here is another example of an <TT>until</TT>loop that shows the statement block getting executed:<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>while</I></TT><I>loop and test the condition.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.<BR>INCrement </I><TT><I>$firstVar</I></TT><I>.<BR>Jump back to the start of the statement block and test the conditionagain.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 10;until ($firstVar &gt; 12) {    print(&quot;inside:  firstVar = $firstVar\n&quot;);    $firstVar++;};print(&quot;outside: firstVar = $firstVar\n&quot;);</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>inside:  firstVar = 10inside:  firstVar = 11inside:  firstVar = 12outside: firstVar = 13</PRE></BLOCKQUOTE><H3><A NAME="ExampleIForILoops">Example: <I>For</I> Loops</A></H3><P>One of the most common tasks in programming is looping a specificnumber of times. Whether you need to execute a certain fuNCtionfor every customer in your database or print a page in a report,the <TT><I>for</I></TT> loop can beused. Its syntax is:<BLOCKQUOTE><PRE>for (INITIALIZATION; CONDITION; INCREMENT/DECREMENT) {    STATEMENTS}</PRE></BLOCKQUOTE><P>The <I>initialization</I> expression is executed first-beforethe looping starts. It can be used to initialize any variablesthat are used inside the loop. Of course, this could be done onthe line before the <TT>for</TT> loop.However, iNCluding the initialization inside the <TT>for</TT>statement aids in identifying the loop variables.<P>When initializing variables, be sure not to confuse the equalityoperator (<TT>==</TT>) with the assignmentoperator (<TT>=</TT>). The followingis an example of what this error could look like:<BLOCKQUOTE><PRE>for ($index == 0; $index &lt; 0; $index++)</PRE></BLOCKQUOTE><P>One of the equal signs should be removed. If you think you arehaving a problem with programming the <TT>for</TT>loop, make sure to check out the operators.<P>The <I>condition</I> expression is used to determine whether theloop should continue or be ended. When the condition expressionevaluates to false, the loop will end.<P>The <I>iNCrement/decrement </I>expression is used to modify theloop variables in some way each time the code block has been executed.Here is an example of a basic <TT>for</TT>loop:<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Start the for loop by initializing </I><TT><I>$firstVar</I></TT><I>to zero. The </I><TT><I>$firstVar</I></TT><I>variable will be iNCremented each time the statement block isexecuted. The statement block will be executed as long as </I><TT><I>$firstVar</I></TT><I>is less than 100.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>each time through the loop.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>for ($firstVar = 0; $firstVar &lt; 100; $firstVar++) {    print(&quot;inside:  firstVar = $firstVar\n&quot;);}</PRE></BLOCKQUOTE><P>This program will display:<BLOCKQUOTE><PRE>inside:  firstVar = 0inside:  firstVar = 1...inside:  firstVar = 98inside:  firstVar = 99</PRE></BLOCKQUOTE><P>This program will display the numbers 0 through 99. When the loopis over, <TT>$firstVar</TT> will beequal to 100.<P><TT>For</TT> loops also can be usedto count backwards.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Start the </I><TT><I>for</I></TT><I>loop by initializing </I><TT><I>$firstVar</I></TT><I>to 100. The </I><TT><I>$firstVar</I></TT><I>variable will be decremented each time the statement block isexecuted. And the statement block will be executed as long as</I><TT><I>$firstVar</I></TT><I> isgreater than 0.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>each time through the loop.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>for ($firstVar = 100; $firstVar &gt; 0; $firstVar--) {    print(&quot;inside:  firstVar = $firstVar\n&quot;);}</PRE></BLOCKQUOTE><P>This program will display:<BLOCKQUOTE><PRE>inside:  firstVar = 100inside:  firstVar = 99...inside:  firstVar = 2inside:  firstVar = 1</PRE></BLOCKQUOTE><P>You can use the comma operator to evaluate two expressions atoNCe in the initialization and the iNCrement/decrement expressions.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Start the </I><TT><I>for</I></TT><I>loop by initializing </I><TT><I>$firstVar</I></TT><I>to 100 and </I><TT><I>$secondVar</I></TT><I>to 0. The </I><TT><I>$firstVar</I></TT><I>variable will be decremented and </I><TT><I>$secondVar</I></TT><I>

⌨️ 快捷键说明

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