Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.

Similar presentations


Presentation on theme: "Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail."— Presentation transcript:

1 Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail

2 Lecture 072 Arithmetic Operators Increment and decrement operators x = x + 1; Or ++x; Or x++; (Prefix and postfix) x = x – 1; Or --x; Or x--; Subtleties Example: int x=5, y=5; cout << x++ << endl; cout << ++y << endl; cout << x << ‘,’ << y; x = 10; y = ++x; x = 10; y = x++;

3 Lecture 073 More 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;

4 Lecture 074 Operator Precedence & Associativity Precedence: If two operators apply to same operand, higher precedence operator applied first e.g. (3 + 5 * 6) has value 33 Associativity: If two operators have same precedence, associate L-R or R-L as defined e.g. (120 / 6 * 5) has value 100 as associate from L-R

5 Lecture 075 Evaluation Order of Operators

6 Lecture 076 Type Conversions in Expressions Operators, variables and constants form expressions e.g. a = b + 2; bool, char, unsigned char, signed char, and short automatically converted to int In an expression with mixed data types, smaller is promoted to larger one e.g. 9.0/5 has 5 promoted to double status

7 Lecture 077 Type Conversions on Assignment Type converted to that of the receiving variable float tree = 3; //int to float int guess = 3.9832; //float to int int debt = 3.0E12; //erroneous What output if we cout tree, guess and debt ? Potential problems: Loss of precision, or loss of fractional part, or out of range. Compiler complains usually

8 Lecture 078 Casting A programmer forces an expression to be of the specified type (as opposed to automatic conversions) (type) expression or type (expression) e.g. (float) 5/2; char(77); long (shorty); int (‘Z’);

9 Lecture 079 Relational & Logical Operators Relational for comparison and Logical for connecting true and false values Outcome of a relational or logical expression is bool (true or false) For relational operators, operands and expressions of any data type that can be compared Any non-zero value converted to true, zero to false

10 Lecture 0710 Relational & Logical Operators Fill table of (p AND q), (p OR q), (NOT p). Also for 3 variables. Precedence lower than arithmetic operators #include int main() { int p, q; cout << "Enter P (0 or 1): "; cin >> p; cout << "Enter Q (0 or 1): "; cin >> q; cout << "P AND Q: " << (p && q) << endl; cout << "P OR Q: " << (p || q) << endl; return 0; }

11 Lecture 0711 Relational & Logical Operators #include int main() { int p=5, q=2; bool bl=(p<=q); cout<<bl<<endl; bl=!bl; cout<<bl<<endl; return 0; } (score >= 0) && (score <= 10) –is true if score is between 0 and 10 (including 0 and 10) (score 10) –is true exactly in the cases where the previous condition would be false ! ( (score >= 0) && (score <= 10) )

12 Lecture 0712 Relational & Logical Operators Pitfall: Do not use strings of inequalities. They may be legal C++ code but will not do what you (for now) expect them to do if (x < y < z) // WRONG! cout << "y is between x and z“ << endl; Instead, you have to write: if ((x < y) && (y < z)) // CORRECT! cout << "y is between x and z" << endl;

13 Lecture 0713 sizeof operator sizeof operator to test sizes e.g. cout << sizeof(int); cout << sizeof i; //i is a variable


Download ppt "Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail."

Similar presentations


Ads by Google