C + + programming or | |, with &&, non! Logical operators basic Usage Collation _c language

Source: Internet
Author: User
Tags logical operators

Logical OR operator: | |

Grammar

Logical-or-expression 
| |
 Logical-and-expression

Note
If either operand or two operands is true, the logical OR operator (| |) returns a Boolean value of true; The operand is implicitly converted to type bool before the calculation, and the result is of type bool. The logic "or" has left-to-right affinity.
The operands of the logical OR operator do not need to be of the same type, but they must be integral or pointer types. The operands are usually relational or equality expressions.
The first operand is fully evaluated and all side effects are completed before the logical or expression continues to be evaluated.
The second operand is computed only if the first operand evaluates to False (0). This eliminates the unnecessary calculation of the second operand when the logical or expression is true.

Copy Code code as follows:

printf ("%d", (x = = W | | | x = = Y | | | x = = z));


In the example above, if X is equal to W, y, or Z, the second argument of the printf function evaluates to True and prints a value of 1. Otherwise, it evaluates to false and prints a value of 0. The calculation stops whenever one of the conditions evaluates to true.
|| The operator keyword
The OR operator is a | | The equivalent text. There are two ways to access the OR operator in your program: include the header file iso646.h or compile using the/za (Disable language extensions) compiler option.

Expre_logical_or_operator.cpp
//compile with:/EHSC
//demonstrate Logical OR
#include <iostream >
using namespace std;
int main () {
  int a = 5, B = ten, c =;
  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 operator:&&
Grammar

Expression 
&&
 expression

Note
If the operand is true, the logical AND operator (&&) returns a Boolean value of true, or false. The operand is implicitly converted to type bool before the calculation, and the result is of type bool. The logic "and" has a left-to-right correlation.
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 equality expressions.
The first operand is fully evaluated and all side effects are completed before the logical and expression continues to be evaluated.
If the first operand evaluates to True (not 0), the second operand is computed. This calculation eliminates the unnecessary calculation of the second operand when the logical and the expression is false. You can use this short-circuit calculation to prevent null pointer dereference, as shown in the following example:

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

If the PCH is null (0), the right side of the expression is never evaluated. Therefore, you cannot assign a value through a null pointer.
operator keywords 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 using the/za (Disable language extensions) compiler option.

Expre_logical_and_operator.cpp
//compile with:/EHSC
//demonstrate Logical and
#include <iostream >

using namespace std;

int main () {
 int a = 5, B = ten, c =;
 cout << boolalpha
   << "The true expression"
   << "a < b && B < c yields"
   <& Lt (A < b && B < c) << Endl
   << "The false expression"
   << "a > B && b &l T C yields "
   << (a > B && b < c) << Endl;
}

Logical non-operator:!

Grammar

Copy Code code as follows:

! Cast-expression


Note
The logical negation operator (!) reverses 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). Operands are implicitly converted to type bool. If the converted operand is false, the result is true, and if the converted operand is true, the result is false. The result is type bool.
For expression E, the unary operator expression!e is equivalent to the expression (E = = 0), except where overloaded operators are involved.
! The operator keyword
The NOT operator is with! Equivalent text. In your program, you can access the NOT operator in two ways: include header file iso646.h, or compile with/za (Disable language extensions) 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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.