ZA Exit For Instrukcja jest używana, gdy chcemy wyjść z ForPętla oparta na określonych kryteriach. GdyExit For jest wykonywany, sterowanie przechodzi do następnej instrukcji bezpośrednio po For Pętla.
Składnia
Składnia Exit For Instrukcja w VBScript to -
Exit For
Diagram przepływu
Przykład
Poniższy przykład używa Exit For. Jeśli wartość licznika osiągnie 4, pętla For zostaje zakończona, a sterowanie przeskakuje do następnej instrukcji bezpośrednio po pętli 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>
Kiedy powyższy kod jest wykonywany, wyświetla w konsoli następujące dane wyjściowe.
The value is i is : 0
The value is i is : 2
The value is i is : 4
The value is i is : 40