VBScript:For…Each循环

目录

当我们要为数组或集合中的每个元素执行一条语句或一组语句时,将使用一个For Each循环。

For Each循环类似于For循环; 但是,对数组或组中的每个元素执行循环。因此,步数计数器在这种类型的循环中将不存在,它通常与数组一起使用或在文件系统对象的上下文中使用,以便进行递归操作。

语法

VBScript中For Each循环的语法是:

For Each element In Group
   [statement 1]
   [statement 2]
   ....
   [statement n]
   [Exit For]
   [statement 11]
   [statement 22]
Next

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         'fruits is an array 
         fruits = Array("apple","orange","cherries")
         Dim fruitnames

         'iterating using For each loop. 
         For each item in fruits
            fruitnames = fruitnames&item&vbnewline
         Next

         msgbox fruitnames
         
      </script>
   </body>
</html>

执行上述代码后,它将在每行中打印所有带有一个项目的水果名称。

apple
orange
cherries

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

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

相关推荐

发表回复

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