Heinzi Heinzi - 1 month ago 6
Vb.net Question

Where is Do ... Loop without a condition documented?

Apparently, one can create a

Do ... Loop
-Loop without a condition. The following code compiles with .NET 4.5 (fiddle) as well as with Roslyn (fiddle):

Public Sub Main()
Do
Console.WriteLine("Hello World")
Exit Do
Loop
End Sub


However, the grammar on the documentation page only offers the following two options:

Do { While | Until } condition
[ statements ]
[ Continue Do ]
[ statements ]
[ Exit Do ]
[ statements ]
Loop
-or-
Do
[ statements ]
[ Continue Do ]
[ statements ]
[ Exit Do ]
[ statements ]
Loop { While | Until } condition


Is this a bug in the compiler, a bug in the documentation or did I just not look hard enough?

Answer

When in doubt, consult the language specification, rather than the reference:

10.9.1 While...End While and Do...Loop Statements

A While or Do loop statement loops based on a Boolean expression. ... An expression may be placed after the Do keyword or after the Loop keyword, but not after both. ... It is also valid to specify no expression at all;

(My emphasis)

The language reference tries to be more straightforward but may lose essential details. The language specification should match what the compiler implements.

Comments