DOC PREVIEW
UMD CMSC 131 - Lecture 7: Evaluation Order

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 7 Evaluation Order Last time 1 Project assigned 2 Named constants in Java 3 More on if 4 Loops Today 1 Project 2 More assignment operators 3 Precedence and short circuiting 9 15 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Get 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 automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Java 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 2006 Rance Cleaveland 2006 University of Maryland 2 Java Keywords abstract continue for new switch assert default goto package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const float native super while CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Expressions Java phrases that yield values e g x x 1 y x y z 0 foo equals cat Expressions have values int boolean etc Expressions can be assigned to variables appear inside other expressions etc CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Expressions and Side Effects Some expressions can also alter the values of variables e 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 effects CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Are 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 x CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 Other Expressions with Side Effects Java includes abbreviations for common forms of assignment Example increment operations x Pre increment Equivalent to x x 1 x Post increment Increments x returns old value of x Difference x x x x always true never true CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 Other Assignment Operators Decrement x x Pre decrement Equivalent to x x 1 Post decrement Decrements x returns old value General modification by constant General form var op constant Examples x 2 equivalent to x x 2 x 2 equivalent to x x 2 x 2 equivalent to x x 2 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 Expressions and Statements turns expressions into statements x 1 x 1 expression statement Any expression No Expression with side effects e g x Method calls e g System out println foo Object creation routines later in class CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Precedence 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 2006 Rance Cleaveland 2006 University of Maryland 10 Java Precedence Rules parentheses unary ops multiply divide add subtract comparisons equality logical and logical or assignments x x x x x x x etc increasing precedence CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Examples x x Equivalent to x x x y z Equivalent to x y z x y y z w z Equivalent 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 11 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 Should You Rely on Precedence No The only ones people can remember are My Dear Aunt Sally and parentheses Bad 2 x 5 z 3 w x 2 Better 2 x 5 z 3 w x 2 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 13 Short 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 determined CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 14 Examples 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 0 1 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 15 Examples cont What does Java print int x 0 y 0 while x 4 y x System out println y 15 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 16 Programming with Side Effects Generally Side effects in conditions are hard to understand Good programming practice Conditions should be side effect free Side effects should be in stand alone statements CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 17


View Full Document

UMD CMSC 131 - Lecture 7: Evaluation Order

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 7: Evaluation Order
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 7: Evaluation Order and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7: Evaluation Order and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?