Yesterday's report printed Left: -15 minutes once the sessions passed the goal. A person would never write that; they would say "Goal reached." To do the same, a program must look at a value and choose what to do next. An if statement makes that choice: it runs its block only when a condition is true, and an else block handles the other case.
A condition is any expression that produces a bool. Comparisons are the most common: <, <=, >, >=, == for "equal to" and != for "not equal to". A bool variable is already a condition, so if (done) reads naturally. Keep braces around every branch, even a one-line branch, so adding a second line later cannot silently fall outside the if.
Predict and run
An else-if chain checks its conditions in order and takes the first one that is true. Which message prints for 25?
#include <iostream>
int main() {
int minutes = 25;
if (minutes <= 0) {
std::cout << "Invalid\n";
} else if (minutes >= 25) {
std::cout << "Focus\n";
} else {
std::cout << "Short\n";
}
return 0;
}Check your prediction
FocusThe first condition was false, the second was true, so Focus printed and the chain stopped. The final else never ran. Try 0, 1, 24 and 25 and say which branch owns each boundary before you run. The value 25 belongs to Focus because the comparison is >=, not >.
Order decides the answer
Because the chain takes the first true condition, the order of the branches is part of the logic. Put the most specific or most important checks first, especially rejections of invalid values. Compare two chains that use the same three conditions:
#include <iostream>
int main() {
int minutes = 90;
// Wrong order: the broad check swallows the specific one.
if (minutes >= 25) {
std::cout << "Focus\n";
} else if (minutes > 60) {
std::cout << "Long\n";
}
// Right order: the narrow check comes first.
if (minutes > 60) {
std::cout << "Long\n";
} else if (minutes >= 25) {
std::cout << "Focus\n";
}
return 0;
}Focus
LongIn the first chain, 90 is at least 25, so Focus wins and the Long branch can never run for any value. The compiler does not warn about unreachable branches like this. A good test is to ask, for each branch, "what value would reach it?" If you cannot name one, the order is wrong.
= is not ==
A single = assigns; a double == compares. Writing if (minutes = 0) does not test anything: it stores 0 in minutes and then uses that 0 as the condition, which counts as false. Your program would quietly destroy its data and take the wrong branch.
int minutes = 25;
if (minutes = 0) { // assigns 0, then tests it: always false
std::cout << "Empty\n";
}Clang warns about this even without extra flags, so the browser shows a -Wparentheses warning, but a warning does not stop the build: the program runs and takes the wrong branch. Day 7's local build adds -Werror, which turns the warning into an error:
main.cpp:5:17: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]When you see that message, you almost always meant ==.
Each branch is its own scope
A variable declared inside a branch's braces exists only inside those braces. When the branch ends, the variable is gone. If both branches need to produce a value you will use afterward, declare the variable before the if and assign to it inside each branch.
#include <iostream>
#include <string>
int main() {
int total = 95;
const int goal = 90;
std::string message = "";
if (total >= goal) {
message = "Goal reached";
} else {
message = "Keep going";
}
std::cout << total << " of " << goal << ": " << message << "\n";
return 0;
}95 of 90: Goal reachedmessage lives in main's scope, so it still exists after the if finishes. Had you written std::string message = "Goal reached"; inside the first branch, that would create a second, short-lived variable that hides the outer one. The assignment would vanish at the closing brace, and the output would end with an empty label. Without the outer declaration at all, the output line would not compile, because no message exists there.
Practice
Your turn
Classify a session length stored in int minutes. Print the value, a colon and one label:
Invalidfor 0 or lessShortfor 1 through 24Focusfor 25 through 60Longfor more than 60
With minutes set to 45, the output is 45: Focus. Then edit the value to 0, 24, 25, 60 and 61 and predict each label before running.
#include <iostream>
int main() {
int minutes = 45;
// Write your chain here.
return 0;
}Hint 1: the idea
Reject invalid values first. After that, each later branch already knows the earlier conditions were false, so it only needs one comparison.
Hint 2: a first step
Print minutes << ": " before the chain, then start with if (minutes <= 0), followed by else if (minutes < 25).
Show one solution
#include <iostream>
int main() {
int minutes = 45;
std::cout << minutes << ": ";
if (minutes <= 0) {
std::cout << "Invalid\n";
} else if (minutes < 25) {
std::cout << "Short\n";
} else if (minutes <= 60) {
std::cout << "Focus\n";
} else {
std::cout << "Long\n";
}
return 0;
}45: FocusThe chain works because each branch relies on the ones before it: by the time minutes <= 60 is checked, the program already knows the value is at least 25. The five test values sit on each side of every boundary, so a < written where <= was meant would change at least one label. Next you will combine two comparisons into a single range check with && and ||.
Completion is saved in this browser only. It does not measure mastery.