friday, december 08, 2006 “experience is something you don't get until just after you need...
Post on 20-Dec-2015
214 views
TRANSCRIPT
Friday, December 08, 2006
“Experience is something you don't get until just after you
need it.”
- Olivier
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 ; */
int big=980000;
short small;
cout<<"big="<<big<<endl;
small=big; //don't do this
cout<<"small="<<small<<endl;
int big=980000;short small;cout<<"big="<<big<<endl;small=big; //don't do thiscout<<"small="<<small<<endl;
big=980000small=-3040
Character 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
Arithmetic Operators
Addition + Subtraction - Multiplication * Division / Modulus %
Modulus returns remainder of division between two integers
% cannot be used on float or double
Arithmetic Operators
Division between two integers results in an integer.
The result is truncated, not rounded
Priority of Operators1 Parentheses Inner most first
2 Unary operators Right to left(+ -)
3 Binary operatorsLeft to right(* / %)
4 Binary operatorsLeft to right(+ -)
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)
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
Assignment Operators
= assignment operator Compound Assignment Operators
operator example equivalent statement += x+=2; x=x+2;
-= x-=2; x=x-2;
*= x*=y; x=x*y;
/= x/=y; x=x/y;
Arithmetic Operators
unary operators• auto increment ++
– post increment x++;– pre increment ++x;
• auto decrement - -– post decrement x- -;– pre decrement - -x;
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
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