Lecture 3
if-else statements
Hopefully, we still remember conditional statements. But as a quick primer, conditional statements allow us to gate the execution of code. Here's a quick example from Python-land to jog our memories.
The idea doesn't really change in C++. For the equivalent code as above, we'll have the following implementation
else-if statements
We can have more than just an if and an else statement. We can chain intermediate conditions using an else-if statement.
This is equivalent to the elif statement in Python.
In an else-if chain, execution stops at the first matching condition; remaining clauses are skipped entirely.
This is different from writing separate if statements: if you replaced each else if with a standalone if, a number like 15 would match multiple conditions and print both Fizz and Buzz instead of FizzBuzz.
Nested conditional statements
Nested conditionals are if statements placed inside the body of another if or else block. Python has the exact same concept — the only difference in C++ is that we use braces instead of indentation to define the blocks.
A good use case is when an inner condition only makes sense if an outer condition is already true. For example, we only care about which letter grade a student earned if they're passing in the first place:
Nesting is appropriate when the inner condition only makes sense if the outer condition is true —
as above, there's no point classifying the letter grade if the student is already failing. If the
conditions are independent of each other, prefer else-if or separate if statements instead.
Deep nesting (3+ levels) quickly becomes hard to read and reason about. If you find yourself
nesting more than two levels deep, it's usually a sign to restructure using else-if chains or to
extract the inner logic into a function (we'll cover functions in a later lecture).
switch statements
A switch statement is a cleaner alternative to a long else-if chain when you need to match a single variable against a set of discrete constant values. Python didn't have an equivalent until match/case in 3.10, so this may look new.
The default case acts like the final else — it runs if none of the case values matched.
Without a break at the end of a case, execution falls through into the next case — even if its condition didn't match. For example:
If day is 6, this prints both Saturday and Sunday. Fallthrough is occasionally used intentionally, but it's almost always a bug.
You can only switch on int, char, enum, and other integer-like types. Switching on a
std::string or a double is a compile error — use an else-if chain for those cases instead.
Loops
Loops let us repeat a block of code without writing it out multiple times. C++ has two main loop types: for and while.
for loops
Use a for loop when you know how many times you want to iterate. The loop header has three parts separated by semicolons: initialization, condition, and update.
This prints 0 through 4. The Python equivalent is for i in range(5) — same idea, just different syntax.
while loops
Use a while loop when you don't know ahead of time how many iterations you'll need — the loop runs as long as its condition remains true.
A classic use case is input validation: keep prompting the user until they give you a valid value.
If the loop condition never becomes false, your program will run forever. Always make sure
something inside the loop body can eventually make the condition false — in the example above,
reading a new value of age on each iteration is what gives the loop a way to exit.
Exercises
Q1.
Answer the following questions about switch statements:
- Give one scenario where a
switchis preferable to anelse-ifchain. What makes it cleaner? - Give one scenario where you cannot use a
switchand must useelse-ifinstead. Why? - What happens if you omit
breakfrom every case in aswitch? Walk through an example.
Q2.
Without running the code, determine what this program prints. Then verify by compiling and running.
Q3.
Write a complete C++ program that reads integers from the user one at a time and keeps a running total. Stop reading when the user enters 0, then print the sum. Do not include 0 in the sum.
Example:
Q4.
The program below is supposed to print the season for a given month number (1–12), but it has bugs. Identify what's wrong in each case, explain why, and write a corrected version.