Chapter 6: Control Flow

Control Flow in C++: Making Your Program Think and Decide!

Hello my superstar student! 🌟 Welcome to one of the most important chapters in C++ β€” Control Flow! Up until now, our programs ran straight from top to bottom, line by line. Today we’re going to teach the computer how to make decisions, repeat things, and choose different paths β€” just like we do in real life!

We’ll cover everything in great detail:

  • if, else if, else – decision making
  • switch statement (including the cool C++17+ version with initializer)
  • Loops: for, while, do-while, and the modern range-based for
  • break, continue, and goto (and why we rarely use goto)

We’ll go step by step with tons of examples, real-life analogies, common mistakes, and best practices that professional developers actually use.

Let’s start making decisions!

1. The if, else if, else Statements – Decision Making

Real-life analogy: β€œIf it’s raining, take an umbrella. Else if it’s sunny, wear sunglasses. Else, just go out normally.”

Syntax:

C++

Important:

  • The condition must evaluate to bool (true or false)
  • You can have as many else if as you want
  • else is optional

Classic example – Grade calculator:

C++

Common mistake – forgetting braces {}:

C++

Fix: Always use braces β€” even for one line (it prevents bugs when you add more lines later).

2. The switch Statement – Multiple Choices

Best used when you are checking one variable against many constant values.

Syntax (classic):

C++

Very important:

  • break; is required β€” otherwise it falls through to the next case (sometimes useful, usually a bug!)
  • default: is optional

Example – Day of the week:

C++

Modern C++17+ Feature: Switch with Initializer

You can declare and initialize a variable inside the switch β€” very clean!

C++

3. Loops – Repeating Code

Loops let us repeat code β€” very powerful!

A. for Loop – Best when you know how many times to repeat

C++

Example – Print numbers 1 to 10:

C++

Modern C++ style (very common):

C++

B. while Loop – Repeat while condition is true

C++

Example – Keep asking until correct password:

C++

C. do-while Loop – Run at least once

C++

Example – Menu that keeps showing until user exits:

C++

D. Range-based for Loop – Modern & Beautiful (C++11+)

Best for looping over arrays, vectors, strings, etc.

C++

Even better (reference to avoid copying):

C++

4. break, continue, and goto

break – Jump out of the loop/switch immediately

C++

continue – Skip the rest of this iteration, go to next

C++

goto – Jump to a label (almost never use!)

C++

Teacher’s advice: Never use goto in modern C++. It makes code hard to read and debug. Use break, continue, functions, or exceptions instead.

5. Full Mini Project – Number Guessing Game

C++

Your Mini Homework (Try These!)

  1. Write a program that asks for a number and prints whether it is:
    • Positive, Negative, or Zero
    • Even or Odd
  2. Print the multiplication table of 5 (5 Γ— 1 = 5, up to 5 Γ— 10 = 50) using a for loop.
  3. Make a program that keeps asking for numbers until the user enters 0, then prints the sum of all entered numbers.
  4. Use a range-based for loop to print each character of your name on a new line.

You’re doing incredibly well! Control flow is where your programs start becoming smart and interactive.

Next lesson: Functions β€” how to organize code into reusable blocks!

Any questions? Confused about do-while vs while? Want more examples with switch or range-based for? Just ask β€” your friendly C++ teacher is right here for you! πŸš€

You may also like...

Leave a Reply

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