A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
Six operators: < > <= >= == !=. They compare two values and produce 1 (true) or 0 (false). Simple. The complexity hides in three places: chaining comparisons does not work the way mathematics suggests, floating-point equality is unreliable, and the signed-unsigned mismatch we covered in the type casting lesson applies here too. Get these wrong and your conditions silently evaluate to the wrong truth value.
#include <stdio.h>int main(void) {int x = 15;if (10 < x < 20) {printf("x is between 10 and 20\n");} else {printf("x is NOT between 10 and 20\n");}int result = (10 < x);printf("10 < x evaluates to: %d\n", result);printf("Then %d < 20 evaluates to: %d\n", result, result < 20);return 0;}
It printed the right answer for the wrong reason. 10 < x evaluates to 1. Then 1 < 20 evaluates to 1. The condition is always true regardless of x. Set x to 100 and it still prints "between 10 and 20" because 1 < 20 is always true. The correct form is 10 < x && x < 20. Two comparisons joined by a logical AND.
#include <stdio.h>#include <math.h>int main(void) {double a = 0.1 + 0.2;double b = 0.3;printf("a = %.20f\n", a);printf("b = %.20f\n", b);printf("a == b: %d\n", a == b);printf("fabs(a - b) < 1e-9: %d\n", fabs(a - b) < 1e-9);return 0;}
0.1 + 0.2 does not equal 0.3 in IEEE 754 double precision. The representations differ by roughly 5.5 × 10⁻¹⁷. The == comparison returns false. The epsilon comparison returns true. Never use == or != on floating-point values that have undergone arithmetic. Use an epsilon tolerance appropriate to your precision requirements.
Relational operators are the atoms of every condition. The chaining trap and the floating-point equality issue are the two bugs that appear most often in code reviews. Memorize both. Next: arithmetic operations, where integer division, overflow, and operator precedence create their own surprises.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.