Understanding iteration statements
Iteration statements repeat a block of statements either while a condition is true
(while
and for
statements) or for each item in a collection (foreach
statement). 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 App /
console
project namedIterationStatements
to theChapter03
solution. - In
Program.cs
, delete the existing statements and then add 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 to...