The planner will accept durations from 1 through 180 minutes. That rule has two edges, and one comparison can only check one of them. You need a way to say "at least 1 and at most 180." C++ provides three logical operators for combining conditions: && requires both sides to be true, || requires at least one, and ! reverses a bool.
Range checks are where many real bugs live. A rule like "1 through 180" can go wrong at 0, at 1, at 180 and at 181, and each mistake looks reasonable on the page. This lesson shows the right way to write a range, two plausible wrong ways, and how to test the edges so the wrong ways cannot hide.
Predict and run
#include <iostream>
int main() {
int minutes = 181;
bool valid = minutes >= 1 && minutes <= 180;
std::cout << valid << "\n";
return 0;
}Check your prediction
0181 is at least 1, but it is not at most 180, so && produces false, which prints as 0. Change the value to 0, 1, 180 and 181 and predict each result. The two middle values print 1; the outer two print 0.
Two wrong ways to write a range
The first wrong way looks like mathematics:
bool valid = 1 <= minutes <= 180; // not a range checkC++ evaluates this left to right. 1 <= minutes produces a bool, which becomes 0 or 1, and then C++ checks whether that 0 or 1 is at most 180. It always is, so every value is "valid." With warnings enabled, Clang warns that the comparison is always true, and a local build with -Werror stops. The browser editor builds without those flags, so here every value prints as valid, and some compilers accept it silently.
The second wrong way uses the wrong operator: minutes >= 1 || minutes <= 180. Every integer is either at least 1 or at most 180, so this is also always true. Use && when a value must satisfy both bounds. Use || when either failure is enough to reject it:
#include <iostream>
int main() {
int minutes = 0;
bool accept = minutes >= 1 && minutes <= 180;
bool reject = minutes < 1 || minutes > 180;
bool flipped = !(minutes >= 1 && minutes <= 180);
std::cout << accept << " " << reject << " " << flipped << "\n";
return 0;
}0 1 1reject and flipped always agree. To negate "inside the range," you flip each comparison and swap && for ||. Writing it either way is fine; pick the one that reads most clearly at that spot.
Short-circuit evaluation
&& and || evaluate their left side first and skip the right side when the answer is already known. With &&, a false left side means the whole result is false. With ||, a true left side means the whole result is true. This is not just a speed trick: it lets the left side guard the right side.
#include <iostream>
int main() {
int total = 0;
int sessions = 0;
if (sessions != 0 && total / sessions >= 25) {
std::cout << "Long average\n";
} else {
std::cout << "No long average\n";
}
return 0;
}No long averageBecause sessions != 0 is false, the division never runs. Swap the two sides and the program divides by zero, which is undefined behavior. Order the parts of a condition so the safety check comes first. You will use the same pattern later to check that a string or vector is not empty before reading its first element.
Test the edges
A range check needs at least four test values: the two bounds and one step outside each. For 1 through 180 those are 0, 1, 180 and 181. A typical value like 25 catches almost nothing: > instead of >=, < instead of <=, and || instead of && all still accept 25. The edges are where those mistakes change the answer.
Practice
Your turn
A task line has int minutes and an int flag. It is accepted only if minutes are 1 through 180 and the flag is exactly 0 or 1. Print one of:
AcceptedRejected: minuteswhen the minutes are out of range (check this first)Rejected: flagwhen the minutes are fine but the flag is not 0 or 1
With minutes = 25 and flag = 2, the output is Rejected: flag. Then try 200 with flag 1, 180 with flag 1, and 0 with flag 7, predicting each result.
#include <iostream>
int main() {
int minutes = 25;
int flag = 2;
// Write your checks here.
return 0;
}Hint 1: the idea
Each rule is one named bool. Minutes use && across two bounds; the flag uses || across two allowed values.
Hint 2: a first step
Write bool minutes_ok = minutes >= 1 && minutes <= 180; and bool flag_ok = flag == 0 || flag == 1;, then build an if/else-if chain from them.
Show one solution
#include <iostream>
int main() {
int minutes = 25;
int flag = 2;
bool minutes_ok = minutes >= 1 && minutes <= 180;
bool flag_ok = flag == 0 || flag == 1;
if (!minutes_ok) {
std::cout << "Rejected: minutes\n";
} else if (!flag_ok) {
std::cout << "Rejected: flag\n";
} else {
std::cout << "Accepted\n";
}
return 0;
}Rejected: flagNaming each rule as a bool makes the chain read like the brief, and it keeps the two operators where they belong: && for "between two bounds," || for "one of two allowed values." The order of the chain gives the minutes check priority, so 0 with flag 7 reports minutes. Next you will repeat work with loops, and a loop's condition is exactly this kind of bool expression.
Completion is saved in this browser only. It does not measure mastery.