Understanding iteration statements
Iteration statements repeat a block of statements either while a condition is true or for each item in a collection. The choice of which statement to use is based on a combination of ease of understanding to solve the logic problem and personal preference.
Looping with the while statement
The while
statement evaluates a Boolean expression and continues to loop while it is true. Let's explore iteration statements:
- Use your preferred coding tool to add a new Console Application to the
Chapter03
workspace/solution namedIterationStatements
. - In Visual Studio Code, select
IterationStatements
as the active OmniSharp project. - In
Program.cs
, type statements to define awhile
statement that loops while an integer variable has a value less than 10, as shown in the following code:int x = 0; while (x < 10) { WriteLine(x); x++; }
- Run the code and view the results, which should be the numbers 0...