Changes

Jump to: navigation, search

C Sharp Looping with do and while Statements

2 bytes removed, 19:14, 17 January 2008
C# do ... while loops
In the above example the ''while'' expression will evaluate whether the ''myCount'' variable is less than 100. If it is already greater than 100 the code in the braces is skipped and the loop exits without performing any tasks. If, on the other hand, ''myCount'' is not greater than 100 the code in the braces is executed and the loop returns to the ''while'' statement and repeats the evaluation of ''myCount''. This process repeats until the value of ''myCount'' is greater than 100, at which point the loop exits.
=== C# do ... while loops ===
It is often helpful to think of the ''do ... while'' loop as an inverted ''while'' loop. The ''while'' loop evaluates an expression before executing the code contained in the body of the loop. If the expression evaluates to ''false'' on the first check then the code is not executed. The ''do .. while'' loop, on the other hand, is provided for situations where you know that the code contained in the body of the loop will ''always'' need to be executed at least once. For example, you may want to keep stepping through the items in an array until a specific item is found. You know that you have to at least check the first item in the array to have any hope of finding the entry you need. The syntax for the ''do ... while'' loop is as follows:

Navigation menu