C++ Programming/Programming Languages/C++/Code/Statements/Variables/Operators/Logical Operators

Relational operators
The operators < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (equal to), and != (not equal to) are relational operators that are used to compare two values. Variables may be compared to another variable or to a literal.

The < operator checks if the first operand is less than the second operand. If the first operand is less than the second operand, returns true. Else returns false.

The > operator checks if the first operand is greater than the second operand. If the first operand is greater than the second operand, returns true. Else returns false.
 * Examples

The <= operator checks if the first operand is less than or equal t o the second operand. If the first operand is less than or equal to the second operand, returns true. Else returns false.
 * Examples

The >= operator checks if the firs t operand is g reater than or equal to the second operand. If the first operand is greater th an or equal to the second operand, returns true. Else returns false.
 * Examples

The == operator checks if th e first operan d is equal to the second operand. If the first operand is equal to the second operan d, returns true. Else returns false.
 * Examples

The != operator checks if the first operand is not equal to the second ope rand. If the first operand is not equa l to the secon d operand, returns true. Else returns false.
 * Examples


 * Examples

Logical operators
The operators and (can also be written as &&) and or (can also be written as ||) allow two or more conditions to be chained together. The and operator checks whether all conditions are true and the or operator checks whether at least one of the conditions is true. Both operators can also be mixed together in which case the order in which they appear from left to right, determines how the checks are performed. Older versions of the C++ standard used the keywords && and || in place of and and or. Both operators are said to short circuit. If a previous and condition is false, later conditions are not checked. If a previous or condition is true later conditions are not checked.

The <tt>not</tt> (can also be written as !) operator is used to return the inverse of one or more conditions.


 * Syntax:

condition1 and condition2 condition1 or condition2 not condition
 * Examples:

When something should not be true. It is often combined with other conditions. If x>5 but not x = 10, it would be written:

When all conditions must be true. If x must be between 10 and 20:

When at least one of the conditions must be true. If x must be equal to 5 or equal to 10 or less than 2:

When at least one of a group of conditions must be true. If x must be between 10 and 20 or between 30 and 40.

Things get a bit more tricky with more conditions. The trick is to make sure the parenthesis are in the right places to establish the order of thinking intended. However, when things get this complex, it can often be easier to split up the logic into nested if statements, or put them into bool variables, but it is still useful to be able to do things in complex boolean logic.

Parenthesis around <tt>x > 10</tt> and around <tt>x < 20</tt> are implied, as the <tt><</tt> operator has a higher precedence than <tt>and</tt>. First <tt>x</tt> is compared to 10. If <tt>x</tt> is greater than 10, <tt>x</tt> is compared to 20, and if <tt>x</tt> is also less than 20, the code is executed.

and (&&)
The logical AND operator, and, compares the left value and the right value. If both statement1 and statement2 are true, then the expression returns TRUE. Otherwise, it returns FALSE.

In this snippet, the if statement checks to see if var1 is greater than var2. Then, it checks if var2 is greater than var3. If it is, it proceeds by telling us that var1 is bigger than both var2 and var3.

or (||)
The logical OR operator is represented with or. Like the logical AND operator, it compares statement1 and statement2. If either statement1 or statement2 are true, then the expression is true. The expression is also true if both of the statements are true.

Let's take a look at the previous expression with an OR operator. If var1 is bigger than either var2 or var3 or both of them, the statements in the if expression are executed. Otherwise, the program proceeds with the rest of the code.

not (!)
The logical NOT operator, not, returns TRUE if the statement being compared is not true. Be careful when you're using the NOT operator, as well as any logical operator.

The logical expressions have a higher precedence than normal operators. Therefore, it compares whether "not x" is greater than 10. However, this statement always returns false, no matter what "x" is. That's because the logical expressions only return boolean values(1 and 0).