C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
A, B: Statement A is reached when the program begins. B is reached only once because the loop body is only entered once.
C, D: C is not reached because the Exit While statement is first encountered. Statement D is reached after the Exit While statement is hit.
VB.NET program that uses Exit While
Module Module1
Sub Main()
Console.WriteLine("A")
Dim i As Integer = 10
While True
Console.WriteLine("B")
If i = 10 Then
Exit While
End If
Console.WriteLine("C")
End While
Console.WriteLine("D")
End Sub
End Module
Output
A
B
D
So: For example, if a While-loop contains an Exit While, the Exit Sub is more symmetric to this.
VB.NET program that uses Exit Sub
Module Module1
Sub Main()
Console.WriteLine("A")
Dim i As Integer = 10
While True
Console.WriteLine("B")
If i = 10 Then
Exit Sub
End If
Console.WriteLine("C")
End While
Console.WriteLine("D")
End Sub
End Module
Output
A
B
Exit Do: As with other loop Exit statements, Exit Do breaks out of an enclosing Do loop.