Expressions

   

Expressions are statements that, when executed, result in a value. When programming, you use expressions all the time, sometimes without even realizing it. The following are examples of expressions:

VB

 65 + 5 ' Produces a value of 70.  ( i < 10 ) ' Produces a true or false. 

C#

 5 * 100 // Produces a value of 500  x = 25 - 5 // Subtracts 5 from 25 then stores in x 

JScript

 175 - 4 // Produces 171  15 / 3 // Produces 5 

Expressions typically are composed of several smaller expressions, or subexpressions , connected by operators. For instance, consider the following lines of C# code:

 int x = 100, y;  y = ( x / 4 ) + 3; 

The first line of code, where the variables are declared, actually contains an expression in the assignment of 100 to the x variable. The integer literal 100 is an expression, albeit a simple one. When the compiler looks at this line, it sees something like "evaluate the expression to the right of the assignment operator = and place its value in the variable on the left." Because the expression is the integer literal 100, it evaluates to 100 and is then stored in the x variable. Pretty simple, yet it is an expression.

Now take a look at the next C# line. The complete expression would be

 y = ( x / 4 ) + 3; 

However, this expression is made up of several subexpressions. You might recognize one on the right off the bat:

 x / 4 

Although you'd be correct in assuming this to be a subexpression, there are even subexpressions inside this simple statement. The x variable is a subexpression that evaluates to 100, and the integer literal 4 is another subexpression that evaluates to 4. After each of these is evaluated and the division operation is performed, the result is 25. To this, the value 3 is added, another subexpression. And finally, the entire value (28) is placed inside the y variable. As you can see, there can be many levels of expressions, even in what appear to be simple statements.

Operators

Because expressions are typically made up of several subexpressions linked together by operators, it's important to understand exactly how operators work. The C# and JScript languages support both unary and binary operators. Unary operators are those that act on a single operand, whereas binary operators act on two operands.

The following example is a JScript expression using the unary postfix increment operator. As you can see, it requires only the one operand (x, in this case):

 x++; 

And here is its functional equivalent, using the binary addition operator:

 x = x + 1; 

As you can see, the binary addition operator acts on two operands (x and 1). After the right side of the assignment operator is evaluated, the result is stored in the x variable on the left.

These two expressions do the same thing: they increase the value of x by one. However, a different operator has been used in each case. In the first example, the unary operator ++ was used. In the second example, the binary addition operator + was used.

There are actually two different kinds of unary operators ”the prefix and the postfix versions. When you see an expression, such as nVariable++ , it means that the variable will be incremented after it is used (such as in a comparison). If you see -++nVariable , it means that the variable is incremented before it is used.

Operator Precedence

Expressions are evaluated from left to right, according to the precedence of the operators in the expression. By following the precedence order, languages guarantee that a particular expression produces the same results every time it is executed. For example, take into consideration the following VB example:

 x = 15 + 3 * 2 - 14 

Without a precedence order, which subexpression is evaluated first? Is it 15 + 3, 3 * 2, or 2 - 14? And after the first subexpression is evaluated, which is next? Clearly, the value of the expression changes, depending on the order in which its subexpressions are evaluated.

If you couldn't rely on the order in which operations are performed, it would make your programs about at consistent as the New Your City subway system. Luckily, operators are arranged and executed in order of precedence. Table 2.1 lists all C#, JScript, and VB operators according to their precedence order. Note that I've placed parentheses first in this table. Parentheses aren't really operators because they don't perform an operation on data types, but they appear in the table nonetheless to emphasize that they're given top priority during evaluation. The associativity is the direction in which the operations are performed, so right to left or left to right.

Table 2.1. Operator Precedence

Precedence

Associativity

Operator

Description

Languages

First

 

()

Parentheses (forcing order)

VB, C#, JScript

Second

R-to-L

++

Pre/post increment

C#, JScript

 

R-to-L

Pre/post decrement (unary)

C#, JScript

 

R-to-L

!

Logical complement (unary)

C#, JScript

 

R-to-L

~

Unary bitwise logical negation

C#, JScript

 

R-to-L

+

Addition (unary)

VB, C#, JScript

 

R-to-L

 

Subtraction (unary)

VB, C#, JScript

Third

L-to-R

* / %

Multiplication, division, and remainder

C#, JScript

 

L-to-R

* / Mod

Multiplication, division, and remainder

VB

Fourth

L-to-R

+

Addition and subtraction

VB, C#, JScript

Fifth

L-to-R

<<

Shift left

C#, JScript

 

L-to-R

>>

Shift right (sign extension)

C#, JScript

Sixth

L-to-R

< <=

"Less than" and "less than or equal to"

VB, C#, JScript

 

L-to-R

> >=

"Greater than" and "greater than or equal to"

VB, C#, JScript

Seventh

L-to-R

==

Equality

C#, JScript

 

L-to-R

=

Equality

VB

 

L-to-R

!=

Non-equality

C#, JScript

 

L-to-R

<>

Non-equality

VB

Eighth

L-to-R

&

Bitwise AND, Boolean AND

C#, JScript

Ninth

L-to-R

^

Bitwise XOR, Boolean XOR

C#, JScript

Tenth

L-to-R

Bitwise OR, Boolean OR

C#, JScript

Eleventh

L-to-R

&&

AND (Boolean conditional " short-circuit ")

C#, JScript

 

L-to-R

AND

AND (Boolean conditional "short circuit")

VB

Twelfth

L-to-R

OR (Boolean conditional)

C#, JScript

 

L-to-R

OR

OR (Boolean conditional)

VB

Thirteenth

R-to-L

=

Assignment

VB, C#, JScript

Fourteenth

R-to-L

+= -= *= /= %=

Assignment (and operation)

C#, JScript

Because all operations are performed according to the precedence of the operators involved, expressions are evaluated in a predictable manner. Operations whose operators have the highest precedence are performed first, with lower precedence operators following in sequence. When operations of the same precedence occur within the same expression, they are processed from left to right.

Now, return to the example:

 x = 15 + 3 * 2 - 14 

Thanks to operator precedence, you can see that the expression evaluates in a specific order. The middle operation ( 3 * 2 ) is performed first, and then the next two operations are carried out from left to right:

3 * 2

produces 6, which is used in the next operation

15 + 6

produces 21, which is used in the next operation

21 - 14

produces 7, which is stored in the variable x

Forcing Order

If you don't want the natural order of evaluation to be used, you can use parentheses to control the order of evaluation. For example, suppose you wanted the subexpression 2 - 14 to be evaluated first. You could ensure this by placing it inside parentheses:

 x = 15 + 3 * ( 2 - 14 ) 

In this case, the following sequence of steps is taken when the expression is evaluated:

2 - 14

produces -12, which is used in the next operation

3 * -12

produces -36, which is used in the next operation

15 + -36

produces -12, which is stored in the variable x

Because multiplication operators have precedence over arithmetic ones, this operation is performed after the subexpression inside parentheses. However, you could have added another pair of parentheses to force the multiplication operation to take place last:

 x = ( 15 + 3 ) * ( 2 - 14 ) 

In this case, the subexpressions inside the parentheses are of the same precedence. As a result, they are carried out from left to right. This sequence of steps is as follows :

15 + 3

produces 18, which is used in the final operation

2 - 14

produces -12, which is used in the final operation

18 * -12

produces -216, which is stored in the variable x

And of course, parentheses can be nested. You could, for example, group these three subexpressions by surrounding them with parentheses and adding another subexpression to the mix. Here are a few examples, each producing a different data type:

x = ( ( 15 + 3 ) * ( 2 - 14 ) ) + 1

Produces integer value

x = ( ( 15 + 3 ) * ( 2 - 14 ) ) + 1.2

Produces a floating point value

x = ( ( 15 + 3 ) * ( 2 - 14 ) ) > 1;

Produces a Boolean value

Array Operators

When operations are performed on arrays, they return the value of a specific element in that array. However, unlike the data types dealt with this far, an array element must be allocated using the new operator before it can be assigned to a variable, except with VB you must declare the size of the array as the following three examples show:

VB

 Dim a(3) as Integer 

C#

 int[] a = new int[15]; 

JScript

 var a : int[] = new int[15]; 

In the preceding examples, an array of 15 integer elements is created and assigned to the variable a . After this operation has taken place, you can store and retrieve values in the array elements using the following syntaxes:

VB

 nArrayVariable=(expression) 

C#

 nArrayVariable[expression] 

JScript

 nArrayVariable[expression] 

For example, each of the following C# lines of code accesses the same element in the array:

 int x = 5, y = 2, z = 10; // Initialize variables.  a[10] = 82569;            // Store 82569 in the 11th element.  a[z] = 4370;              // Store 4370 in the 11th element.  a[x*y] = 1117911;         // Store 1117911 in the 11th element.  a[x+5] = 592;             // Store 592 in the 11th element.  int i;  i = a[10];                // Retrieve the 11th element.  i = a[x+5];               // Retrieve the 11th element.  i = a[100/z+1];           // Retrieve the 11th element. 

If, in any case, an array index is negative or greater than the number of elements in the array minus one (which would be 14 in this example), an exception is thrown. Using the example array, the following operations would cause an exception:

 a[x+2];      // Expression evaluates to 15  a[x*x];      // Expression evaluates to 25 

Array index values can be of short , int , or long types, as the following JScript example shows:

 var x : short = 0;  var y : int = 5;  var z : long = 9;  a[x];  a[y];  a[z]; 

Keep in mind that array indexing begins at zero. In the preceding example, as with all arrays, the first element is accessed with an index value of zero: a[0] . Because this particular array has 15 elements (from index 0 to 14), the last element is referenced with an index value of 14: a[[14] .

   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net