Download presentation
Presentation is loading. Please wait.
1
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier
2
§Numeric data types
3
Declarations and Initialization double dx ; dx=3.5 ; OR double dx=3.5 ;
4
Declarations and Initialization //(comma separated) double a, b, c=5.6, d ; double r1, r2, r3; //(white spaces ignored) /*All declarations and statements must end with semicolon ; */
5
int big=980000; short small; cout<<"big="<<big<<endl; small=big; //don't do this cout<<"small="<<small<<endl;
6
int big=980000; short small; cout<<"big="<<big<<endl; small=big; //don't do this cout<<"small="<<small<<endl; big=980000 small=-3040
7
short small = 980000; cout << small << endl; -3040
8
Character Data l Each character corresponds to a binary code l Most commonly use binary codes are ASCII (American Standard Code for Information Interchange) CharacterASCII CodeInteger Equivalent %010010137 3011001151 A100000165 a110000197 b110001098 c110001199
9
Arithmetic Operators §Addition+ §Subtraction- §Multiplication* §Division/ §Modulus%
10
Arithmetic Operators §Addition+ §Subtraction- §Multiplication* §Division/ §Modulus% l Modulus returns remainder of division between two integers l % cannot be used on float or double
11
l Example 5 % 2 evaluates to ? 10 % 2 evaluates to ?
12
l Example 5 % 2 evaluates to 1 10 % 2 evaluates to 0
13
Arithmetic Operators §Division between two integers results in an integer. §The result is truncated, not rounded
14
Modulo operation 17 / 5 evaluates to 17 % 5 evaluates to
15
Modulo 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
18
Priority of Operators 1ParenthesesInner most first 2Unary operatorsRight to left (+ -) 3Binary operatorsLeft to right (* / %) 4Binary operatorsLeft to right (+ -)
19
Precedence §Order of mathematical operations is important. Examples: (3+2)*4 = 5*4 = 20 3+(2*4) = 3 + 8 = 11 * and / evaluated before + and - Example: 3+2*4 evaluated as 3+(2*4)
20
Precedence If precedence is equal, then evaluate from left to right. Examples: 3+2+5 = 5 + 5 = 10 3*2/5 = 6/5 = 1 Parentheses enforce evaluation order Example: (3+2)*4 = 5*4 = 20
21
Unary operators: +, - §Unary operators: +, - -3, +17 allowed Example: 4*(-3) = -12
22
Assignment Operators =assignment operator Compound Assignment Operators operatorexampleequivalent statement +=x+=2; x=x+2; -=x-=2;x=x-2; *=x*=y;x=x*y; /=x/=y;x=x/y;
23
Example x = 4; x *= 5;
24
Example x = 4; x *= 5; // x = 20
25
Arithmetic 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 = x+1 ++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 = x+1 ++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
28
Shortcuts n++ equivalent to n = n + 1 read as "add 1 to n" n-- equivalent to n = n - 1 read as "subtract 1 from n" s += n equivalent to s = s + n read as "add n to s" s -= n equivalent to s = s - n read as "subtract n from s
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.