BACS 387 - University of Northern Colorado

Download Report

Transcript BACS 387 - University of Northern Colorado

Language Elements 2
Expressions & Operators
Learn the basics of the components that make up
expressions.
 Learn the operators supported by C#.
 Learn the order of precedence of C# operators.

7/18/2015
OO Programming
2
Expressions are one of the basic building blocks of
any modern language.
 Expressions are strings of operators and operands
that can be used to build statements.
 In C#, a variety of constructs can act as operands,
including:

 Literals
 Constants
 Variables

Method calls
Array assessors and indexes
Other expressions
When an expression is evaluated, it returns a value
to the position where the expression resides.
7/18/2015
OO Programming
3

The following are examples of valid expressions.
int a = 10, b = 15, c;
string x = “Hi”, y = “mom”, z;
bool r, s;
c
z
c
c
r
s
c
=
=
=
=
=
=
=
a + b;
x + “ ” + y;
++a;
a++;
(1 == 1) && (2 == 2);
(1 == 1) && (2== 3);
(a + b) * (b – a) / 2;
//
//
//
//
//
//
//
result is
result is
result, c
result, c
true
false
result is
25
“Hi mom”
= 11, a = 11
= 10, a = 11
62 (int truncates)
int p = a < b ? 8 : b;
// result is 8 (since a is < b)
Console.WriteLine(“answer is: {0}”, a * b);
// answer is: 150
7/18/2015
OO Programming
4

C# operators fall into the following categories:
 Arithmetic operators
 Increment/Decrement operators
 Unary arithmetic operators
 Assignment operators
 Comparison operators
 Conditional logical operators
 Bitwise logical operators
 Shift operators
7/18/2015
OO Programming
5

Basic Operators
Operator
Name
Description
+
*
/
addition
adds two operands
subtraction
subtracts the right operand from the left
multiplication
multiplies the two operands
division (float)
divides the right operand into the left
%
modulus
returns the remainder from a division
7/18/2015
OO Programming
6
Operator
++
--
++
--
7/18/2015
Name
Description
Pre-increment
++X
Add 1 to the operand (x + 1) and save it.
Return the new value of the variable.
Pre-decrement
Subtracts 1 from the operand (x – 1) and save
it. Return the new value of the variable.
--X
Post-increment
X++
Add 1 to the operand (x + 1) and save it.
Return the old value before the update.
Post-decrement Subtracts 1 from the operand (x – 1) and save
it. Return the old value before the update.
X--
OO Programming
7
int
int
int
int
a
b
c
d
=
=
=
=
5;
5;
4;
4;
int
int
int
int
w
x
y
z
=
=
=
=
++a;
b++;
--c;
d--;
7/18/2015
//
//
//
//
a
b
c
d
=
=
=
=
6,
6,
3,
3,
w
x
y
z
OO Programming
=
=
=
=
6
5
3
4
8
Operator
+
-
7/18/2015
Name
Description
Positive sign
Returns the numeric value of the operand.
Negative sign
Returns the inverse of the value of the operand
(i.e., multiplied by -1).
OO Programming
9
Operator
=
Name
Description
assignment
Assigns a new value to the variable. Note, this
is not used to test equality.
+=
addition
Adds the right operand to the value stored in
the variable and assigns the result to the
variable (e.g., x += 1 same as x = x + 1)
-=
subtraction
Same as above, but with subtraction
*=
/=
%=
multiplication
Same as above, but with multiplication
division
Same as above, but with division
modulus
Same as above, but with modulo math
7/18/2015
OO Programming
10
int a = 10;
int b;
b
a
a
a
a
a
= 7;
+= 2;
-= 2;
*= 3;
/= 2;
%= 3;
7/18/2015
//
//
//
//
//
//
value
value
value
value
value
value
of
of
of
of
of
is
b
a
a
a
a
1
is
is
is
is
is
(a
OO Programming
7
12 (a = a + 2)
8 (a = a – 2)
30 (a = a * 3)
5 (a = a / 2)
/ 3 = 3 remainder 1)
11
Operator
Name
Description
==
Equal to
True if the first operand is equal to the second.
False otherwise. Note: a single = is used for
assignment, not equality comparison.
!=
Not equal to
True if the first operand is not equal to the
second. False otherwise.
>
Greater than
True if the first operand is greater than the
second. False otherwise.
<
Less than
True if the first operand is less than the first.
False otherwise.
>=
Greater than or
equal to
True if the first operand is greater than or equal
to the second. False otherwise.
<=
Less than or
equal to
True if the first operand is less than or equal to
the second. False otherwise.
7/18/2015
OO Programming
12
int num1 = 5;
int num2 = 6;
num1
num1
num1
num1
num1
num1
7/18/2015
== num2;
!= num2;
> num2;
< num2;
<= num2;
>= num2;
//
//
//
//
//
//
OO Programming
false
true
false
true
true
false
13

Conditional Logic Operators
Operator
Name
Description
Logical AND
Returns true if both operands are true, returns false
otherwise.
||
Logical OR
Returns true if at least one of the operands is true,
returns false otherwise.
!
Logical NOT
Returns true if the operand is false. Returns false if
the operand is true. (unary operand)
&&

The Conditional Operator
Operators
? :
7/18/2015
Name
Description
Ternary
operator
A shorthand form of an if…then statement. (e.g.,
condition ? true_value : false_value)
OO Programming
14
int i = 6;
int j = 12;
bool a = false;
bool
bool
bool
bool
aa
aa
aa
aa
= !a;
= i>3 && j<10;
= i>3 || j<10;
// value aa true
// value aa false
// value aa true
= (i>3 && J<10) || (i<7 && j>10);
// value aa true
int c = x < y ? 7 : 9;
7/18/2015
// value c is 7
OO Programming
15
Operator
Name
Description
&
Bitwise AND
On a bit-by-bit basis, performs the AND
operation on the two operands.
|
Bitwise OR
On a bit-by-bit basis, performs the OR operation
on the two operands.
^
Bitwise XOR
On a bit-by-bit basis, performs the XOR
operation on the two operands.
~
7/18/2015
Used a single operator. Each bit in the operand
Bitwise negation is switched to its opposite (1’s to 0’s and 0’s to
1’s). This produces the complement of the
operand.
OO Programming
16
byte x = 12;
byte y = 10;
sbyte a;
a = x & y;
// value a = 8
a = x | y;
// value a = 14
a = x ^ y;
// value a = 6
a = ~x;
// value a = -13
7/18/2015
OO Programming
17
Operator
<<
>>
7/18/2015
Name
Left shift
Right shift
Description
Shifts the bits that make up the operand the
specified number of positions to the left. Bit
positions opened up on the right are filled with
0’s.
Shifts the bits that make up the operand the
specified number of positions to the right. Bits
shifted off the right side are lost.
OO Programming
18
int a;
int b;
int x = 14;
a = x << 3
// a is 112
b = x >> 3
// b is 1
7/18/2015
OO Programming
19
Operators
Precedence
++x, --x (prefixes), ( ) ,+ - (unary), !, ~
Highest
*, /, %
+, <<, >>
<, >, <=, >=
==, !=
&
^
|
&&
||
=, +=, -=, *=, /=, %=, <<=,>>=, &=, ^=, |=
Lowest
X++, x– (suffixes)
7/18/2015
OO Programming
20

As stated earlier, the operand types are:







Literals
Variables
Constants
Method calls
Array assessors and indexes
Other expressions
The first three of these are covered in the slides that
follow.
7/18/2015
OO Programming
21
Literals are strings or numbers directly typed into
the source code to represent a specific, set value of a
specific data type.
 C# supports 4 categories of literals:

 Integer literals
 Real literals
 Character literals
 String literals
7/18/2015
OO Programming
22
Integer literals are merely a sequence of digits with
no decimal point and an optional suffix to specify
the type of integer.
 Valid Examples:






int
myInteger = 125;
// an integer
long myLong = 220L;
// a long integer
uint myUnsigned = 176U;
// an unsigned integer
ulong myUnsignedLong = 320UL;
// an unsigned long integer
Error Examples:
 int
myInteger = 3000000000;
 long myLong = “220L”;
 uint myUnsigned = -176;
7/18/2015
OO Programming
// too big for integer
// not a number
// no negatives allowed
23


Real literals are a sequence of digits with an optional
decimal point, optional exponent, and optional suffix.
Valid Examples:
 float
myFloat = 260F;
// a float
 double myDouble = -274.23D;
// a double
 double myDouble2 = 3.45e-15;
// double with exponent notation
 decimal myDecimal = 45.35453421M;
// decimal value (high precision)

Error Examples:
 float
myFloat = 165U;
 double myDouble = “144.55D”;
 double myDouble2 = 9.25e410 ;
7/18/2015
OO Programming
// wrong type of suffix
// not a number
// too big for double
24


Character literals are a sequence of characters between
two single quote marks. They can be a single character,
a simple escape sequence, or a hex or Unicode escape
sequence.
Valid Examples:
 char char1 = ‘r’;
// single character (r)
 char char2 = ‘\t’;
// a simple escape sequence (tab character)
 char char3 = ‘\x0043’;
// a hex escape sequence
 char char4 = ‘\u004a’;
// a Unicode escape sequence

Error Examples:
 char char1 = “r”;
 char char2 = ‘/t’;
7/18/2015
// wrong type of quote mark
// wrong type of slash
OO Programming
25



String literals are a sequence of characters between
two double quote marks. They can be a regular
string literal or a verbatim string literal.
Verbatim literals are preceded by @ and are printed
exactly as shown (including escape sequences).
Valid Examples:
 string st1 = “A string”;
// regular string
 string st2 = “A \t regular \n string”;
// regular string with esc chars
 string st3 = @ “A \t verbatim one”;
// verbatim string (prints the /t)

Error Examples:
 string st1 = ‘A string’;
// wrong type of quote
 string st2 = “A /t regular \n string”; // backwards slash
 string st3 = “A \t verbatim one”;
// forgot the @ symbol
7/18/2015
OO Programming
26





Variables represent memory locations capable of
storing changeable data.
Consequently, variables are used in expressions as
placeholders for changeable data.
When a variable is evaluated in an expression, its
current value is substituted into the expression.
Constants are identical to variables with the
exception that they cannot change during the
execution of the program.
The value of a constant is also substituted into the
expression prior to its evaluation.
7/18/2015
OO Programming
27



Expressions are strings of operators and operands
that can be used to build statements.
C# operators fall into the following categories:








Arithmetic operators
Increment/Decrement operators
Unary arithmetic operators
Assignment operators
Comparison operators
Conditional logical operators
Bitwise logical operators
Shift operators
In C#, a variety of constructs can act as operands,
including:
 Literals
 Constants
 Variables
7/18/2015
Method calls
Array accessors and indexes
Other expressions
OO Programming
28