The while Statement

To continue executing an embedded statement as long as a certain condition holds true, the while statement is the ideal choice. Its shape is as follows:

while (condition)   embedded-statement

Just as with the if statement, the condition is a Boolean-valued expression, or to be completely accurate, something implicitly convertible to a Boolean. Once more, this means null checks or nonzero checks need to be made explicit, as opposed to C and C++, where it’s not uncommon to write code like this:

// How one would copy a string in C or C++.while (*cpy++ = *str++)    ;

Side-effecting assignment, treatment of a character as a truth value, and other wackiness with pointer arithmetic—no way in C#. (For the curious, the preceding code ...

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