Expressions, Relational operations, Logical operations...


In programming languages, expression means a combination of constants, variables, method calls (return values), joined by operation signs and parentheses, which returns some result as a result of the calculation. When calculating expressions , its value and type are determined. The type of result of the expression depends on the types of variables and constants involved in this expression, and is the most complex type of variables and constants in accordance with Fig. 3.4. The simplest type is byte, and the most complex type is decimal. For example:

int a = 5; float f;

f = a/4; // value f = 1.0, because type result a/4 - int f = a/4f; // value a = 1.25, because the result type is float

Relational operations

Relational operations are used to compare the values ​​of variables and constants. There are 6 relationship operations in total: == (equal),! = (Not equal), & lt; (less), & gt; (more), & lt; = (less than or equal to), & gt; = (greater than or equal to).

You should pay attention to the record of the operation "is equal to", as == (two characters to assign =), and "no equal", like! =.

Also note that when comparing reference variables, not objects are compared, but object references (if the comparison operation is not overridden for this type).

Logical operations

In the C # language, logical operations fall into two categories: one performed on the logical values ​​of the operands, the other performing the logical operation on the bits of the operands. For this reason, in C #, there are two unary negation operations-the logical negation specified by the! Operator, and the bitwise negation specified by the operation ~. The first of them is defined over the operand of type bool, the second - over the operand of integer type, starting with type int and higher (int, uint, long, ulong). The result of the operation in the second case is the operand, in which each bit is replaced by its complement (0 by 1 and 1 by 0). Consider the following example: bool b1, b2;

M = 2 * 2 == 4; // M = true;

b2 =! b1; // b2 = false;

// b2 = ~ b1; // error ! uint j1 = 7, j2;

j2 = ~ j1; // j2 = 4294967288

// j2 =! j1; //error! int j4 = 7, j5; j5 = ~ j4; // j5 = -8

In this fragment, operators are annotated, leading to errors. In the first case, an attempt was made to apply a bitwise negation operation to the expression of the bool type, in the second case, a logical negation was applied to the integer data. Both in C # are illegal. Note the different interpretation of bitwise negation for unsigned and signed integer types. For variables j5 and 2 , the bit string that specifies the value is the same, but is interpreted in different ways.

Binary logical operations ( & amp; - conditional AND and || - conditional OR ) are defined only on data of type bool. The operations are called conditional, or short, because the second operand will be evaluated, depends on the already computed value of the first operand.

In & & amp; operation, if the first operand is false, the second operand is not evaluated and the result of the operation is false. Similarly, in the ||: operation, if the first operand is true, then the second operand is not evaluated and the result of the operation is true.

The value of conditional logical operations does not consist in their effectiveness in terms of execution time. Often they allow us to compute a logical expression that makes sense, but in which the second operand is not defined. For example, consider the task of finding an array element. A given element in an array may or may not be. In this case, the typical solution of this problem will be:

// Conditional And - & amp; amp; int [] ar = {1,2,3};

intsearch = 7, i = 0;

// search - the specified value

while ((i & lt; ar.Length) & amp; (ar [i]! = search)) i ++;

if (i & lt; ar.Length)

Console.WriteLine ("Value found"); else

Console.WriteLine ( Value not found );

If the value of the search variable does not match any of the values ​​of the elements of the ar array, the last test of the while loop condition will be executed with the value of i equal to ar.Length. In this case, the first operand will be false, and although the second operand is not defined, the loop will normally exit. The second operand is not defined in the last check, because the index of the array element is out of range (in C #, indexing of elements starts from zero). Note that normal the conjunction requires the calculation of both operands, so its use in this program would lead to an exception in the case where the sample is not in the array.

Three binary bitwise operations: & amp; - AND, | - OR, ^ - XOR can be used both with integer types above int, and with Boolean types. In the first case, they are used as bitwise operations, in the second - as usual logical operations. Sometimes it is necessary that both operands be evaluated in any case, then these operations can not be dispensed with. Here is an example of their first use:

// Logical bitwise operations And, Or, XOR (& amp ;, |, ^) int k2 = 7, k3 = 5, k4, k5, kb; k4 = k2 & amp; k3; k5 = k2 | k3; k6 = k2kS;

Conditional operation

In C #, there is a conditional operation that starts with the condition (expression of type bool) enclosed in parentheses, followed by a question mark and a pair of If the condition is true, the first of the is executed, otherwise the result is the value of the second expression. For example: int a = 7, b = 9, max;

max = (a & gt; b)? a: b; // max will get a value of 9.

thematic pictures

Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)