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.