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:

C#

With else:

C#

With else if (multiple choices):

C#

Real example – Age checker:

C#

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):

C#

Modern switch expression (C# 8+ – Cleaner & Recommended in 2026!)

C#

3. Ternary Operator – Short if-else (Very Common!)

Syntax: condition ? valueIfTrue : valueIfFalse

Examples:

C#

Real use:

C#

4. Loops – Repeating Code

Loops let you repeat code many times.

A. for loop – When you know how many times to repeat

C#

Real example – Print multiplication table:

C#

B. foreach loop – Best for going through collections (arrays, lists)

C#

C. while loop – Repeat while condition is true

C#

Real use – Keep asking until correct password:

C#

D. do-while loop – Run at least once, then check condition

C#

5. break, continue, goto (Rarely used – But Good to Know)

  • break → Immediately exit the loop/switch
C#
  • continue → Skip the rest of this loop iteration, go to next
C#
  • goto → Jump to a labeled place (almost never used – considered bad style)
C#

Rule: Use goto only in very special cases (like deep nested loops). Most modern C# code never uses it.

Mini-Project: Number Guessing Game

C#

Your Homework (Super Fun!)

  1. Create a new console project called DecisionMaster
  2. 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
  3. Bonus: Add a for loop that prints stars like this based on user input:
text

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! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *