VBScript 中的 Do..Until 迴圈



當我們希望在條件為假時重複一組語句時,將使用Do..Until迴圈。條件可以在迴圈開始時或結束時檢查。

語法

VBScript 中Do..Until迴圈的語法為 −

Do Until condition
   [statement 1]
   [statement 2]
   ...
   [statement n]
   [Exit Do]
   [statement 1]
   [statement 2]
   ...
   [statement n]
Loop           

流程圖

VBScript Do..Until statement

示例

下面的示例使用Do..Until迴圈在迴圈開始時檢查條件。僅當條件為假時才會執行迴圈內的語句。當條件變為真時,它退出迴圈。

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         i = 10
         Do Until i>15  'Condition is False.Hence loop will be executed
            i = i + 1
            Document.write("The value of i is : " & i)
            Document.write("<br></br>")
         Loop 

      </script>
   </body>
</html>

執行上述程式碼後,它會在控制檯中列印以下輸出。

The value of i is : 11

The value of i is : 12

The value of i is : 13

The value of i is : 14

The value of i is : 15

The value of i is : 16

備用語法

還有一個備用Do..Until迴圈語法,可以在迴圈結束時檢查條件。下面透過示例解釋這兩個語法的重大區別。

Do 
   [statement 1]
   [statement 2]
   ...
   [statement n]
   [Exit Do]
   [statement 1]
   [statement 2]
   ...
   [statement n]
Loop Until condition

流程圖

VBScript Do..Until statement

示例

下面的示例使用Do..Until迴圈在迴圈結束時檢查條件。即使條件為真,迴圈內的語句也會執行一次。

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">        
         i = 10
         Do 
            i = i + 1
            Document.write("The value of i is : " & i)
            Document.write("<br></br>")
         Loop Until i<15 'Condition is True.Hence loop is executed once.
         
      </script>
   </body>
</html>

執行上述程式碼後,它會在控制檯中列印以下輸出。

The value of i is : 11
vbscript_loops.htm
廣告
© . All rights reserved.