Conditions and logical decisions
Build exclusive categories, combine comparisons, and protect operations with short-circuit logic.
Ask a true-or-false question
Use == for equality, != for inequality, and <, <=, >, >= for order. A single = assigns a value; it does not ask whether two values are equal. An if executes its block only when its condition is true, while if/else chooses exactly one of two branches.
Independent if statements can both run. An if / else if / else chain stops after the first matching branch. Use a chain for one category, and independent conditions when several facts can be true at once.
Check the strongest threshold first
For a guaranteed score from 0 through 100, check A before B before C. If you first check score >= 50, then 95 would match that broad condition before reaching the A case.
Test the exact boundaries: 49/50, 74/75, and 89/90. Braces determine the block; indentation helps you read it. An accidental semicolon immediately after if creates an empty statement and changes the intended control flow.
#include <iostream>
int main() {
int score = 0;
std::cin >> score;
if (score >= 90) {
std::cout << "A\n";
} else if (score >= 75) {
std::cout << "B\n";
} else if (score >= 50) {
std::cout << "C\n";
} else {
std::cout << "F\n";
}
return 0;
}Both, either, and not
A && B requires both facts. A || B accepts at least one, including both. ! reverses a truth value. To express 10 ≤ x ≤ 20, write x >= 10 && x <= 20. The mathematical chained spelling 10 <= x <= 20 means something different in C++.
Outside that interval is x < 10 || x > 20. Repeat a complete comparison for each choice: choice == 'A' || choice == 'B'. A lone 'B' is not another comparison. The single-character & and | operators are not substitutes for logical && and ||.
Put the safety condition first
For built-in &&, a false left operand prevents evaluating the right one. For built-in ||, a true left operand prevents evaluating the right one. Therefore k != 0 && n % k == 0 checks the divisor before attempting remainder.
For evenness, use n % 2 == 0. For oddness with positive or negative inputs, use n % 2 != 0. Zero is even. These tests work independently of the positive/negative/zero classification.
Decision explorer
Follow if (n > 0), else if (n == 0), and else.
n > 0positive✓ Branch takenn == 0zeroSkippedelsenegativeSkippedOutput: positive
Guided simulation · Change the inputs to test your understanding.
Which expression correctly accepts x from 0 through 100 inclusive?
Turn the idea into code
Read positive side lengths a, b, c ≤ 10⁹. Print INVALID, EQUILATERAL, ISOSCELES, or SCALENE for a nondegenerate triangle.
Give me a hint
First require all three pairwise sums to exceed the remaining side. Then test all-equal before any-equal.
Show the explained solution
Validity must come before classification. Equality in a triangle inequality is a flattened shape. Among valid triangles, all equal comes first because it also satisfies “some pair is equal.”
#include <iostream>
int main() {
long long a = 0, b = 0, c = 0;
std::cin >> a >> b >> c;
if (!(a + b > c && a + c > b && b + c > a)) {
std::cout << "INVALID\n";
} else if (a == b && b == c) {
std::cout << "EQUILATERAL\n";
} else if (a == b || a == c || b == c) {
std::cout << "ISOSCELES\n";
} else {
std::cout << "SCALENE\n";
}
return 0;
}Your notes
Mark complete when you can explain it yourself.