Chapter 6: Control Flow Statements
1. if, else, else if – Making Decisions
The if statement is the most important decision-making tool in programming.
Basic syntax:
|
0 1 2 3 4 5 6 7 8 9 |
if (condition) { // Code runs ONLY if condition is true } |
With else:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
if (condition) { // true } else { // false } |
With else if (multiple choices):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
if (condition1) { // ... } else if (condition2) { // ... } else { // none of the above } |
Real example – Age checker:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Console.Write("Enter your age: "); var ageInput = Console.ReadLine(); var age = int.Parse(ageInput); if (age >= 18) { Console.WriteLine("You are an adult! 🎉 You can vote, drive, and drink coffee freely!"); } else if (age >= 13) { Console.WriteLine("You are a teenager! 😎 Time to explore the world!"); } else if (age >= 0) { Console.WriteLine("You are a child! 🧸 Keep smiling and learning!"); } else { Console.WriteLine("Wait... negative age? Are you a time traveler? 🤔"); } |
2. Switch Statement – Clean Way for Multiple Exact Matches
switch is perfect when you have one value and want to compare it against many possible exact values.
Classic switch (C# before 8):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Console.Write("Enter day number (1-7): "); var day = int.Parse(Console.ReadLine()); switch (day) { case 1: Console.WriteLine("Monday – Start of the week!"); break; case 2: Console.WriteLine("Tuesday"); break; case 3: Console.WriteLine("Wednesday"); break; case 4: Console.WriteLine("Thursday"); break; case 5: Console.WriteLine("Friday – Almost weekend! 🎉"); break; case 6: Console.WriteLine("Saturday – Weekend vibes!"); break; case 7: Console.WriteLine("Sunday – Rest day ☀️"); break; default: Console.WriteLine("Invalid day number!"); break; } |
Modern switch expression (C# 8+ – Cleaner & Recommended in 2026!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Console.Write("Enter day number (1-7): "); var day = int.Parse(Console.ReadLine()); string message = day switch { 1 => "Monday – Start of the week!", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday – Almost weekend! 🎉", 6 => "Saturday – Weekend vibes!", 7 => "Sunday – Rest day ☀️", _ => "Invalid day number!" // _ = default }; Console.WriteLine(message); |
3. Ternary Operator – Short if-else (Very Common!)
Syntax: condition ? valueIfTrue : valueIfFalse
Examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
var age = 20; string status = age >= 18 ? "Adult" : "Minor"; Console.WriteLine($"You are an {status}."); // Nested ternary (be careful – can get messy) string message = age >= 18 ? (age >= 60 ? "Senior Citizen" : "Adult") : "Child"; |
Real use:
|
0 1 2 3 4 5 6 7 8 |
var temperature = 32; string feeling = temperature > 30 ? "Hot 🔥" : "Pleasant 😊"; Console.WriteLine($"It's {feeling} today!"); |
4. Loops – Repeating Code
Loops let you repeat code many times.
A. for loop – When you know how many times to repeat
|
0 1 2 3 4 5 6 7 8 9 10 |
for (int i = 1; i <= 5; i++) { Console.WriteLine($"Count: {i}"); } // Output: 1, 2, 3, 4, 5 |
Real example – Print multiplication table:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
Console.Write("Which number's table? "); var num = int.Parse(Console.ReadLine()); for (int i = 1; i <= 10; i++) { Console.WriteLine($"{num} × {i} = {num * i}"); } |
B. foreach loop – Best for going through collections (arrays, lists)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
string[] friends = { "Rahul", "Priya", "Amit", "Sneha" }; foreach (string friend in friends) { Console.WriteLine($"Hello, {friend}! How are you? 😊"); } |
C. while loop – Repeat while condition is true
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
int count = 0; while (count < 5) { Console.WriteLine($"Count is {count}"); count++; } |
Real use – Keep asking until correct password:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
string password = ""; while (password != "secret123") { Console.Write("Enter password: "); password = Console.ReadLine(); if (password != "secret123") { Console.WriteLine("Wrong password! Try again."); } } Console.WriteLine("Welcome! Access granted! 🔓"); |
D. do-while loop – Run at least once, then check condition
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
string answer; do { Console.Write("Do you want to continue? (yes/no): "); answer = Console.ReadLine()?.ToLower(); } while (answer == "yes"); Console.WriteLine("Okay, goodbye! 👋"); |
5. break, continue, goto (Rarely used – But Good to Know)
- break → Immediately exit the loop/switch
|
0 1 2 3 4 5 6 7 8 9 10 |
for (int i = 1; i <= 10; i++) { if (i == 5) break; // Stops at 5 Console.WriteLine(i); // Prints 1 2 3 4 } |
- continue → Skip the rest of this loop iteration, go to next
|
0 1 2 3 4 5 6 7 8 9 10 |
for (int i = 1; i <= 10; i++) { if (i % 2 == 0) continue; // Skip even numbers Console.WriteLine(i); // Prints only odd: 1 3 5 7 9 } |
- goto → Jump to a labeled place (almost never used – considered bad style)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
Console.WriteLine("Start"); goto End; Console.WriteLine("This will be skipped"); End: Console.WriteLine("End"); |
Rule: Use goto only in very special cases (like deep nested loops). Most modern C# code never uses it.
Mini-Project: Number Guessing Game
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Console.WriteLine("=== Number Guessing Game ===\n"); Console.WriteLine("I'm thinking of a number between 1 and 100. Can you guess it?"); var random = new Random(); var secret = random.Next(1, 101); var attempts = 0; while (true) { Console.Write("Your guess: "); var guess = int.Parse(Console.ReadLine()); attempts++; if (guess == secret) { Console.WriteLine($"Congratulations! You got it in {attempts} attempts! 🎉"); break; } else if (guess < secret) { Console.WriteLine("Too low! Try higher ↑"); } else { Console.WriteLine("Too high! Try lower ↓"); } } Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(); |
Your Homework (Super Fun!)
- Create a new console project called DecisionMaster
- Make a simple menu program:
- Ask user: “Choose an option: 1) Greet 2) Joke 3) Exit”
- Use switch expression to show different messages
- Use do-while to keep showing the menu until they choose Exit
- Bonus: Add a for loop that prints stars like this based on user input:
|
0 1 2 3 4 5 6 7 8 9 10 |
* ** *** **** ***** |
Next lesson: Arrays and Collections (Basics) – we’re going to start storing lots of data at once!
You’re doing absolutely fantastic! 🎉 Any part confusing? Want more examples? Just tell me — I’m right here for you! 💙
