Logical Expressions for C #

Source: Internet
Author: User
Tags bool expression logical operators

Using logical operators to connect relational expressions or Boolean expressions is a logical expression. The value of the logical expression is still a Boolean value.

In the evaluation of a logical expression, not all logical operators are executed. Sometimes, you can determine the result of a logical expression without having to execute all the operators. You continue to execute a logical expression only if the next logical operator must be executed before it can be evaluated. In this case we call the "short-circuit" of the logical expression.

Suppose A is a Boolean value or a logical expression, and bool-exp is a logical expression, then:

a&& (BOOL-EXP) continues to judge values only if a is true. If A is false, the value of the logical expression is already set to false and no further evaluation is required.

a| | (BOOL-EXP) only if A is false will the value continue to be judged. If A is true, the value of the logical expression is already determined to be true and no further evaluation is required.

After mastering logical operators and relational operators skillfully, you can use logical expressions to represent various complex conditions. For example, give a year to determine whether it is a leap month. We know that the terms of a leap year are: multiples of 400, or multiples of 4 but not multiples of 100. Year is set, and leap years or not can be represented by a logical expression:

(year%400) ==0| | ((year%4) ==0&& (year%100)!=0)

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.