intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Using Boolean Operators

Chia sẻ: Nghia Tuan | Ngày: | Loại File: PDF | Số trang:4

108
lượt xem
5
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Sử dụng Boolean sử dụng Một điều hành Boolean là một nhà điều hành có kết quả hoặc là đúng hoặc sai. C # có một số nhà khai thác Boolean rất hữu ích, đơn giản nhất trong số đó là các nhà điều hành không, được đại diện bởi các biểu tượng dấu chấm than (!).

Chủ đề:
Lưu

Nội dung Text: Using Boolean Operators

  1. Using Boolean Operators A Boolean operator is an operator whose result is either true or false. C# has several very useful Boolean operators, the simplest of which is the NOT operator, which is represented by the exclamation point symbol (!). The ! operator negates a Boolean value, yielding the opposite of that value. In the previous example, if the value of the variable areYouReady is true, the value of the expression !areYouReady is false. Understanding Equality and Relational Operators Two much more commonly used Boolean operators are the equality (==) and inequality (!=) operators. You use these binary operators to find out whether a value is the same as another value of the same type. The following table summarizes how these operators work, using an int variable called age as an example. Operator Meaning Example Outcome if age is 42 -- Equal to age -- 100 false != Not equal to age != 0 true Closely related to these two operators are the relational operators. You use these operators to find out whether a value is less than or greater than another value of the same type. The following table shows how to use these operators. Operator Meaning Example Outcome if age is 42 < Less than age < 21 false 16 true >= Greater than or equal to age >= 30 true NOTE Don't confuse the equality operator == with the assignment operator =. Code such as x==y compares x to y and has the value true if the values are the same. Code such as x=y assigns the value of y to x. Understanding Conditional Logical Operators C# also provides two other Boolean operators: the logical AND operator, which is represented by the && symbol, and the logical OR operator, which is represented by the || symbol. Collectively, these are known as the conditional logical operators. Their purpose is to combine Boolean expressions together into bigger expressions. These binary operators are similar to the equality and relational operators in that their outcome
  2. is either true or false, but they differ in that the values they operate on must themselves be either true or false. The outcome of the && operator is true if and only if both of the Boolean expressions it operates on are true. For example, the following statement assigns the value true to validPercentage if and only if the value of percent is greater than or equal to zero and the value of percent is less than or equal to 100: bool validPercentage; validPercentage = (percent >= 0) && (percent
  3. In this expression, if the value of percent is less than zero, the Boolean expression on the left side of && evaluates to false. This value means that the result of the entire expression must be false, regardless of the remaining expression; therefore, the Boolean expression on the right side of && is not evaluated. (percent < 0) || (percent > 100) In this expression, if the value of percent is less than zero, the Boolean expression on the left side of || evaluates to true. This value means that the result of the entire expression must be true; therefore, the Boolean expression on the right side of || is not evaluated. If you carefully design expressions that use the conditional logical operators, you can boost the performance of your code by avoiding unnecessary work. Place simple Boolean expressions that can be evaluated easily on the left side of a conditional logical operator and put more complex expressions on the right side. In many cases, you will find that the program does not need to evaluate the more complex expressions. Summarizing Operator Precedence and Associativity The following table summarizes the precedence and associativity of all the operators you have learned about so far. Operators in the same category have the same precedence. Operators in a higher category take precedence over operators in a lower category. Category Operators Description Associativity () Precedence override Primary ++ Post-increment Left -- Post-decrement ! Logical NOT + Addition Unary - Subtraction Left ++ Pre-increment -- Pre-decrement * Multiply Multiplicative / Divide Left % Division remainder + Addition Additive Left - Subtraction < Less than Greater than >= Greater than or equal Equality == Equal to Left
  4. Category Operators Description Associativity != Not equal to Conditional AND && Logical AND Left Conditional OR || Logical OR Left Assignment = Right
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2