9/15/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 7:Evaluation OrderLast time:1. Project assigned2. Named constants in Java3. More on if4. LoopsToday:1. Project2. More assignment operators3. Precedence and short-circuitingCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Get Started on Project #1 The assignment is on the CMSC 131 web-site (click “Projects” link). It is due Tuesday, 9/19 at 11 pm The project is open Start now! Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly, as much of grading is automatedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Java Variable Names Last time: strategies for naming variables / classes / constants What are the legal names (= identifiers) in Java? First character must be letter, _, $ Second and subsequent characters may be letter, _, $ or digit Identifiers are case-sensitive: A != a No keywords! Another convention: Avoid variable names that differ only on case (i.e. don’t use foo, fOO)CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3Java KeywordsnativelonginterfaceintinstanceofimportimplementsifgotoforwhilesuperfloatconstvolatilestrictfpfinallyclassvoidstaticfinalchartryshortextendscatchtransientreturnenumcasethrowspublicelsebytethrowprotecteddoublebreakthisprivatedobooleansynchronizedpackagedefaultassertswitchnewcontinueabstractCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Expressions Java “phrases” that yield valuese.g.xx + 1 - yx == y && z == 0foo.equals (“cat”) Expressions have values (int, boolean, etc.) Expressions can be assigned to variables, appear inside other expressions, etc.CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5Expressions and Side Effects Some expressions can also alter the values of variablese.g. x=1 x=1 is an expression? Yes! Value is result of evaluation right-hand side of = It also alters the value of x Such alterations are called side effectsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6Are the Following Legal? int x, y;x = y = 1;Yes. Result assigns 1 to x, y int x = 0, y = 1;boolean b;if (b = (x <= y)) x = y;Yes. Result assigns true to b, 1 to xCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7Other Expressions with Side EffectsJava includes abbreviations for common forms of assignment Example: increment operations++x “Pre-increment”Equivalent to x = x+1x++ “Post-increment”Increments x, returns old value of x Difference x == x++ x == ++xalways truenever trueCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Other Assignment Operators Decrement--x “Pre-decrement”Equivalent to x = x – 1x-- “Post-decrement”Decrements x, returns old value General modification by constant General form: <var> <op> = <constant> Examplesx += 2 equivalent to x = x+2x *= 2 equivalent to x = x*2x /= 2 equivalent to x = x/2CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Expressions and Statements ; turns expressions into statements x = 1 expression x = 1; statement Any expression? No Expression with side effectse.g. x++; Method callse.g. System.out.println (foo); Object-creation routines (later in class)CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Precedence Explains how to evaluate expressions What is value of 1 – 2 + 3 * 4? Precedence rules answer this question Higher-precedence operators evaluated first Example from math: “My Dear Aunt Sally”Multiple and divide (higher precedence) before you add and subtract (lower precedence) Java follows “My Dear Aunt Sally” … but what about other operators?CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Java Precedence Rules parentheses: ( ) unary ops: +x -x ++x –-x x++ x-- !x multiply/divide: * / % add/subtract: + - comparisons: < > <= >= equality: == != logical and: && logical or: || assignments: = += *= /= %= etc.increasing precedenceCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Examples x++ + --xEquivalent to (x++) + (--x) x * y + -zEquivalent to (x*y) + (-z) x <= y && y <= z || w > zEquivalent to ((x <= y) && (y <= z)) || (w > z) What is value of 1 – 2 + 3 * 4?1 -2 + 3 * 4= (1-2) + (3*4)= -1 + 12= 11CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland13Should You Rely on Precedence?No! The only ones people can remember are “My Dear Aunt Sally” (and parentheses) Bad2 * x ++ < 5 * z + 3 && -w != x / 2 Better(2 * (x ++) < (5 * z + 3)) && ((-w) != (x / 2))CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland14Short-circuiting What does Java print?int x = 0, y = 1;if ((y > 1) && (++x == 0))--y;System.out.println (x); 0 Why? y > 1 is false The result of && will be false, regardless of second expression Java therefore does not evaluate second expression of && This treatment of &&, || is called short-circuiting Subexpressions evaluated from left to right Evaluation stops when value of over-all expression is determinedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland15Examples What does Java print?int x = 0, y = 1;if ((y >= 1) && (++x == 0))--y;System.out.println (x); 1 What does Java print?int x = 0, y = 1;if ( ((y > 1) && (++x == 0))||((y == 1) && (x++ == 0)) )--y;System.out.println (y);System.out.println (x); 01CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland16Examples (cont.) What does Java print?int x = 0, y = 0;while (x++ <= 4)y += x;System.out.println (y); 15CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland17Programming with Side-EffectsGenerally: Side effects in conditions are hard to understand Good programming practice Conditions should be side-effect-free Side effects should be in “stand-alone
View Full Document