<style> .reveal-up, .reveal-left, .reveal-right, .reveal-stagger > * { opacity: 1 !important; transform: none !important; } </style>
  1. Learn
  2. C#
  3. Control flow
All lessons in this course

Lesson 7 of 60 12 minBeginner

Decisions with if / else

Make your program choose: if, else, else if, nested conditions, the ternary operator and common traps.

So far every line ran from top to bottom. Real programs must decide: show an error if the password is wrong, give a discount if the total is high. The if statement runs code only when a condition is true.

Program.cs
int temperature = 38; if (temperature > 35){    Console.WriteLine("It's hot. Drink water!");}Console.WriteLine("Done");
Output
It's hot. Drink water!
Done

if / else: one of two paths

Program.cs
int age = 16; if (age >= 18){    Console.WriteLine("You can vote.");}else{    Console.WriteLine($"Wait {18 - age} more years.");}
Output
Wait 2 more years.

else if: many paths, first match wins

Chain conditions with else if. C# checks them in order and runs only the first branch whose condition is true; the rest are skipped. Drag the slider and watch:

Interactive

Follow the decision path

Drag the slider. C# checks the conditions from top to bottom and runs only the first branch that is true.

marks >= 90?
false ↓
grade = "A"
marks >= 75?
false ↓
grade = "B"
marks >= 50?
true →
grade = "C"
Result: grade = "C" · 3 conditions checked
Grade.cs
int marks = 72;string grade; if (marks >= 90)    grade = "A";else if (marks >= 75)    grade = "B";else if (marks >= 50)    grade = "C";else    grade = "Fail"; Console.WriteLine($"Grade: {grade}");

Combining conditions

Program.cs
int age = 25;bool hasLicence = true; if (age >= 18 && hasLicence)    Console.WriteLine("You may drive."); string day = "Sunday";if (day == "Saturday" || day == "Sunday")    Console.WriteLine("Weekend!");
Output
You may drive.
Weekend!

Nested if

Program.cs
bool loggedIn = true;bool isAdmin = false; if (loggedIn){    if (isAdmin)        Console.WriteLine("Admin dashboard");    else        Console.WriteLine("User dashboard");}else{    Console.WriteLine("Please log in");}
Output
User dashboard

The ternary operator ? :

For a simple “choose one of two values”, condition ? valueIfTrue : valueIfFalse is shorter than a full if/else.

Program.cs
int stock = 0;string label = stock > 0 ? "In stock" : "Sold out";Console.WriteLine(label);
Output
Sold out

Common mistakes

  • if (x = 5): assignment, not comparison. Use ==. (C# reports an error here.)
  • if (x > 5); with a semicolon: the if controls an empty statement, and the block below always runs.
  • Comparing decimals with ==: 0.1 + 0.2 == 0.3 is false for double. Compare with a tolerance or use decimal.
Quick check

With int x = 80;, what prints? if (x > 50) Console.Write("A"); else if (x > 70) Console.Write("B"); else Console.Write("C");

Quick check

What does int n = 7; string s = n % 2 == 0 ? "even" : "odd"; store in s?

Practice

Read a year from the user and print whether it is a leap year. A year is a leap year if it is divisible by 4, except years divisible by 100, unless they are also divisible by 400 (2000 is, 1900 is not).

Show a hint

(year % 4 == 0 && year % 100 != 0) || year % 400 == 0

Show the solution
Solution.cs
Console.Write("Year: ");if (int.TryParse(Console.ReadLine(), out int year)){    bool leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;    Console.WriteLine(leap ? $"{year} is a leap year" : $"{year} is not a leap year");}else{    Console.WriteLine("Invalid year");}
Output
Year: 2024
2024 is a leap year

Remember

  • if runs a block only when its condition is true; else handles the rest.
  • In an else if chain only the first true branch runs, so order matters.
  • Combine conditions with &&, ||, !.
  • Use cond ? a : b to choose between two values.
0 of 60 lessons completed