C++ programming or || and not! Logical operator basic usage arrangement

  • 2020-05-07 20:11:56
  • OfStack

logical or operator: ||
 
grammar


logical-or-expression 
||
 logical-and-expression

note
If any of the 1 operands or two operands are true, the logical or operator (||) returns the Boolean value true. Otherwise return false. The operand is implicitly converted to type bool before the calculation, and the resulting type is bool. Logical "or" has relevance from left to right.
The operands of the logical or operator do not need to be of the same type 1, but they must be of the integer or pointer type. The operands are usually relational or equal expressions.
The first operand is fully evaluated, and all side effects are completed before continuing to evaluate the logical or expression.
The second operand is calculated only if the result of the first operand is false (0). This eliminates unnecessary evaluation of the second operand when the logical "or" expression is true.


printf( "%d" , (x == w || x == y || x == z) );

In the above example, if x is equal to w, y, or z, the second parameter of the printf function is calculated as true and the value 1 is printed. Otherwise, it evaluates to false and prints the value 0. As long as the result of one of the conditions is true, the calculation stops.
Operator keyword for ||
The or operator is the equivalent of ||. There are two ways to access the or operator in your program: include the header iso646.h or compile with the /Za (language extension disabled) compiler option.


// expre_Logical_OR_Operator.cpp
// compile with: /EHsc
// Demonstrate logical OR
#include <iostream>
using namespace std;
int main() {
  int a = 5, b = 10, c = 15;
  cout << boolalpha
     << "The true expression "
     << "a < b || b > c yields "
     << (a < b || b > c) << endl
     << "The false expression "
     << "a > b || b > c yields "
     << (a > b || b > c) << endl;
}

logic and operators: &&  
grammar


expression 
&&
 expression

note
If the operand is true, the logical and operator (&&) returns the Boolean value true, otherwise false. The operand is implicitly converted to type bool before calculation, and the resulting type is bool. Logical "and" has relevance from left to right.
The operands of the logical and operators do not need to have the same type, but they must be integers or pointer types. The operands are usually relational or equal expressions.
The first operand is fully evaluated, and all side effects are completed before continuing to evaluate the logical and expression.
If the result of the first operand is true (non-zero), the second operand is calculated. When the logical "and" expression is false, this calculation eliminates the unnecessary calculation of the second operand. You can use this short-circuit calculation to prevent the null pointer from dereferencing, as shown in the following example:


char *pch = 0;
...
(pch) && (*pch = 'a');

If pch is null (0), the right-hand side of the expression is never evaluated. Therefore, assignment cannot be made with the null pointer.
The operator keyword for &&
The and operator is the text equivalent of &&. There are two ways to access the and operator in your program: include the header file iso646.h, or compile with the /Za (language extension disabled) compiler option.


// expre_Logical_AND_Operator.cpp
// compile with: /EHsc
// Demonstrate logical AND
#include <iostream>

using namespace std;

int main() {
 int a = 5, b = 10, c = 15;
 cout << boolalpha
   << "The true expression "
   << "a < b && b < c yields "
   << (a < b && b < c) << endl
   << "The false expression "
   << "a > b && b < c yields "
   << (a > b && b < c) << endl;
}

logical non-operator:!
 
grammar


! cast-expression

note
Logical inverse operator (!) Inverts the meaning of its operands. The operand must be an algorithm or pointer type (or an expression that evaluates to an algorithm or pointer type). The operand implicitly converts to the type bool. If the converted operand is false, the result is true. If the converted operand is true, the result is false. The result is of type bool.
For the expression e, the 1 - bit operator expression! e is equivalent to this expression (e == 0), except where overloaded operators are involved.
! Operator keyword
The not operator is the! Equivalent text. In your program, the not operator can be accessed in two ways: by including the header file iso646.h, or by compiling with the /Za (disabled language extension) compiler option.


// expre_Logical_NOT_Operator.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

int main() {
 int i = 0;
 if (!i)
  cout << "i is zero" << endl;
}


Related articles: