- VBScript 教程
- VBScript - 主页
- VBScript - 概览
- VBScript - 语法
- VBScript - 启用
- VBScript - 位置
- VBScript - 变量
- VBScript - 常量
- VBScript - 运算符
- VBScript - 决策
- VBScript - 循环
- VBScript - 事件
- VBScript - Cookie
- VBScript - 数字
- VBScript - 字符串
- VBScript - 数组
- VBScript - 日期
- VBScript 高级
- VBScript - 过程
- VBScript - 对话框
- VBScript - 面向对象
- VBScript - 正则表达式
- VBScript - 错误处理
- VBScript - 杂项语句
- VBScript 实用资源
- VBScript - 常见问题解答
- VBScript - 快速指南
- VBScript - 实用资源
- VBScript - 讨论
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
vbscript_loops.htm
广告