Search⌘ K
AI Features

Understanding Iteration Statements

Learn how to use C# iteration statements including for while do and foreach to control repeated execution of code blocks. Understand the syntax and behavior of these loops and practice running them with example code to solidify your knowledge.

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 statement choice 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:

Step 1: Use your preferred coding tool to add a new "Console App/console" project named IterationStatements to the Chapter03 workspace/solution.

  • In Visual Studio Code, select IterationStatements as the active OmniSharp project.

Step 2: In the Program.cs, delete the existing statements and then add statements to define a while statement that loops while an integer variable has a value less than 10, as shown in the ... ...