The logical operators in C++ include &&, ||, and !, which represent logical AND, logical OR, and logical negation respectively. They are explained below.
1. Logical AND &&:
Logical AND is a binary operator. When used, there is an expression on both sides of &&. If the expressions on the left and right sides of the operator are both true, the entire expression is true, otherwise it is false, such as the expression: 5&&6, 1&&-1 and 1<2&&5 are both true, and the two expressions 3-3&&5 and 10--&&0 are false.
2. Logical OR ||:
The logical OR operator is also a binary operator. When used, it requires an expression on both sides. If only one of the expressions on the left and right sides of the operator is true, then the entire logical OR expression is true, otherwise both are false. For example, the expression: 3-3||5 is true, 0||5-5 is false.
3. Logical NOT!:
Logical negation is a unary operator, which means negation. It is placed on the left side of the expression, such as !a, that is, the expression that was originally true becomes false after negation, and the expression that was originally false becomes false after negation. becomes true. For example, the expression !0 results in 1, and !(2>1) results in 0.
Above, I hope you will strengthen your understanding. In actual learning work, it is often compound expressions that combine operators and variables such as relational operators, arithmetic operators, logical operators, etc., so you must be proficient in the rules.
As shown in the following code, what do you think the values of these two expressions are?
#include<iostream>usingnamespacestd;intmain(){inta=10;intb=20;intc=30;intd,e;d=!c>(ba)&&(cb)>(ba);e=(ba)| |(cb)&&!(cba);return0;}