Loops

Loops are a fundamental component of every modern programming language. Whenever a set of commands has to be executed more than once, a loop is a good idea and, in many cases, the only choice.

C# provides all fundamental kinds of loops and we take a closer look at them in this section.

while

while loops are a widespread way to perform repeated computation. A while loop is executed as long as a certain condition returns true. The next listing contains a program showing how things work:

 using System; class Loop { public static void Main() { bool keepdoing = true; string input; while (keepdoing == true) { Console.Write("Input: "); input = Console.ReadLine(); Console.WriteLine("Output: " + input); if (input.Length < 3) keepdoing = false; } ...

Get Mono Kick Start 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.