! Logical non-!expr

< less than expr < expr

<= is less than or equal to expr <= expr

> Greater than expr > expr

>= is greater than or equal to expr >= expr

= = Equality Expr = = Expr

!= Unequal expr!= expr

&& logic and expr && expr

|| Logic or Expr | | Expr

Relational operators and logical operators use operands of arithmetic or pointer types, and return values of type bool.

1. Logic and logic or operators

Logical operators treat their operands as conditional expressions: The operand is evaluated first, and if the result is 0, the condition is False (false), or True (true). The result is true only if the logical and (&&) operators have two operands that are true. For logic or (| |) operator, its value is true as long as one of the two operators is true. Given the following form:

Expr1 && expr2//logical and

Expr1 | | EXPR2//logical OR

EXPR2 is solved only if the value of an expression cannot be determined by EXPR1. That is, you must ensure that the EXPR2 is computable if and only if the following conditions occur.

A. In logic and expression, EXPR1 evaluates to True. If the value of Expr1 is false, the value of logic and expression is false regardless of the value of EXPR2. When the Expr1 value is true, only the value of EXPR2 is true, The value of logic and expression is true.

B. In a logical or an expression, the EXPR1 evaluates to False. If the value of Expr1 is false, the value of the logical or expression depends on whether the value of EXPR2 is true.

------------------------------------------------------------I was waiting for the split line---------------------------------------------------

Note: logical and logical OR operator always evaluates its left operand first. The right-hand operand is then calculated. The right operand is solved only if the value of the left-hand operand cannot determine the result of the logical expression. We often call this job search strategy "short-circuit evaluation" (short-circuit Evaluation) ".

------------------------------------------------------------I am also a waiting line-----------------------------------------------

A valuable use for logic and operators is that if a boundary condition makes the EXPR2 calculation dangerous, the EXPR1 evaluates to False before the condition appears. For example, the writer uses a string object to store a sentence, Then capitalize all the characters in the first word of the sentence, which can be implemented as follows:

String S ("Expressions in C + + are conposed ...");

String::iterator It=s.begin ();

Convert the word in S to uppercase

while (It!=s.end () &&!isspace (*it))

{

*it=toupper (*it); ToUpper covered in section 3.2.4 (chapter 3.2.4)

++it;

}

In this example, the while loop determines the two conditions. First check if it has reached the end of the string type object, and if not, it points to a character in S. Only when the test condition is established does the system compute the right-hand operand of the logic and the operator. That is, after you guarantee that it does point to a real character, check that the character is a space. If you encounter a space, or if there is no space in s that has reached the end of S, the loop ends.

2. Logical non-operator

Logical non-operator (!) Treats its action rate as a conditional expression, producing a conditional value that is the opposite of its operand value. If the operand is not 0, do it! The result of the operation is false. For example, you can use a logical non-operator on the empty member function of a vector type object to determine whether the object is empty based on the function return value:

Assign value of the-a-in-VEC to X if there is one

int x=0;

if (!vec.empty ())

X=*vec.begin ();

If the call to the empty function returns FALSE, the value of the subexpression!vec.empty () is true.

3. The use of relational operators should not be threaded

Relational operators (<, <=, >, >=) have a left-bound feature. In fact, because the relational operator returns the result of the bool type, it rarely uses its left binding attribute. If you use multiple relational operators in tandem, the results are often unexpected:

Oops!this condition does not determine if the 3 values are

if (i<j<k)

{

/*...*/

}

The value of this expression is true as long as K is greater than 1. This is because the second less than the left-hand operand of the operator is the result of the first less than the operator: TRUE or false. That is, the condition compares K with an integer 0 or 1. To achieve the conditions we want to test, the above expression should be rewritten as follows:

if (i<j && j<k) {/*...*/}

4. Equivalence test with BOOL literals.

The bool type can be converted to any arithmetic type-------bool value false in 0, and true in 1.

----------------------------------------------------I'm a segmented line of tiny sleepy------------------------------------------------

Caution: Because true is converted to 1, it is often difficult to correctly write the equivalent criteria for determining whether a value is equal to the bool value true:

if (val==true) {/*....*/}

---------------------------------------------------I'm a segmented line of tiny sleepy-------------------------------------------------

The Val itself is of type bool, or Val has a data type that can be converted to a bool type. If Val is of type bool, the judgment condition is equivalent to:

if (val) {/*.....*/}

Such code is shorter and more straightforward (although for beginners, such abbreviations can be confusing).

More importantly, if Val is not bool, the comparison of Val and true is equivalent to:

if (val==1) {/*...*/}

This is completely different from the conditions below.

Condition succeeds if Val are any nonzero value

if (val) {/*...*/}

At this point, the criteria are true if Val is any value not 0. If you explicitly write a condition comparison, the condition is only valid if Val equals the specified 1 value

Exercise 5.5 explains when the operands of the logic and the operator, the logical or the operator, and the equality operator are computed.

Logic and logic or operators use the evaluation strategy called "short-circuit evaluation", that is, the left-hand operand is computed, the right operand is computed, and the right-hand operand is computed only if the value of the left-hand operand cannot determine the result of the logical operation.

Both the left and right operands of the equality operator need to be computed

Exercise 5.6 Explains the behavior of the following while loop conditions:

Char *cp= "Hello World";

while (CP && *CP)

The condition of the while loop is the execution of the loop body when the pointer cp is a non-empty pointer and the character that the CP points to is not a null character null ('. 0 '). That is, the loop can handle characters in the string "Hello world" one at a time.

Exercise 5.7 Writes a while loop condition that reads integer (int) data from a standard input device and ends when the read value is 42 o'clock.

int Val;

while (Cin>>val && val!=42)

Or

int Val;

cin>>val;

while (val!-42)

Exercise 5.8 Write an expression to determine whether four values A, B, C, and D satisfy a greater than b,b greater than C, and C greater than D

A>b && b>c && c>d