Check The Programming Section
Relational operators are also known as comparison operators and the word relational refers to the relationship that values can have between each other. Expressions that contain relational operators are called relational expressions. The Table displayed the list of relational operators and the type of operations they support.
C++ supports three logical operators and the term logical refers to the ways these relationships can be connected. Here these relationships refer to the relations supported by the relational operator between two operands. Because, relational and logical operator often works together and it is also explained together here. The list of logical operators with their action is listed in the Table.
Both these sets of operators work with the idea of a relationship between two values either is true and false. In C/C++, any value other than zero is considered as true value and zero is considered as false. Consider the relationships listed in the Table, to understand the true and false relationship.
In C++ and since C99, defines a separate data type bool to handle zero/non-zero concept of true and false. The bool data type has two Boolean constants as true and false. In C++, a non-zero value automatically converts to true, and a zero value automatically converts to false. Consider the example metioned below (edit and try online):
bool check;
check = 4;
cout<<std::boolalpha;
cout<<check<<endl;
check = 0;
cout<<check;
In the above code, check is declared as bool data type, which can have only two values either as true or false. Thereafter, a non-zero value 4 is assigned to check and C++ compiler automatically converts 4 to true and assigned to check. To print any bool values we further need a manipulator boolalpha with cout stream object. Else all bool types will be displayed as 1 or 0. After setting a format flag to cout with insertion operator<<, the cout statement prints the value of check as true (as we have set it to 4). Thereafter, a zero value is assigned to check and cout prints the value of check as false.
Lets consider the below statements
int p = 5>6;
int q = 5<6;
The expression 5>6 is a false relation between 5 and 6, so a value 0 is assigned to p. On the other hand, the expression 5<6 is a true relation between 5 and 6, so 1 as non-zero value is assigned to q. Let's print the truth table for the logical operators is displayed in the table using 1's and 0's.
Both the relational and logical operators are lower in precedence than the arithmetic operators. Let's consider the below expression
int k = 10 > 1+2;
as same as
int k = 10> (1+2);
The result of above expression is false, because 10>3 is a false relation. Let's prints the precedence and associativity table by combining arithmetic, relational, and logical operators together.
Consider the below expression which combines several operations together
int k = 10>5 && !(10<9) || 3<=4;
() have higher precedence. So the relation (10<9), turns out be a true relation and 1 as the outcome of it
k = 10>5 && !(10<9) || 3<=4
k = 10>5 && !1 || 3<=4
Now, expression has multiple relational and logical operators. Lets consider the precedence and according to the precedence, the logical NOT has higher precedence among all. The !1 becomes a 0 value, by considering the truth table of logical operator.
k = 10>5 && 0 || 3<=4
Now, both relational operator > and <= has same precedence. So compiler follows left-to-right according t0 associativity. And the relationship between 10 and 5 turns out be a true relation and 1 is returned as true value.
k = 1 && 0 || 3<=4
Now, the relation between 3 and 4 turns out to be a true relation. Because 3 is less or equal to 4.
k = 1 && 0 || 1
Now, expression left with logical AND and OR operator and AND operator has higher precedence. So according to truth table, when p and q both is true then only p&&q returns a true else a false value 0 is returned. So, the expression becomes
k = 0 || 1
Now acoording to the truth table a logical OR operation between p and q is true if any of these is true. So the outcome of 0||1 logical operation turns out be a true value 1 and assigned to k.
k = 1
Note: Remember, all relational and logical expressions produce either a true or false result. Therefore, the following program fragment is not only correct, but will print the number 1.
int x;
x = 100;
printf("%d", x>10);