04operatorsSmall

04operatorsSmall - Lecture Set 4: Evaluation Order Today:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) Lecture Set 4: Evaluation Order Today: c More assignment operators c Precedence and short- circuiting CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 1 Expressions c Java “phrases” that yield values e.g. x x + 1 - y foo.equals (“cat”) c Expressions have values (int, boolean, etc.) c Expressions can be assigned to variables, appear inside other expressions, etc. CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 2 Expressions and Side Effects c Some expressions can also alter the values of variables e.g. x=1 c x=1 is an expression? c Yes! c Value is result of evaluation right-hand side of = c It also alters the value of x c Such alterations are called side effects
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 3 Are the Following Legal? c int x, y; x = y = 1; Yes. Result assigns 1 to x and to y c int x = 0, y = 1; boolean b = false; if (b = (x <= y)){ x = y; } Yes. Result assigns true to b and 1 to x CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 4 Other Expressions with Side Effects c Java includes abbreviations for common forms of assignment c Example: increment operations (Basically equivalent to x = x + 1 ++ x “Pre-increment” Increments x, returns the new value of x x++ “Post-increment” In crements x, returns the old value of x c Same or Different c x == x++ c x == ++x c Compare c x++ * y++ c ++x * ++y c ++x * y++ c x++ * ++y always true never true CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

04operatorsSmall - Lecture Set 4: Evaluation Order Today:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online