Using do…while to Loop an Indeterminate Number of Times

In some situations, you won't know the exact number of times a loop needs to be performed—not even when the loop begins. When you need to create such a loop, using the do…while loop is the best solution.

The do…while comes in a number of flavors. Its most basic form has the following syntax:

						do statement while
						(expression);
					

The following is used to execute multiple statements:

do
{
   [Statements]
}  while (expression);
						

Ending a do…while

A do…while without some sort of exit mechanism or defined condition is an endless loop. In its most basic form, nothing is present to tell the loop when to ...

Get Sams Teach Yourself C#™ in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.