The last section used expressions to calculate numbers. A comparison is an expression with a different kind of result: a bool, either true or false. Comparing a booking count with a capacity lets the program answer whether there is space, without changing either count.
| Operator | Question |
|---|---|
== | Are the values equal? |
!= | Are they different? |
< | Is the left value smaller? |
> | Is the left value larger? |
<= | Is the left value smaller or equal? |
>= | Is the left value larger or equal? |
#include <iostream>
int main() {
int visitors = 7;
int capacity = 10;
bool full = visitors == capacity;
bool space = visitors < capacity;
std::cout << std::boolalpha;
std::cout << full << "\n";
std::cout << space << "\n";
return 0;
}
This prints false and then true. The comparison reads the numbers without changing them. Notice that == has two equals signs. A single = performs assignment; it does not ask an equality question.
The boundary is important. If a rule says “at least ten,” use >=, which includes exactly ten. If it says “more than ten,” use >, which excludes ten. Read a condition aloud to check which rule you meant.
The starter has 18 bookings and a capacity of 20. Replace the two boolean placeholders with comparisons using those names. The output is already configured with std::boolalpha, so you can read the answers as words. Both results describe the same booking data; neither should modify it.
hasSpace with a comparison asking whether bookings is less than capacity. Expect Has space: true.full with an equality comparison between bookings and capacity. Expect Full: false.Edit main.cpp, press Run to inspect the Console, then Submit to check these results.