if/else, loops, switch/case, ternary operator, goto, and loop control with break and continue.
if (condition) {
// runs when condition is true
} else if (otherCondition) {
// runs when otherCondition is true
} else {
// runs when none of the above are true
}
{ } even for single-line bodies. Omitting them is a common source of bugs when adding lines later.Evaluates the condition before each iteration. If the condition is false from the start, the body never runs.
int i = 0;
while (i < 10) {
// ...
++i;
}
The key difference from a regular while: the condition is evaluated after the body, so the body is guaranteed to run at least once.
do {
// ...
} while (expression);
The classic for loop has three optional sections: init, condition, and end expression.
for (int i = 0; i < count; ++i) {
// ...
}
Any or all of the three sections can be omitted. An empty for loop runs forever:
for (;;) { // infinite loop — equivalent to while(true)
// ...
}
You can declare multiple variables in the init section, but they must be the same type:
for (int i = 0, j = 10; i < j; ++i, --j) {
// i goes up, j goes down
}
The exit condition and end expression can reference variables from outside the loop, and the end expression can use the comma operator to update multiple variables:
int extra = 5;
for (int i = 0; i < count && extra > 0; ++i, --extra) {
// ...
}
Iterates over every element in a range (array, vector, string, etc.) without needing an index:
int nums[] = {1, 2, 3, 4, 5};
for (int n : nums) {
std::cout << n << "\n";
}
const auto& as the loop variable type to avoid copying elements and to make intent clear when you don't need to modify them: for (const auto& item : collection).continueSkips the rest of the current iteration and jumps back to the top of the loop to re-evaluate the condition.
for loops, the end expression (++i) is also evaluated before re-checking the conditionfor (int i = 0; i < 10; ++i) {
if (i % 2 == 0) continue; // skip even numbers
std::cout << i; // prints 1 3 5 7 9
}
breakImmediately exits the entire loop block. Execution resumes at the statement after the closing brace.
for (int i = 0; i < 100; ++i) {
if (i == 5) break; // exits when i reaches 5
std::cout << i;
}
Checks the result of an expression against a set of constant values. More readable than a long chain of if/else if when matching a single variable to many possible values.
switch (expression) {
case CONSTANT_A:
// ...
break;
case CONSTANT_B:
// ...
break;
default: // optional — runs if no case matched
// ...
break;
}
Without a break, execution falls through into the next case and continues evaluating — it does not stop until it hits a break or the end of the switch. This is usually a bug, but can be intentional.
case labels — no variables, no ranges, no expressions. switch also only works with integer types and enums.// Intentional fallthrough example — C++17 [[fallthrough]] suppresses the warning
switch (grade) {
case 'A':
case 'B':
std::cout << "Pass"; // both A and B reach this
[[fallthrough]];
case 'C':
std::cout << "Marginal";
break;
}
A compact inline if/else that produces a value. Syntax: condition ? if_true : if_false
int max = (a > b) ? a : b;
// equivalent to:
// if (a > b) max = a;
// else max = b;
goto instructs the program to jump to a labeled point in the code.
Start:
// ...
goto Start; // jumps back to the label
goto leads to "spaghetti code" — unstructured jumps that make program flow impossible to reason about. Any use of goto can be replaced with structured loops and functions. Compilers and code reviews will flag it.