INTRODUCTION TO ARITHMETIC EXPRESSION:
EXPRESSIONS:
Expressions are
things that have values
A variable by itself is
an expression: radius
A constant by itself is
an expression: 3.14
Often expressions are combinations of
variables, constants, and operators.
area = 3.14 * radius * radius;
Expression Evaluation:
Some terminology:
Data or
operand means the integer or
floating-point constants and/or variables in the expression.
Operators are things like addition, multiplication, etc.
The value of an
expression will depend on the data types and values
and on the operators used
Additionally, the
final result of an assignment statement will depend on the type of the assignment
variable.
Arithmetic Types: Review:
C#
provides two
different kinds of numeric values
Integers
(0, 12, -17, 142)
Type
int
Values
are exact
Constants
have no decimal point or exponent
Floating-point
numbers (3.14, -6.023e23)
Type
double
Values
are approximate (12-14 digits precision typical)
Constants
must have decimal point and/or exponent
OPERATORS:
¨Binary: operates on two operands
¡3.0 * b
¡zebra + giraffe
¨Unary: operates on one operand
¡-23.4
¨C# operators are unary or binary
¨Puzzle: what about
expressions like a+b+c?
¡Answer: this is two
binary ops, in sequence
Expressions with doubles:
¨Constants of type
double:
¡0.0, 3.14,
-2.1, 5.0, 6.02e23,
1.0e-3
¡not
0
or 17
¨Operators on doubles:
¡unary: -
¡binary: +, -,
*, /
¡Note: no
exponentiation operator in C#
Example Expressions with doubles:
¨Declarations
¨ double height, base, radius,
x, c1, c2;
¨Sample expressions
(not statements):
¨ 0.5 * height * base
¨ ( 4.0
/ 3.0 ) * 3.14 * radius * radius * radius
¨ -
3.0 + c1 * x - c2 * x * x
Expressions with ints:
¨Constants of type int:
¡0, 1,
-17, 42
¡not
0.0
or 1e3
¨Operators on ints:
¡unary: -
¡binary: +, -, *, /,
%
int Division and Remainder:
¨Integer operators
include integer
division
and integer
remainder:
¨ symbols / and %
¨
¨Caution:
division looks like an old friend, but there is a new wrinkle!
int Division and Remainder:
****/ is integer
division: no remainder, no rounding
299/100 = 2
6/4 = 1
****% is mod or remainder:
299%100 = 99
6%4 = 2....
Why Use int? Why Not doubles Always? :
Sometimes only ints make sense
the 15th
spreadsheet cell, not the 14.997th cell
Doubles may be inaccurate
representing “ints”
In mathematics 3 • 15
• (1/3) = 15
But, 3.0 * 15.0 * (1.0 / 3.0) might be 14.9999997
Last, and least
operations with doubles is slower on some
computers
doubles often require more
memory
Order of Evaluation:
¨Precedence determines the order of evaluation
of operators.
of operators.
¨Is a + b * a - b equal to ( a + b ) * ( a - b )
or
a + ( b * a ) - b ??
And does it matter?
a + ( b * a ) - b ??
And does it matter?
¨Try this:
¨ 4 + 3 * 2 - 1
(4 + 3) * (2 -
1) = 7
4 + (3 * 2) - 1 = 9
Operator Precedence Rules:
¡1. do ( )’s
first, starting with innermost
¡2. then do
unary minus (negation): -
¡3. then do “multiplicative” ops: *, /, %
¡4. lastly do “additive” ops: binary +, -
Precedence Isn’t Enough:
¨Precedence doesn’t
help if all the operators have the same precedence
¨Is a / b * c
equal to
¨ a / ( b * c )
or ( a / b ) * c ??
¨Associativity determines the order
among
consecutive operators of equal precedence
consecutive operators of equal precedence
¨Does it matter? Try this: 15 / 4 * 2
Associativity Matters:
¨Associativity determines the order
among
consecutive operators of equal precedence
consecutive operators of equal precedence
¨Does it matter? Try this: 15 / 4 * 2
¨ (15
/ 4) * 2 = 3 * 2 = 6
¨ 15
/ (4 * 2) = 15 / 8 = 1
Associativity Rules:
¨Most C# arithmetic operators are “left associative”, within the same
precedence level
¡a / b * c equals (a / b) * c
¡a + b - c + d
equals ( ( a + b ) - c ) + d
¨
¨C# also has a few operators
that are right associative.
Precedence and
Associativity: Example
¨Mathematical
formula:
- b + square root ( b2 - 4 a c)
----------------------
2 a
- b + square root ( b2 - 4 a c)
----------------------
2 a
¨C# formula:
¨(-
b + sqrt ( b * b - 4.0 * a * c) ) / ( 2.0 * a )
//////////////////END OF THIS SESSION///////////////
//////////////////WORKING IN PROCESS//////////////
//////////////////KEEP VISITING THANK YOU/////////
No comments:
Post a Comment