1.
Selection Statements (Decision Making)
These allow your program to choose a path based on a condition.
• if / else if / else: Executes a block if a condition is true.
• switch: Best for comparing a single variable against a list of constant values (like
a menu).
2. Iteration Statements (Loops)
These repeat a block of code multiple times.
• while: Repeats as long as a condition is true. Checks the
condition before running the code.
• do-while: Similar to while, but checks the condition after running the code,
ensuring it runs at least once.
• for: Ideal when you know exactly how many times you want to loop. It bundles
initialization, condition, and increment in one line.
• Range-based for: A modern way (C++11+) to easily iterate through every
element in a collection like an array or vector.
3. Jump Statements
These force the program to "jump" to another part of the code.
• break: Immediately exits the current loop or switch.
• continue: Skips the rest of the current loop iteration and moves to the next one.
• return: Exits the current function and optionally sends back a value.
1. Decision Making (Selection)
This is your program asking a question.
• if: "If it is raining, take an umbrella."
• else: "...otherwise, wear sunglasses."
• switch: Like a vending machine. You press button A, you get a soda; button B,
you get water. It's a faster way to handle many specific choices.
2. Repeating Tasks (Loops)
This is for when you don't want to write the same line of code 100 times.
• for: "Do 10 pushups." (You know exactly how many to do before you start).
• while: "Keep scrubbing the floor while it's still dirty." (You don't know how
long it will take, you just check the condition repeatedly).
• do-while: "Eat a slice of pizza, then check if you're still hungry." (You always
do the action at least once).
3. Cutting in Line (Jump Statements)
These are your "emergency exits."
• break: "I'm in the middle of a workout, but I feel a sharp pain—stop
everything and leave the gym now."
• continue: "I'm cleaning the house; I'll skip the guest room because it's
already clean, but I'll keep cleaning the rest of the rooms."
How it looks in code:
cpp
int energy = 5;
if (energy > 0) {
for (int i = 1; i <= 3; i++) {
cout << "Doing lap number " << i << endl;
} else {
cout << "Time to sleep!";