Vc#2005 Quick Start using Boolean operators

Source: Internet
Author: User
Tags bool expression logical operators variable
The QuickStart Boolean operator (Boolean operator) is an operator that evaluates to either true or false. C # provides several very useful Boolean operators, the simplest of which is the not (negation) operator, which uses an exclamation point (!). to indicate that.! the operator asks for a Boolean value of the inverse value. In the example above, if the variable Areyouready value is true, then the expression!areyouready evaluates to False.

understanding equality and relational operators

The two more commonly used Boolean operators are equal (= =) and unequal (!=) operators. Using these two two-element operators, you can determine whether a value is equal to another value of the same type. Table 1 shows how these operators work, taking an int variable named age as an example.

Table 1 Boolean operators

operator Meaning Example Result (assuming age = 42)
== Equals Age = = 100 False
!= Not equal to Age!= 0 True

Closely related to the above two operators is the relational operator (relational operator). Using these operators, you can determine whether a value is less than or greater than another value of the same type. Table 2 shows you how to use these operators.

Table 2 relational operators

operator Meaning Example Result (assuming age = 42)
&< Less than Age &< 21 False
&<= Less than or equal to Age &<= 18 False
> Greater than Age > 16 True
>= Greater than or equal to Age >= 30 True

Be careful not to confuse the equality operator (= =) with the assignment operator (=). Like x==ySuch code compares x and Y, and returns True if two values are the same. Instead, like x=ySuch code assigns the value of Y to X.

Understanding conditional logical operators

C # also provides two additional Boolean operators: the Logical AND (logical AND) operators, which use the &&and the logical or (logical OR) operator, which uses | | to indicate. These two operations Fu Shi called conditional logical operators (conditional logical operator). Their role is to combine different Boolean expressions to form a larger expression. The similarity between the two two-dollar operators and the equality and relational operators is that their results are true or false. The difference is that the value (operand) that they manipulate must itself be true or false.

The,&& operator evaluates to true only if the two Boolean expressions that are operands are true. For example, the following statement assigns a true value to validpercentage only if the value of the percent is greater than or equal to zero, and the percent value is less than or equal to 100:

BOOL Validpercentage;

Validpercentage = (Percent >= 0) && (Percent &<= 100);

A common mistake for beginners is to name the percent variable only once when merging two Tests, as follows:

Percent >= 0 && &<= 100//This statement cannot be compiled

Using parentheses helps to avoid this type of error and also helps clarify expressions. For example, you can compare the following two expressions:

Validpercentage = percent >= 0 && percent &<= 100

Validpercentage = (Percent >= 0) && (Percent &<= 100)

Two expressions return the same value because the operator && precedence is lower than >= and &<=. However, the second expression shows its purpose in a clearer and more understandable way.

Two operator any one is true, operator | | The evaluated result is true. Using the operator | |, we can determine whether any one of the two conditions is set up. If the value of percent is less than 0, or if the value of percent is greater than 100, the following statement assigns the value true to Invalidpercentage:

BOOL Invalidpercentage;
Invalidpercentage = (Percent &< 0) | | (Percent > 100);


Short-circuit evaluation

Operators && and | | Has a feature called short-circuit evaluation (short circuiting). Sometimes it is not necessary to evaluate two operands at all. For example, assuming that the left operand of the operator && evaluates to false, the result of the entire expression must be false, regardless of the value of the right-hand operand. Similarly, if the operator | | Evaluates to true for the left operand, the result of the entire expression is definitely true. In these cases, operator && and | | The evaluation of the Boolean expression to the right is bypassed. Here are some examples:




(Percent >= 0) && (Percent &<= 100)




In this expression, if the value of the percent is less than 0, then the boolean expression on the operator && left evaluates to False. This value means that the result of the entire expression must be false, regardless of the expression on the right. Therefore, the expression on the right side is not evaluated.




(Percent &< 0) | | (Percent > 100)




In this expression, if the value of percent is less than 0, the operator | | The Boolean expression on the left evaluates to True. This value means that the result of the entire expression must be true. Therefore, the expression on the right side is not evaluated.

If you can design an expression carefully to use conditional logic operators, you can avoid unnecessary work to improve the performance of your code. Put the easy, simple Boolean expression on the left side of the conditional logic operator and put the more complex expression on the right. In many cases, the program does not require a complex expression.

   operator Precedence and associativity summary

Table 3 summarizes the precedence and associativity of all the operators that have been learned so far. Operators of the same category have the same precedence. Operators in a higher category take precedence over operators in lower categories.

Table 3 Operator Summary

Category

operator

Description

Combination of


Main (Primary)

()

++

--

Override Priority

After increment

After descending

Left

One dollar (unary)

!

+

-

++

--

Logical not

Add

Reducing

Forward increments

Before descending

Left

Multiply (multiplicative)

*

/

%

By

Except

Find Yu

Left

Add (additive)

+

-

Add

Reducing

Left

Relationship (relational)

&<

&<=

>

>=

Less than

Less than or equal to

Greater than

Greater than or equal to

Left

Equality (equality)

==

!=

Equals

Not equal to

Left

Conditions and (Conditional and)

&&

Logical AND

Left

Condition or (Conditional or)

||

Logical OR

Left

Assign Value (Assignment)

=




Right


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.