Vc#2005 Quick Start using Boolean operators

Source: Internet
Author: User
Tags logical operators

A Boolean operator (a 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 = N)
&< less than age &< &<= false
> age > & gt;= is greater than or equal to age >= true

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

Understanding Conditional logical operators

C # also provides two additional Boolean operators: the Logical AND (logical AND) operator, which is represented by &&, and the logical or (logical OR) operator, which uses the | | 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)

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.