home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / MS_DEV / VID / SERVER / ASF / DATA.Z / loops.asp < prev    next >
Text File  |  1996-09-20  |  2KB  |  84 lines

  1. <HTML>
  2. <!-- the following script is from the "Writing ActiveX Server Scripts"
  3.      section of the "ActiveX Server Scripting Guide." -->
  4.  
  5. <% On Error Resume Next %>
  6.  
  7. <H3> Repeating a Loop While a Condition Is True</H3><P>
  8.  
  9. Use the While keyword to check a condition in a Do...Loop statement. You can check the condition before you enter the loop (as shown in the first example following this paragraph), or you can check it after the loop has run at least once (as shown in the second example).<P>
  10.  
  11. <%
  12.   ' First example of the While keyword
  13.  
  14.   Counter = 0
  15.   MyNum = 20
  16.  
  17.   Do While MyNum > 10
  18.     MyNum = MyNum - 1
  19.     Counter = Counter + 1
  20.   Loop
  21. %>
  22. The first loop made <%= Counter %> repetitions.<P>
  23.  
  24. <%
  25.   ' Second example of the While keyword
  26.  
  27.   Counter = 0
  28.   MyNum = 9
  29.  
  30.   Do
  31.     MyNum = MyNum - 1
  32.     Counter = Counter + 1
  33.   Loop While MyNum > 10
  34. %>
  35. The second loop made <%= Counter %> repetitions.<P>
  36. <P>
  37.  
  38. <H3>Repeating a Statement Until a Condition Becomes True</H3>
  39. <P> 
  40. You can use the Until keyword in two ways to check a condition in a Do...Loop statement. You can check the condition before you enter the loop (as shown in the first example following this paragraph), or you can check it after the loop has run at least once (as shown in the second example). As long as the condition is False, the looping occurs.
  41. <P>
  42. <%
  43.   ' First example of the Until keyword
  44.   Counter = 0
  45.   MyNum = 20
  46.  
  47.   Do Until myNum = 10
  48.     MyNum = MyNum - 1
  49.     Counter = Counter + 1
  50.   Loop
  51. %>
  52. The first loop made <%= Counter %> repetitions.<P>
  53.  
  54. <%
  55.   ' Second example of the Until keyword
  56.   Counter = 0
  57.   MyNum = 1
  58.   Do
  59.     MyNum = MyNum + 1
  60.     Counter = counter + 1
  61.   Loop Until MyNum = 10
  62. %>
  63. The second loop made <%= Counter %> repetitions.<P>
  64. <P>
  65.  
  66. <H3>Exiting a Do ... Loop Statement from Inside the Loop</H3>
  67. <P>
  68. You can exit a Do ... Loop by using the Exit Do statement. You usually want to exit when you have accomplished the task the loop is performing or in certain situations to avoid an endless loop.<P>
  69.  
  70. In the following example, myNum is assigned a value that creates an endless loop. The If...Then...Else statement checks for this condition, preventing the endless repetition.
  71. <P>
  72.  
  73. <%
  74.   Counter = 0
  75.   MyNum = 9
  76.   Do Until myNum = 10
  77.     MyNum = MyNum - 1
  78.     Counter = Counter + 1
  79.     If MyNum < 10 Then Exit Do
  80.   Loop
  81. %>
  82. The loop made <%= Counter %> repetitions.<P>
  83.  
  84. </HTML>