Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 8 EXPRESSIONS AND OPERATORS

Increment and Decrement Operators

The increment operator adds 1 to the operand. The decrement operator subtracts 1 from the operand. Table 8-9 lists the operators and their descriptions.

These operators are unary and have two forms, the pre- form and the post- form, which act differently.

In the pre-form, the operator is placed before the operand; for example, ++x and --y.

In the post-form, the operator is placed after the operand; for example, x++ and y--.

Table 8-9. The Increment and Decrement Operators

Operator

Name

Description

++Pre-increment ++var Increment the value of the variable by 1 and save it back into

the variable. Return the new value of the variable.

Post-increment var++ Increment the value of the variable by 1 and save it back into the variable. Return the old value of the variable before it was incremented.

--Pre-decrement--var Decrement the value of the variable by 1 and save it back into

the variable. Return the new value of the variable.

Post-decrement var-- Decrement the value of the variable by 1 and save it back into the variable. Return the old value of the variable before it was decremented.

In comparing the preand post-forms of the operators

The final, stored value of the operand variable after the statement is executed is the same regardless of whether the preor post-form of the operator is used.

The only difference is the value returned by the operator to the expression.

Table 8-10 shows an example summarizing the behavior.

217

CHAPTER 8 EXPRESSIONS AND OPERATORS

Table 8-10. Behavior of Preand Post-Increment and Decrement Operators

 

 

Value Returned to the

Value of Variable

 

Expression: x = 10

Expression

After Evaluation

 

 

 

 

Pre-increment

++x

11

11

Post-increment

x++

10

11

Pre-decrement

--x

9

9

Post-decrement

x--

10

9

 

 

 

 

For example, the following is a simple demonstration of the four different versions of the operators. To show the different results on the same input, the value of the operand x is reset to 5 before each assignment statement.

int

x = 5, y;

 

y =

x++;

//

result: y: 5, x: 6

Console.WriteLine("y: {0}, x: {1}" , y, x);

x

=

5;

 

y

=

++x;

// result: y: 6, x: 6

Console.WriteLine("y: {0}, x: {1}" , y, x);

x = 5;

y = x--; // result: y: 5, x: 4

Console.WriteLine("y: {0},

x: {1}" , y, x);

x

=

5;

 

 

y

=

--x;

// result: y: 4, x: 4

Console.WriteLine("y: {0},

x: {1}" , y, x);

This code produces the following output:

y: 5, x: 6 y: 6, x: 6 y: 5, x: 4 y: 4, x: 4

218

CHAPTER 8 EXPRESSIONS AND OPERATORS

Conditional Logical Operators

The logical operators are used for comparing or negating the logical values of their operands and returning the resulting logical value. Table 8-11 lists the operators.

The logical AND and logical OR operators are binary and left-associative. The logical NOT is unary.

Table 8-11. The Conditional Logical Operators

Operator

Name

Description

&&

Logical AND

true if both operands are true; false otherwise

||

Logical OR

true if at least one operand is true; false otherwise

!

Logical NOT

true if the operand is false; false otherwise

 

 

 

The syntax for these operators is the following, where Expr1 and Expr2 evaluate to Boolean values:

Expr1 && Expr2 Expr1 || Expr2

! Expr

219

CHAPTER 8 EXPRESSIONS AND OPERATORS

The following are some examples:

 

 

 

 

 

bool bVal;

 

 

bVal = (1 == 1) && (2 == 2);

// True, both operand expressions are true

bVal = (1 == 1) && (1 == 2);

// False, second operand expression is false

bVal = (1 == 1) || (2 == 2);

// True, both operand expressions are true

bVal = (1 == 1) || (1 == 2);

// True, first

operand expression is true

bVal = (1 == 2) || (2 == 3);

// False, both

operand expressions are false

 

 

 

bVal = true;

// Set bVal to

true.

bVal = !bVal;

// bVal is now

false.

The conditional logical operators operate in “short-circuit” mode, meaning that, if after evaluating Expr1 the result can already be determined, then it skips the evaluation of Expr2. The following code

shows examples of expressions in which the value can be determined after evaluating the first operand:

bool bVal;

bVal = (1 == 2) && (2 == 2); // False, after evaluating first expression bVal = (1 == 1) || (1 == 2); // True, after evaluating first expression

Because of the short circuit behavior, do not place expressions with side effects (such as changing a value) in Expr2, since they might not be evaluated. In the following code, the post-increment of variable iVal would not be executed, because after executing the first subexpression, it can be determined that the value of the entire expression is false.

bool

bVal; int iVal =

10;

 

bVal

= (1 == 2) && (9

== iVal++);

// result: bVal = False, iVal = 10

 

 

 

 

 

 

False

Never evaluated

 

220

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]