Do while programs

As you have seen, C# supports four types of iteration statements: for, foreach, do-while, and while.

Microsoft's language reference documentation describes these statements as follows:

  • The for statement: executes its body while a specified Boolean expression (the 'condition') evaluates to true.

  • The foreach statement: enumerates the elements of a collection and executes its body for each element of the collection.

  • The do-while statement: conditionally executes its body one or more times.

  • The while statement: conditionally executes its body zero or more times.

The for and foreach iterations seem to be clearly differentiated from each other and from the do-while and while iterations. The definitions for the do-while and while statements, however, appear to be quite similar.

Knowing when to choose between a do-while and a while seems more arbitrary, and can even be a bit confusing. Some challenge projects may help to make the differences clear.

Last updated

Was this helpful?