cpp / beginner
Snippet
Relational and Logical Operators
Relational operators compare values and return boolean results. == checks equality, != checks inequality, > greater than, < less than. Logical operators combine boolean expressions: && (AND) requires both conditions true, || (OR) requires at least one true, ! (NOT) inverts the boolean value. These are essential for building conditional logic.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>int main() {int x = 5, y = 10;std::cout << "x > y: " << (x > y) << std::endl;std::cout << "x < y: " << (x < y) << std::endl;std::cout << "x == y: " << (x == y) << std::endl;std::cout << "x != y: " << (x != y) << std::endl;bool result = (x < y) && (y > 0);std::cout << "(x < y) && (y > 0): " << result << std::endl;return 0;}
Breakdown
1
x > y
Greater than, returns false (5 is not > 10)
2
x < y
Less than, returns true (5 is < 10)
3
x == y
Equality check, returns false (5 != 10)
4
(x < y) && (y > 0)
AND operator, true only if both conditions are true