VBScript Exit For 语句



当我们想要根据某些条件退出 For 循环时,会使用 Exit For 语句。当执行 Exit For 时,控制权会立即跳至 For 循环之后的下一条语句。

语法

VBScript 中 Exit For 语句的语法为:-

 Exit For

流程图

VBScript Exit For statement

示例

以下示例使用了 Exit For。如果计数器值达到 4,则退出 For 循环,控制权会立即跳至 For 循环之后的下一条语句。

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim a : a = 10
         For i = 0 to a Step 2 'i is the counter variable and it is incremented by 2
            document.write("The value is i is : " & i)
            document.write("<br></br>")
         
         If i = 4 Then
            i = i*10  'This is executed only if i = 4
            document.write("The value is i is : " & i)
            Exit For 'Exited when i = 4
         End If	 
         Next
         
      </script>
   </body>
</html>

当执行以上代码时,它会在控制台打印以下输出。

The value is i is : 0

The value is i is : 2

The value is i is : 4

The value is i is : 40 
vbscript_loops.htm
广告
© . All rights reserved.