Title: Friday, December 08, 2006
1Friday, December 08, 2006
- Experience is something you don't get until just
after you need it. - - Olivier
2 3Declarations and Initialization
double dx dx3.5 OR double dx3.5
4Declarations and Initialization
//(comma separated) double a, b, c5.6, d
double r1, r2, r3 //(white spaces
ignored) /All declarations and statements must
end with semicolon /
5- int big980000
- short small
- coutltlt"big"ltltbigltltendl
- smallbig //don't do this
- coutltlt"small"ltltsmallltltendl
6- int big980000
- short small
- coutltlt"big"ltltbigltltendl
- smallbig //don't do this
- coutltlt"small"ltltsmallltltendl
- big980000
- small-3040
7- short small 980000
- cout ltlt small ltlt endl
- -3040
8Character Data
- Each character corresponds to a binary code
- Most commonly use binary codes are ASCII
(American Standard Code for Information
Interchange) - Character ASCII Code Integer Equivalent
- 0100101 37
- 3 0110011 51
- A 1000001 65
- a 1100001 97
- b 1100010 98
- c 1100011 99
9Arithmetic Operators
- Addition
- Subtraction -
- Multiplication
- Division /
- Modulus
10Arithmetic Operators
- Addition
- Subtraction -
- Multiplication
- Division /
- Modulus
- Modulus returns remainder of division between two
integers - cannot be used on float or double
11- Example
- 5 2 evaluates to ?
- 10 2 evaluates to ?
12- Example
- 5 2 evaluates to 1
- 10 2 evaluates to 0
13Arithmetic Operators
- Division between two integers results in an
integer. - The result is truncated, not rounded
14Modulo operation
- 17 / 5 evaluates to
- 17 5 evaluates to
15Modulo operation
- 17 / 5 evaluates to 3.
- 17 5 evaluates to 2.
16- Example
- 5/3 evaluates to ?
- 3/6 evaluates to ?
17- Example
- 5/3 evaluates to 1
- 3/6 evaluates to 0
18Priority of Operators
- Parentheses Inner most first
- Unary operators Right to left
- ( -)
- Binary operators Left to right
- ( / )
- Binary operators Left to right
- ( -)
19Precedence
- Order of mathematical operations is
important.Examples - (32)4 54 20
- 3(24) 3 8 11
- and / evaluated before and -Example 324
evaluated as 3(24)
20Precedence
- If precedence is equal, then evaluate from left
to right.Examples - 325 5 5 10
- 32/5 6/5 1
- Parentheses enforce evaluation orderExample
(32)4 54 20
21Unary operators , -
- Unary operators , -
- -3, 17 allowed
- Example 4(-3) -12
22Assignment Operators
assignment operator Compound Assignment
Operators operator example equivalent statement
x2 xx2 - x-2 xx-2 xy x
xy / x/y xx/y
23Example x 4 x 5
24Example x 4 x 5 // x 20
25Arithmetic Operators
- unary operators
- auto increment
- post increment x
- pre increment x
- auto decrement - -
- post decrement x- -
- pre decrement - -x
26- x is executed after it is used
- x 7
- y x
- x is also equivalent to x x1
- x executed before it is used
- x 7
- y x
- --x is equivalent to x x-1
- -- works just like , but with subtraction
instead of addition
27- x is executed after it is used
- x 7
- y x // x 8, y 7
- x is also equivalent to x x1
- x executed before it is used
- x 7
- y x // x 8, y 8
- --x is equivalent to x x-1
- -- works just like , but with subtraction
instead of addition
28Shortcuts
- n equivalent to n n 1read as "add 1 to n"
- n-- equivalent to n n - 1read as "subtract 1
from n" - s n equivalent to s s n read as "add n
to s" - s - n equivalent to s s - nread as "subtract
n from s