0Pricing
C# Academy · Lesson

if/else, switch (classic), loops: for, while, do, foreach

Use if/else to branch, classic switch for discrete cases, and core loops (for, while, do, foreach) to repeat work clearly.

Overview

Goal: Choose actions with if/else, map discrete cases with switch, and repeat work with for, while, do, and foreach.

if/else basics

Use if/else for simple, readable branching; keep conditions small.

using System;

public class Program
{
  public static void Main(string[] args)
  {
    int temperature = 18;

    if (temperature >= 25)
    {
      Console.WriteLine("Warm");
    }
    else if (temperature >= 15)
    {
      Console.WriteLine("Mild");
    }
    else
    {
      Console.WriteLine("Cold");
    }
  }
}

All lessons in this course

  1. if/else, switch (classic), loops: for, while, do, foreach
  2. break/continue; goto (avoid); scope; exceptions vs guard checks
  3. Simple diagnostics: exceptions vs guard checks
← Back to C# Academy