Hello, I’m Liang Tang.

This is part 24 of the EasyC++ series on logical expressions.

If you want a better reading experience, you can visit the Github repository.

Logical expression

We use logical judgments whether in for loops or while loops or if conditions.

All of the examples we’ve done are very simple, single judgments. Sometimes our logic is very complex and we need to use logical expressions when we have multiple criteria.

Logical expressions are concatenated by logical operators such as OR, AND, AND NOT, which translates to AND OR NOT.

The OR operator.

Or operator is the translation of the meaning of “or”, say, two or more persons to have a to true, the result is true, writing | |, note there are two vertical lines, a single vertical lines and or operation, but is an operation or operation, of the mean is different, don’t make a mistake.

Using the or operator, we can concatenate multiple judgment conditions, and as long as one of them is true, the final result is true.

5= =5 || 4 < 4;
5 > 3 || 2 < 1 || 4 < 7;
Copy the code

The | | operator priority lower than the comparison operators, so it can be without the parentheses. Other provisions of the c + + | | operators is a sequence point, means that the compiler will calculating the value of the left, then calculate the right values. Stop as soon as you see an expression that results in true, and do not continue to the right.

AND operator

The and operator translates to “and”, indicating that both conditions are true, i.e., the result is true when both are true, written as &&.

5= =5 && 4= =4;
4= =3 && 1 < 2;
Copy the code

Similarly, the && operator has a lower priority than the comparison operator. Also, the && operator is a point of order. Means C++ executes the left result first, then the right. If the left result is false, the right result will not be executed. This feature is useful, especially if we use elements that might be illegal, such as:

string s;
if (n < s.size() && s[n] == 'h') {
    // do something
}
Copy the code

In the example above, we need to determine the s[n] element, but n can be large enough to exceed the range of the string. If n < s.size() is not true, then s[n]==h will not be executed and no error will be raised.

There is also a case in C++ that should never be abbreviated when using the ampersand operator to determine ranges:

if (a >= 5 && a < 10);  / / legal
if (5 <= a < 10);		/ / illegal
Copy the code

(5 <= a) < 10 (5 <= a) < 10 (5 <= a) Obviously 5 <= a is a bool, it must be less than 10, so this expression is always true no matter what a is equal to.

The NOT operator.

The not operator is also a non-operator that negates the result of a logical expression. True to false, false to true, write! . Such as:

if(! (x >5));
Copy the code

Both the AND and OR operators have lower precedence than the comparison operators, but not the NOT operator, which has precedence over all the relational and arithmetic operators. So if the object that we’re going to invert is an expression, make sure you put parentheses.

Such as:

! (x >5); ! x >5;
Copy the code

The latter computes the inverse of x first, yielding either true or false, which is clearly less than 5.

In addition, the not operator takes precedence over and over OR. So the expression:

age > 30 && age < 45 && !flag || weight > 300
Copy the code

Would be interpreted as:

(age > 30 && age < 45&& (! flag)) || weight >300
Copy the code