Back to course

Conditional Statements (If, Else, Switch)

.NET Zero to Hero: Master C# and Modern App Development

Control Flow: Decisions

Use conditions to run code based on logic.

If-Else Statement:

csharp int age = 18; if (age >= 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Minor"); }

Switch Statement:

Useful when comparing one variable against multiple values: csharp int day = 2; switch (day) { case 1: Console.WriteLine("Mon"); break; case 2: Console.WriteLine("Tue"); break; default: Console.WriteLine("Other"); break; }