Basic usage collation of logical operators in C + + programming

Source: Internet
Author: User
Tags logical operators

If either operand or two operand is true, the logical OR operator (| |) returns a Boolean value of true, otherwise false is returned. The operand is implicitly converted to type bool before the calculation, and the result is of type bool. Logical "or" has left-to-right associativity.
The operands of the logical OR operator do not need to be of the same type, but they must be integral or pointer types. 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.
Calculates the second operand only if the first operand evaluates to False (0). This eliminates unnecessary computations on the second operand when the logical or expression is true.

Copy the 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 the value 1. Otherwise, it evaluates to false and prints the value 0. The calculation stops as long as one of the conditions evaluates to true.
|| The operator keyword
The OR operator is | | The equivalent text. There are two ways to access the OR operator in your program: include the header file iso646.h or compile with 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 = 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 operator:&&
Grammar

Expression
&&
Expression

Note
If the operand is true, the logical AND operator (&&) returns a Boolean value of true, otherwise false is returned. The operand is implicitly converted to type bool before the calculation, and the result is of type bool. Logical "and" have left-to-right associativity.
Operands of the logical AND operator do not need to have the same type, but they must be integers or pointer types. 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 evaluated. This calculation eliminates unnecessary calculations on the second operand when the logical and expression is false. You can use this short-circuit calculation to prevent a null pointer from dereferencing, 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 by using 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 with 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 = 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;

Basic usage collation of logical operators in C + + programming

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.