CHAPTER 8

image

Loops

There are four looping structures in C#. These are used to execute a specific code block multiple times. Just as with the conditional if statement, the curly brackets for the loops can be left out if there is only one statement in the code block.

While loop

The while loop runs through the code block only if its condition is true, and will continue looping for as long as the condition remains true. Note that the condition is only checked at the beginning of each iteration (loop).

int i = 0;while (i < 10) { System.Console.Write(i++); } // 0-9

Do-while loop

The do-while loop works in the same way as the while loop, except that it ...

Get C# Quick Syntax Reference 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.