VBScript Exit For 语句

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

语法

VBScript 中 Exit For 语句的语法是 −

 Exit For

流程图

VBScript Exit For 语句

示例

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