Categories: VBScript 教程

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           

流程图

下面的示例使用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

流程图

下面的示例使用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

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

vue:页面注入js修改input值

一般会直接这样写: let z…

8 小时 ago

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago