rev 489 - in trunk: docs/tutorial pr/tutorial

SVN User <[email protected]> Fri, 14 May 2004 18:36:05 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-05-14 18:36:02 -0400 (Fri, 14 May 2004)
New Revision: 489

Added:
   trunk/pr/tutorial/tut6.pr
   trunk/pr/tutorial/tut7.pr
Modified:
   trunk/docs/tutorial/index.htm
   trunk/docs/tutorial/tutorial5.htm
   trunk/pr/tutorial/tut5.pr
Log:
added while, break, continue, else

Modified: trunk/docs/tutorial/index.htm
===================================================================
--- trunk/docs/tutorial/index.htm	2004-05-14 21:19:32 UTC (rev 488)
+++ trunk/docs/tutorial/index.htm	2004-05-14 22:36:02 UTC (rev 489)
@@ -34,7 +34,7 @@
   <table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffee" height="1707">
     <tr> 
       <td height="2203" valign="top"> 
-        <ul class="TofC">
+        <ul>
           <li>1. <a href="tutorial1.htm#uint">Using the Prothon Interpreter</a> 
             <ul>
               <li><a href="tutorial1.htm#intcon">1.1 Interactive Console </a> 
@@ -130,18 +130,27 @@
                   <li><a href="tutorial5.htm#ques">Question Functions_&amp; ?</a> 
                   <li><a href="tutorial5.htm#bool">obj.bool_?</a> 
                   <li><a href="tutorial5.htm#smplcode">Sample Code</a> 
-                  </ul>
+                </ul>
               <li><a href="tutorial5.htm#forstmt">4.2 For Statement</a> 
                 <ul>
-                      
-                  <li><a href="tutorial5.htm#objnext">obj.next()</a>
+                  <li><a href="tutorial5.htm#objnext">obj.next()</a> 
                   <li><a href="tutorial5.htm#iters">Iterators</a> 
                   <li><a href="tutorial5.htm#itertype">Iterator Types</a> 
                   <li><a href="tutorial5.htm#forsmpl">Sample Code</a> 
                 </ul>
+              <li><a href="tutorial5.htm#while">4.3 While Statement</a> 
+                <ul>
+                  <li><a href="tutorial5.htm#whilesmpl">Sample Code</a> 
                 </ul>
-              
-  
+              <li><a href="tutorial5.htm#bcae">4.4 Break, Continue, &amp; Else</a> 
+                <ul>
+                  <li><a href="tutorial5.htm#break">Break Statement</a> 
+                  <li><a href="tutorial5.htm#continue">Continue Statement</a> 
+                  <li><a href="tutorial5.htm#tags">Loop Block Tags</a> 
+                  <li><a href="tutorial5.htm#else">Else Clause</a>
+                  <li><a href="tutorial5.htm#bcaesmpl">Sample Code</a> 
+                </ul>
+            </ul>
         </ul>
       </td>
     </tr>
Modified: trunk/docs/tutorial/tutorial5.htm
===================================================================
--- trunk/docs/tutorial/tutorial5.htm	2004-05-14 21:19:32 UTC (rev 488)
+++ trunk/docs/tutorial/tutorial5.htm	2004-05-14 22:36:02 UTC (rev 489)
@@ -32,7 +32,7 @@
   <br>
   <table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffee" height="1707">
     <tr> 
-      <td height="3521" valign="top"> 
+      <td height="3732" valign="top"> 
         <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="flowcntrl"></a>4.0 
           Flow Control</font></p>
         <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="if-then-else"></a>4.1 
@@ -144,6 +144,8 @@
 for i in 3: 
     print i
 
+print 'i =', i  # i is still valid from loop
+
 for c in "abc":  # ending print with comma
     print c,     # suppresses newline at end
 
@@ -162,11 +164,111 @@
         <pre>0 
 1 
 2 
+i = 2
 a b c 
 0 a 3.7 
 1 2 
 3 4 
 5 6 </pre>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="while"></a>4.3 
+          While Statement</font> </p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="whilesmpl"></a></font>The 
+          &quot;while&quot; statement is a looping statement to repeat blocks 
+          of code like the &quot;for&quot; statement, but it is actually more 
+          similar in structure to the &quot;if&quot; statement. It has an expression 
+          that it evaluates and it repeats the block every time that statement 
+          evaluates to True and finishes when it returns False.</p>
+        <pre>
+# Prothon source file tut6.pr
+
+i = 0
+while i < 3:
+    print i
+    i = i + 1
+
+d = {1:2, 3:4, 5:6}
+while Len(d):
+    print d.popItem!()</pre>
+        <p>Results of running tut6.pr: </p>
+        <pre>0 
+1 
+2 
+(1, 2) 
+(3, 4) 
+(5, 6)</pre>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="bcae"></a>4.3 
+          Break, Continue, &amp; Else</font></p>
+        <p>The &quot;for&quot; and &quot;while&quot; loops have a number of ways 
+          to terminate the looping early before the normal ending. Two of these 
+          ways are the use of the &quot;break&quot; and &quot;continue&quot; statements. 
+          Since these statements interfere with the looping, they are usually 
+          located in a conditional &quot;if&quot; statement and are executed in 
+          certain situations only.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="break"></a></font>The 
+          break statement stops the execution of code in the current block and 
+          stops the looping. Execution continues after the looping block. If code 
+          is executing in blocks nested several deep from the looping block then 
+          the execution jumps out of all the blocks to the outside of the looping 
+          block attached to the &quot;for&quot; or &quot;while&quot; statement. 
+          In the &quot;for&quot; loop, the loop vars that we set by the .next() 
+          call will remain set to the value they had when the break was executed.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="continue"></a></font>Continue 
+          stops code executing at the continue statement also but execution begins 
+          again at the top of the looping block after another call to the iterator 
+          with .next_() has been made if in a &quot;for&quot; loop, or after the 
+          &quot;while&quot; expression has been checked in a &quot;while&quot; 
+          loop. One way to look at the &quot;continue&quot; statement is that 
+          it jumps to the end of the loop block so that it can start another loop.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="tags"></a></font>Both 
+          the &quot;for&quot; and the &quot;while&quot; statements can optionally 
+          have a &quot;nametag&quot; before the word &quot;for&quot; or the word 
+          &quot;while&quot; in their statements. This tag must be written like 
+          any variable name and be seperated from the &quot;for&quot; or &quot;while&quot; 
+          by a colon. Then the &quot;break&quot; and &quot;continue&quot; statements 
+          can optionally specify which loop they want to break out of or continue 
+          by adding this nametag as a parameter. This is very useful if the break 
+          is nested inside several loops and you want to break out of more than 
+          one of them at once.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="else"></a></font>The 
+          &quot;for&quot; and &quot;while&quot; statements can also have an &quot;else&quot; 
+          statement and code block added to the end like the &quot;if&quot; statement 
+          has. This &quot;else&quot; code block is executed if the associated 
+          loop executes normally without a break command interrupting it. This 
+          is useful if the loop is searching for something and the break command 
+          is used when the thing searched for is found. The &quot;else&quot; block 
+          can give an error saying the search failed.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="bcaesmpl"></a></font>Here 
+          is some sample code using break, continue, and else:</p>
+        <pre>
+# Prothon source file tut7.pr
+
+i = 10
+while True:
+    i = i - 1
+    if i % 2 == 0:  # is i an even number?
+        continue    # then start next loop
+    if i < 0:       # is i a negative number?
+        break       # then quit
+    print i,
+print
+
+a: for i in 10:
+    for j in 10:
+        for k in 10:
+            if i==1 and j==2 and k==3:
+                break a
+else:
+    print 'this will not print'
+print i,j,k
+    
+for i in 10:
+    j = i
+else:
+    print 'this will print', j</pre>
+        <p>Here is what that file printed: </p>
+        <pre>9 7 5 3 1 
+1 2 3 
+this will print 9 </pre>
       </td>
     </tr>
   </table>
Modified: trunk/pr/tutorial/tut5.pr
===================================================================
--- trunk/pr/tutorial/tut5.pr	2004-05-14 21:19:32 UTC (rev 488)
+++ trunk/pr/tutorial/tut5.pr	2004-05-14 22:36:02 UTC (rev 489)
@@ -4,6 +4,8 @@
 for i in 3: 
     print i
 
+print 'i =', i  # i is still valid from loop
+
 for c in "abc":  # ending print with comma
     print c,     # suppresses newline at end
 
Added: trunk/pr/tutorial/tut6.pr
===================================================================
--- trunk/pr/tutorial/tut6.pr	2004-05-14 21:19:32 UTC (rev 488)
+++ trunk/pr/tutorial/tut6.pr	2004-05-14 22:36:02 UTC (rev 489)
@@ -0,0 +1,11 @@
+
+# Prothon source file tut6.pr
+
+i = 0
+while i < 3:
+    print i
+    i = i + 1
+
+d = {1:2, 3:4, 5:6}
+while Len(d):
+    print d.popItem!()
\ No newline at end of file

Added: trunk/pr/tutorial/tut7.pr
===================================================================
--- trunk/pr/tutorial/tut7.pr	2004-05-14 21:19:32 UTC (rev 488)
+++ trunk/pr/tutorial/tut7.pr	2004-05-14 22:36:02 UTC (rev 489)
@@ -0,0 +1,26 @@
+
+# Prothon source file tut7.pr
+
+i = 10
+while True:
+    i = i - 1
+    if i % 2 == 0:  # is i an even number?
+        continue    # then start next loop
+    if i < 0:       # is i a negative number?
+        break       # then quit
+    print i,
+print
+
+a: for i in 10:
+    for j in 10:
+        for k in 10:
+            if i==1 and j==2 and k==3:
+                break a
+else:
+    print 'this will not print'
+print i,j,k
+    
+for i in 10:
+    j = i
+else:
+    print 'this will print', j
\ No newline at end of file