C# Tutorial

Control Flow

if/else, switch, and the various loop types in C#

Conditional Logic (if)

In C#, the if statement evaluates a boolean expression to branch your code.
csharp
if (age >= 18) {
    Console.WriteLine("Adult");
} else {
    Console.WriteLine("Minor");
}

The Modern switch

C# has a very powerful switch statement that has evolved into a concise Switch Expression in recent versions.
csharp
string result = status switch {
    "Active" => "Welcome!",
    "Pending" => "Please wait.",
    _ => "Unknown status" // The default case
};

Loops in C#

Wait! C# provides four main types of loops. Choosing the right one makes your code cleaner.
  1. for: Best for when you know exactly how many times to run.
  2. foreach: The most common way to iterate through a collection (List, Array, etc.).
  3. while: Runs as long as a condition is true.
  4. do while: Runs at least once, then checks the condition.
csharp
var fruits = new[] { "Apple", "Banana", "Cherry" };

foreach (var fruit in fruits) {
    Console.WriteLine(fruit);
}

Branching Keywords

  • break: Exits the loop immediately.
  • continue: Skips the rest of the current iteration and starts the next one.
  • return: Exits the entire method.
[!TIP] Always prefer foreach over for when working with collections. It's safer because you can't accidentally go "out of bounds."

Summary

KeywordDescription
if / elseBasic branching
switchMulti-way branching
foreachIterating through collections
whileConditional looping
breakStop the loop
continueSkip current step
yieldUsed for creating custom iterators (Advanced)
LogicControl flow dictates the "intelligence" of your app
SafetyCompiler ensures all paths return a value in methods
ModernSwitch expressions make code much more functional and clean
Identityforeach works on any type that implements IEnumerable
StandardUse braces {} even for single-line if statements for clarity