Relational and logical operators

Relational operators are used to compare values with each other. They return a logical result: true or false. Values that are linked with relational operators are called elementary statements in propositional logic.
A typical example of a Boolean logical operator is the and operation: It always returns true if all operands are also true.

OperatorDescription
<smaller
<=less than or equal to
>greater
>=greater than or equal to
==equal
!=unequal
!expression is false
&&both expressions are true
||at least one expression is true

Example

Code Explanation

LineDescription
5Initializes the integer variables
a with value 0,
i with value 20 and
b with value 8
6As long as the value of variable a is not equal to the value of
b and i is greater than or equal to the sum of a and b
7The values of variable a and b are output with the indication that the two variables are not equal
8Increases the value of the variable a by 1 (other notation: a = a + 1)
10If the condition of the while loop is no longer true, both values are equal and are output
#include &lt;iostream&gt;
using namespace std;

int main() {
	int a = 0, i = 20, b = 8;
	while (a != b && i >= a+b) {
		cout << a << " is equal " << b << endl;
		a++;
	}
	cout << a << " is unequal " << b << endl;
}
Output
0 is equal 8
1 is equal 8
2 is equal 8
3 is equal 8
4 is equal 8
5 is equal 8
6 is equal 8
7 is equal 8
8 is unequal 8