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.