Professional Documents
Culture Documents
Computer Programming Week 4: Operators
Computer Programming Week 4: Operators
Computer Programming Week 4: Operators
Week 4
Operators
13.10.2014
arithmetic,
relational,
and logical.
13.10.2014
Arithmetic Operators
13.10.2014
You can obtain the remainder of this division by using the modulus
operator, %. The % is also referred to as the remainder operator. It
yields the remainder of an integer division. For example, 10 % 3 is 1. In
C#, the % can be applied to both integer and floating-point types. Thus,
10.0 % 3.0 is also 1.
Example shows % operator for int and double type (Figure 5.1)
13.10.2014
13.10.2014
As you can see, the % yields a remainder of 1 for both integer and
floating-point operations.
13.10.2014
a
b
13.10.2014
13.10.2014
13.10.2014
10
The circled numbers under the statement indicate the order in which
C# applies the operators. The multiplication, remainder and division
operations are evaluated first in left-to-right order (i.e., they
associate from left to right), because they have higher precedence
than addition and subtraction. The addition and subtraction
operations are evaluated next. These operations are also applied
from left to right.
13.10.2014
11
13.10.2014
12
If the two operands of an arithmetic operation are of the int data type
the result is an int value. If the two operands are float the result
is float. If the two operands are of the same data type the result is
the same data type as the operands. If the two operands are mixed,
(e.g., one operand is int and the other is float, the arithmetic is
known as mixed-mode arithmetic) and the result has the float data
type.Table 5.3 shows the data type of the result when mixed-mode
arithmetic is involved. The following examples show mixed-mode
operations:
13.10.2014
13
13.10.2014
14
13.10.2014
15
13.10.2014
16
13.10.2014
17
then y will be set to 10. In this case, the value of x is first obtained, x is
incremented, and then the original value of x is returned. In both cases,
x is still set to 11. The difference is what is returned by the operation.
13.10.2014
18
13.10.2014
19
y= y+ x++
y= y+ ++x
10
14
15
10
20
14
21
15
27
20
28
21
35
27
36
28
44
35
45
36
54
44
10
55
45
10
65
54
11
13.10.2014
20
13.10.2014
21
13.10.2014
22
13.10.2014
23
24
For the logical operators, the operands must be of type bool, and
the result of a logical operation is of type bool. The logical
operators, &, |, ^, and !, support the basic logical operations AND,
OR, XOR, and NOT, according to the following truth Table 5.6:
13.10.2014
25
Table 5.6 The basic logical operations AND, OR, XOR, and NOT
13.10.2014
26
13.10.2014
27
13.10.2014
28
13.10.2014
29
13.10.2014
30
13.10.2014
31
13.10.2014
32
13.10.2014
33
13.10.2014
34
var-name= expression;
Here, the type of var-name must be compatible with the type of
expression.
The assignment operator allows you to create a chain of assignments.
For example, consider this fragment:
13.10.2014
35
Compound Assignments
is the same as
13.10.2014
36