Danh mục

Using Boolean Operators

Số trang: 4      Loại file: pdf      Dung lượng: 12.79 KB      Lượt xem: 1      Lượt tải: 0    
Thư viện của tui

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

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 (!).
Nội dung trích xuất từ tài liệu:
Using Boolean Operators Using Boolean OperatorsA Boolean operator is an operator whose result is either true or false. C# has several veryuseful Boolean operators, the simplest of which is the NOT operator, which isrepresented 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 variableareYouReady is true, the value of the expression !areYouReady is false.Understanding Equality and Relational OperatorsTwo 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 asanother value of the same type. The following table summarizes how these operatorswork, 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 trueClosely related to these two operators are the relational operators. You use theseoperators to find out whether a value is less than or greater than another value of the sametype. 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 trueNOTEDont confuse the equality operator == with the assignment operator =. Code such asx==y compares x to y and has the value true if the values are the same. Code such as x=yassigns the value of y to x.Understanding Conditional Logical OperatorsC# also provides two other Boolean operators: the logical AND operator, which isrepresented by the && symbol, and the logical OR operator, which is represented by the|| symbol. Collectively, these are known as the conditional logical operators. Theirpurpose is to combine Boolean expressions together into bigger expressions. Thesebinary operators are similar to the equality and relational operators in that their outcomeis either true or false, but they differ in that the values they operate on must themselves beeither true or false.The outcome of the && operator is true if and only if both of the Boolean expressions itoperates on are true. For example, the following statement assigns the value true tovalidPercentage if and only if the value of percent is greater than or equal to zero and thevalue of percent is less than or equal to 100:bool validPercentage;validPercentage = (percent >= 0) && (percent In this expression, if the value of percent is less than zero, the Boolean expression on theleft side of && evaluates to false. This value means that the result of the entireexpression must be false, regardless of the remaining expression; therefore, the Booleanexpression 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 theleft side of || evaluates to true. This value means that the result of the entire expressionmust 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 canboost the performance of your code by avoiding unnecessary work. Place simple Booleanexpressions that can be evaluated easily on the left side of a conditional logical operatorand put more complex expressions on the right side. In many cases, you will find that theprogram does not need to evaluate the more complex expressions.Summarizing Operator Precedence and AssociativityThe following table summarizes the precedence and associativity of all the operators youhave 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 overridePrimary ++ Post-increment Left -- Post-decrement ! Logical NOT + AdditionUnary - Subtraction Left ++ Pre-increment -- Pre-decrement * MultiplyMultiplicative / Divide Left % Division remainder + AdditionAdditive Left - Subtraction < Less than Greater than >= Greater than or equalEquality == Equal to LeftCategory Operators Description Associativity != Not equal toConditional AND && Logical AND LeftConditional OR || Logical OR LeftAssignment = Right

Tài liệu được xem nhiều: