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.
- typed input
- C# expression and method
- CLR value or object state
Explain the core concept and runtime behavior of Loops
Core syntax or pattern
// 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
- 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.
- C# expression and method
// foreach loop: visits every item in a collectionChoose `for`, `foreach`, `while`, or `do` according to index control, sequence iteration, and condition-driven repetition. Verify termination, collection mutation, cancellation, and work per iteration.
- CLR value or object state
}Iterate a list while building a separate filtered result, testing empty, single-item, and multi-item inputs.
- typed input
- C# expression and method
- 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.
// 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
forloop starts ati = 1and keeps running as long asi <= 3stays true.i++increasesiby 1 after every pass through the loop.foreachgrabs each item in the array one by one and prints it.
Step 1 Step 2 Step 3 C# Java PHPInfo
⚠️ 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
// 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
Quick quiz 2 / 3
Quick quiz 3 / 3
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