C# Loops

It's often necessary to perform a sequence of logic multiple times in a program. For example, there might be a list of some items where each item needs the same processing. This processing is performed with language constructs called loops. In C# there are four types of loops—the while loop, the do loop, the for loop, and the foreach loop. Each has its own benefits for certain tasks.

while Loops

If it's necessary to continually execute a group of statements while a condition is true, use the while loop. The general form of the while loop is as follows:

While (Boolean expression)
[{]
    true condition statement(s)
[}]

When the Boolean expression evaluates to true, the true condition statements are executed. The following example shows ...

Get C# Unleashed 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.