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循环

下面的示例使用Do..Until循环在循环开始时检查条件。仅当条件为false时,才执行循环内的Statements。当条件变为true时,它将退出循环。

<!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循环

下面的示例使用Do..Until循环在循环结束时检查条件。即使条件为True,循环内的语句也至少执行一次。

<!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

作者:terry,如若转载,请注明出处:https://www.web176.com/vbscript/1268.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2020年12月9日 下午5:40
下一篇 2020年12月9日 下午5:51

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注