C# Control Flow

Control flow determines which code runs based on conditions and repeats code with loops. C# has familiar structures with a few C#-specific improvements.

if / else if / else

CSHARP
int score = 85;

if (score >= 90)
{
    Console.WriteLine("Grade: A");
}
else if (score >= 80)
{
    Console.WriteLine("Grade: B");
}
else if (score >= 70)
{
    Console.WriteLine("Grade: C");
}
else
{
    Console.WriteLine("Grade: F");
}

Note: C# uses { } braces for all blocks — even single-line bodies. This prevents bugs when adding lines later.

Ternary Operator

A compact if/else for simple assignments:

CSHARP
string result = score >= 50 ? "Pass" : "Fail";

switch Expression (C# 8+)

Modern pattern matching — concise and exhaustive:

CSHARP
string day = "Monday";

string type = day switch
{
    "Saturday" or "Sunday" => "Weekend",
    "Monday"              => "Start of work week",
    "Friday"              => "Almost weekend!",
    _                     => "Weekday",  // _ = default
};

Console.WriteLine(type); // "Start of work week"

Traditional switch

CSHARP
switch (day)
{
    case "Saturday":
    case "Sunday":
        Console.WriteLine("Weekend");
        break;
    case "Monday":
        Console.WriteLine("Start of work week");
        break;
    default:
        Console.WriteLine("Weekday");
        break;
}

for Loop

CSHARP
for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i); // 0, 1, 2, 3, 4
}

foreach Loop

Iterate over collections (arrays, lists, dictionaries):

CSHARP
string[] cities = { "Kathmandu", "Pokhara", "Chitwan" };

foreach (string city in cities)
{
    Console.WriteLine(city);
}

// With index
for (int i = 0; i < cities.Length; i++)
{
    Console.WriteLine($"{i}: {cities[i]}");
}

while and do-while

CSHARP
// while — checks condition before each iteration
int count = 0;
while (count < 5)
{
    Console.WriteLine(count);
    count++;
}

// do-while — runs at least once, then checks
int num = 10;
do
{
    Console.WriteLine(num);
    num--;
} while (num > 0);

break and continue

CSHARP
// break — exit the loop immediately
for (int i = 0; i < 100; i++)
{
    if (i == 5) break;   // stops at 5
    Console.WriteLine(i);
}

// continue — skip to the next iteration
for (int i = 0; i < 10; i++)
{
    if (i % 2 == 0) continue;  // skip even numbers
    Console.WriteLine(i);       // prints 1, 3, 5, 7, 9
}

Pattern Matching in if

C# lets you match patterns directly in conditions:

CSHARP
object obj = 42;

if (obj is int number && number > 0)
{
    Console.WriteLine($"Positive int: {number}");
}

if (obj is string { Length: > 5 } longString)
{
    Console.WriteLine($"Long string: {longString}");
}

Tips

  • Always use braces { } even for single-line if/else — it prevents bugs.
  • Use switch expressions over traditional switch when possible — they are more concise and enforce exhaustiveness.
  • Prefer foreach over for when you do not need the index.
  • Use break and continue sparingly — they can make loops hard to follow.

Next: methods — reusable blocks of behaviour.