t04BVariablesAndExpressionsAssignment

# t04BVariablesAndExpressionsAssignment - Variable...

This preview shows pages 1–4. Sign up to view the full content.

Dale Roberts Variable Declarations, Data types, Expressions - Assignments

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Dale Roberts Assignment Operators Assignment Operators Syntax: Syntax: var = expression; Assign the value of expression to variable ( var ) Example : int x, y, z; x = 5; y = 7; z = x + y; int x, y, z; x = y = z = 0; int x = y = z = 0; int i, j; float f, g; i = f = 2.5; g = j = 3.5; i = 2; f = 2.5; g = 3.0; j = 3; Z = (x = 5) + (y = 7) much faster same as x = (y = (z = 0)); wrong ! int x = 0, y = 0, z = 0;
Dale Roberts Short Hand Assignment Short Hand Assignment Syntax Syntax f = f op g f = f op g can be rewritten to be f op= f op= g g such as: a = a + 2 a += 2, a = a - 2 a -= 2, a = a * 2 a *= 2, a = a / 2 a /= 2, a = a % 2 a %= 2, a = a << 2 a <<= 2, a = a & 2 a &= 2, a = a | 2 a |= 2, a = a ^ 2 a ^= 2 No blanks between op and = x *= y + 1 is actually x = x * (y+1) rather than x = x * y + 1 Example : q = q / (q+2) q /= q+2 j = j << 2 j <<= 2 Advantage: help compiler to produce more efficient code More complicated examples : int a=1, b=2, c=3, x=4, y=5; a += b += c *= x + y - 6; printf(“%d %d %d %d\n”,a,b,c,x,y); a += 5 + b += c += 2 + x + y;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern