C++ Short-circuit Logic
Use short-circuit logic to protect division and reserve the conditional operator for a simple value choice.
How it works
Logical AND evaluates its right operand only if the left is true. Logical OR evaluates its right operand only if the left is false. That sequencing permits a precondition to guard a later operation. The conditional operator evaluates only the selected alternative and is useful for a short label or value. Avoid a long chain with hidden side effects: an ordinary branch is easier to inspect when several actions or failure paths are involved.
Run it locally
Use a C++20 compiler and matching standard library in a separate lab folder. The command uses c++; substitute g++, clang++ or zig c++ for your toolchain. C++ does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.
c++ -std=c++20 -Wall -Wextra -Wpedantic -Werror short-circuit.cpp -o lessonOn Windows use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Thread examples may need -pthread on your platform.
Download the complete C++ source. Run file labs in a private empty directory, away from existing reports.
Example
#include <iostream>
#include <string>
int main() {
int parcels{18};
int rounds{3};
if (rounds > 0 && parcels / rounds >= 5) { std::cout << "Busy rounds\n"; }
std::string label = rounds > 0 ? "scheduled" : "unassigned";
std::cout << "Status: " << label << '\n';
}
Expected result
Busy rounds
Status: scheduled
The division is reached only for a positive round count. The selected fixed values also avoid signed division overflow. The ternary expression chooses one string literal, then the owning string stores its text. The rule should still validate the parcel count separately when it comes from input; a local division guard is not a complete domain validator.
Consult the WG21 C++ working group and the C++ Core Guidelines for language references and design guidance. Check the selected standard version and your implementation’s library support.