Loops, foreach, Controls & 9 Practice Programs Masterclass

โšก C# 12 & .NET 8 ๐ŸŸข Chapter 10 of 35 ๐Ÿ“‚ Phase 4: Conditions & Loops ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this chapter: for Loop ยท while Loop ยท do-while Loop ยท foreach Loop ยท break & continue ยท Factorial ยท Prime Number ยท Fibonacci Series ยท Star Patterns

Welcome to Phase 4 (Chapter 10): C# Loops, foreach, Control Flow & 9 Practice Programs Masterclass! Loops iterate blocks of code until a condition evaluates to false. C# provides for, while, do-while, and foreach loops along with break and continue controls. This chapter includes 9 essential practice programs (Even/Odd, Largest of 3, Factorial, Prime, Fibonacci, Multiplication table, Number reversal, Star patterns, Menu calculator).

1Loop Types โ€” for, while, do-while, foreach
C# โ€” Loop Constructs โ–ถ Run in Compiler
// 1. for loop
for (int number = 1; number <= 5; number++)
{
    Console.WriteLine($"for count: {number}");
}

// 2. while loop
int w = 1;
while (w <= 3)
{
    Console.WriteLine($"while count: {w}");
    w++;
}

// 3. do-while loop (executes AT LEAST ONCE)
int d = 10;
do
{
    Console.WriteLine("do-while executes at least once!");
} while (d < 5);

// 4. foreach loop (safely iterates arrays and collections)
string[] courses = { "C#", "ASP.NET Core", "SQL", "Azure" };
foreach (string course in courses)
{
    Console.WriteLine($"Course: {course}");
}
29 Essential C# Practice Programs
C# โ€” 9 Complete Practice Solutions โ–ถ Run in Compiler
// 1. Even or Odd
int num = 17;
Console.WriteLine($"{num} is {(num % 2 == 0 ? "Even" : "Odd")}");

// 2. Largest of Three Numbers
int x = 25, y = 42, z = 18;
int max = (x > y && x > z) ? x : (y > z ? y : z);
Console.WriteLine($"Largest of ({x}, {y}, {z}) = {max}");

// 3. Factorial of a Number (5!)
int n = 5;
long factorial = 1;
for (int i = 1; i <= n; i++) factorial *= i;
Console.WriteLine($"Factorial of {n} = {factorial}");

// 4. Prime Number Check
int primeCandidate = 29;
bool isPrime = true;
for (int i = 2; i * i <= primeCandidate; i++)
{
    if (primeCandidate % i == 0) { isPrime = false; break; }
}
Console.WriteLine($"{primeCandidate} is Prime: {isPrime}");

// 5. Fibonacci Series (first 8 terms)
int f1 = 0, f2 = 1;
Console.Write($"Fibonacci: {f1} {f2} ");
for (int i = 2; i < 8; i++)
{
    int fNext = f1 + f2;
    Console.Write($"{fNext} ");
    f1 = f2;
    f2 = fNext;
}
Console.WriteLine();

// 6. Multiplication Table of 7
int tableNum = 7;
for (int i = 1; i <= 5; i++)
{
    Console.WriteLine($"{tableNum} x {i} = {tableNum * i}");
}

// 7. Number Reversal (12345 -> 54321)
int original = 12345, reversed = 0, temp = original;
while (temp > 0)
{
    reversed = (reversed * 10) + (temp % 10);
    temp /= 10;
}
Console.WriteLine($"Reversed {original} -> {reversed}");

// 8. Star Pyramid Pattern
for (int row = 1; row <= 4; row++)
{
    for (int col = 1; col <= row; col++)
    {
        Console.Write("* ");
    }
    Console.WriteLine();
}

// 9. Menu-Driven Calculator
int num1 = 10, num2 = 5;
char op = '+';
int calcResult = op switch
{
    '+' => num1 + num2,
    '-' => num1 - num2,
    '*' => num1 * num2,
    '/' => num1 / num2,
    _   => 0
};
Console.WriteLine($"Calculator ({num1} {op} {num2}) = {calcResult}");
3Technical FAQs

Q1: Can I modify array elements inside a foreach loop?

No! Iteration variables in foreach loops are read-only. To modify array values, use a standard for loop.