Thuta Learning
C#
BasicProgrammingBeginner

Loops

C#Lesson 1120 min

What you'll walk away with

  • Explain the core concept and runtime behavior of Loops
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

Explain it simply

Loops, in plain language

Loop is a way to run a block of code over and over. Whenever you need to print items from a list, count things, or process data, skipping the loop is like walking from Tokyo to Osaka instead of taking the train — technically possible, but needlessly exhausting.

Problem → Why → Solution

The problem — why this concept matters

Mutating a collection during enumeration, off-by-one bounds, or missing a termination update causes exceptions or infinite loops.

A common mistake

Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Core concept — understand how it works

Choose `for`, `foreach`, `while`, or `do` according to index control, sequence iteration, and condition-driven repetition. Verify termination, collection mutation, cancellation, and work per iteration.

Mental model
  1. typed input
  2. C# expression and method
  3. CLR value or object state

Explain the core concept and runtime behavior of Loops

Core syntax or pattern

csharp
// for loop: works well when the iteration count is known
for (int i = 1; i <= 3; i++)
{
    Console.WriteLine($"Step {i}");
}

// foreach loop: visits every item in a collection
string[] languages = { "C#", "Java", "PHP" };
foreach (string language in languages)
{
    Console.WriteLine(language);
}

Choose `for`, `foreach`, `while`, or `do` according to index control, sequence iteration, and condition-driven repetition. Verify termination, collection mutation, cancellation, and work per iteration.

Read the code step by step

  1. typed input// for loop: works well when the iteration count is k...

    Loop is a way to run a block of code over and over. Whenever you need to print items from a list, count things, or process data, skipping the loop is like walking from Tokyo to Osaka instead of taking the train — technically possible, but needlessly exhausting.

  2. C# expression and method// foreach loop: visits every item in a collection

    Choose `for`, `foreach`, `while`, or `do` according to index control, sequence iteration, and condition-driven repetition. Verify termination, collection mutation, cancellation, and work per iteration.

  3. CLR value or object state}

    Iterate a list while building a separate filtered result, testing empty, single-item, and multi-item inputs.

Execution flow
  1. typed input
  2. C# expression and method
  3. CLR value or object state

Success

Explain the core concept and runtime behavior of Loops

Failure

Mutating a collection during enumeration, off-by-one bounds, or missing a termination update causes exceptions or infinite loops.

Loop is a way to run a block of code over and over. Whenever you need to print items from a list, count things, or process data, skipping the loop is like walking from Tokyo to Osaka instead of taking the train — technically possible, but needlessly exhausting.

csharp
// for loop: works well when the iteration count is known
for (int i = 1; i <= 3; i++)
{
    Console.WriteLine($"Step {i}");
}

// foreach loop: visits every item in a collection
string[] languages = { "C#", "Java", "PHP" };
foreach (string language in languages)
{
    Console.WriteLine(language);
}

What this code does

  • for loop starts at i = 1 and keeps running as long as i <= 3 stays true.
  • i++ increases i by 1 after every pass through the loop.
  • foreach grabs each item in the array one by one and prints it.
You should see
Step 1 Step 2 Step 3 C# Java PHP

Info

⚠️ Common mistake

while loop, forgetting to update the condition can leave you with an infinite loop. Every time you write a loop, always ask yourself: "When does this actually stop?"

Real-world usage

How to apply it in a real project

Iterate a list while building a separate filtered result, testing empty, single-item, and multi-item inputs.

Mini challenge

Build it yourself

Iterate a list while building a separate filtered result, testing empty, single-item, and multi-item inputs.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
csharp
// for loop: works well when the iteration count is known
for (int i = 1; i <= 3; i++)
{
    Console.WriteLine($"Step {i}");
}

// foreach loop: visits every item in a collection
string[] languages = { "C#", "Java", "PHP" };
foreach (string language in languages)
{
    Console.WriteLine(language);
}

Expected: Explain the core concept and runtime behavior of Loops

Show hint

Iterate a list while building a separate filtered result, testing empty, single-item, and multi-item inputs.

Quick quiz 1 / 3

What best describes the core idea of Loops?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • typed input → C# expression and method → CLR value or object state
  • Choose `for`, `foreach`, `while`, or `do` according to index control, sequence iteration, and condition-driven repetition. Verify termination, collection mutation, cancellation, and work per iteration.
  • Mutating a collection during enumeration, off-by-one bounds, or missing a termination update causes exceptions or infinite loops.
  • Iterate a list while building a separate filtered result, testing empty, single-item, and multi-item inputs.

C# documentation — Microsoft Learn

Easy traps

  • Mutating a collection during enumeration, off-by-one bounds, or missing a termination update causes exceptions or infinite loops.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Iterate a list while building a separate filtered result, testing empty, single-item, and multi-item inputs.

You'll know it worked when: Step 1 Step 2 Step 3 C# Java PHP

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Loops | Thuta Learning