VBScript:Do..While循环

一个Do..While当我们想只要条件为true,重复一组语句循环使用。可以在循环开始时或循环结束时检查条件。

语法

VBScript中的Do..While循环的语法是:

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

流程图

VBScript:Do..While循环

下面的示例使用Do..while循环在循环开始时检查条件。仅当条件变为True时,才执行循环内的语句。

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">        
         Do While i < 5
            i = i + 1
            Document.write("The value of i is : " & i)
            Document.write("<br></br>")
         Loop         
      </script>
   </body>
</html>

执行以上代码后,它将在控制台上输出以下输出。

The value of i is : 1

The value of i is : 2

The value of i is : 3

The value of i is : 4

The value of i is : 5

备用语法

Do..while循环还有一个备用语法,它在循环结束时检查条件。下文将通过示例说明这两种语法之间的主要区别。

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

流程图

VBScript:Do..While循环

下面的示例使用Do..while循环在循环结束时检查条件。即使条件为False,循环内的Statement至少要执行一次。

<!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 While i<3 'Condition is false.Hence loop is executed once.
         
      </script>
   </body>
</html>

执行上述代码后,它将在控制台中输出以下输出。

The value of i is : 11

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

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

相关推荐

发表回复

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