Unformatted text preview:

Overview CSE 142 Computer Programming I Arithmetic expressions Integer and floating point double types Unary and binary operators Precedence Associativity Conversions and casts Symbolic constants Arithmetic Expressions 2000 UW CSE D 1 D 2 Assignment Statement Review Why Study Expressions double area radius We need precise rules that define exactly what an expression means What is the value of 4 4 4 4 area 3 14 radius radius assignment statement Arithmetic on a computer may differ from everyday arithmetic or math expression Execution of an assignment statement Evaluate the expression on the right hand side Store the value of the expression into the variable named on the left hand side 1 0 9 0 9 0 could be 0 99999998213 2 3 is zero in C not 667 Reading Text sec 2 5 D 3 D 4 Expressions Expression Evaluation Expressions are things that have values A variable by itself is an expression radius A constant by itself is an expression 3 14 Often expressions are combinations of variables constants and operators area 3 14 radius radius Some terminology Data or operand means the integer or floatingpoint constants and or variables in the expression Operators are things like addition multiplication etc The value of an expression will depend on the data types and values and on the operators used D 5 Additionally the final result of an assignment statement will depend on the type of the assignment variable D 6 D 1 Arithmetic Types Review Operator Jargon C provides two different kinds of numeric values Integers 0 12 17 142 Type int Values are exact Constants have no decimal point or exponent Floating point numbers 3 14 6 023e23 Type double Values are approximate 12 14 digits precision typical Constants must have decimal point and or exponent Binary operates on two operands 3 0 b zebra giraffe Unary operates on one operand 23 4 C operators are unary or binary Puzzle what about expressions like a b c Answer this is two binary ops in sequence D 7 D 8 Example Expressions with doubles Expressions with doubles Constants of type double 0 0 3 14 2 1 5 0 6 02e23 1 0e 3 not 0 or 17 Operators on doubles unary binary Note no exponentiation operator in C Declarations double height base radius x c1 c2 Sample expressions not statements 0 5 height base 4 0 3 0 3 14 radius radius radius 3 0 c1 x c2 x x D 9 D 10 Expressions with ints int Division and Remainder Constants of type int Integer operators include integer division and integer remainder symbols and 0 1 17 42 not 0 0 or 1e3 Operators on ints unary binary Caution division looks like an old friend but there is a new wrinkle 2 rem 99 1 100 299 200 D 11 99 D 12 D 2 Expressions with ints Time Example int Division and Remainder is integer division no remainder no rounding 299 100 2 6 4 1 5 6 0 is mod or remainder 299 100 99 6 4 2 5 6 5 Given total minutes 359 Find hours 5 minutes 59 Solution in C hours total minutes 60 minutes total minutes 60 D 13 D 14 Why Use ints Why Not doubles Always A Cautionary Example Sometimes only ints make sense int radius double volume double pi 3 141596 volume 4 3 pi radius radius radius the 15th spreadsheet cell not the 14 997th cell Doubles may be inaccurate representing ints In mathematics 3 15 1 3 15 But 3 0 15 0 1 0 3 0 might be 14 9999997 Last and least operations with doubles is slower on some computers doubles often require more memory D 15 Order of Evaluation D 16 Operator Precedence Rules Precedence determines the order of evaluation of operators Is a b a b equal to a b a b or a b a b And does it matter Try this 4 3 2 1 Precedence rules 1 do s first starting with innermost 2 then do unary minus negation 3 then do multiplicative ops 4 lastly do additive ops binary 4 3 2 1 7 4 3 2 1 9 D 17 D 18 D 3 Precedence Isn t Enough Associativity Matters Precedence doesn t help if all the operators have the same precedence Associativity determines the order among consecutive operators of equal precedence Is a b c equal to Does it matter Try this 15 4 2 a b c or a b c 15 4 2 3 2 6 Associativity determines the order among consecutive operators of equal precedence 15 4 2 15 8 1 Does it matter Try this 15 4 2 D 19 D 20 Associativity Rules The Full Story Most C arithmetic operators are left associative within the same precedence level C has about 50 operators 18 precedence levels A Precedence Table shows all the operators their precedence and associativity Look on inside front cover of our textbook Look in any C reference manual When in doubt check the table When faced with an unknown operator check the table a b c equals a b c a b c d equals a b c d C also has a few operators that are right associative D 21 D 22 b 2 5 a 1 0 Depicting Expressions Precedence and Associativity Example b 2 5 Mathematical formula b b2 4 a c 2a b 4 0 a c 15 2 1 0 2 5 6 25 4 0 c 15 2 60 8 C formula b sqrt b b 4 0 a c 2 0 a D 23 67 05 D 24 D 4 Mixed Type Expressions Conversions in Assignments What is 2 3 14 Compiler will implicitly automatically convert int to double when they occur together int total count value int double total 97 double avg double double likewise 2 3 3 14 2 3 3 14 6 3 14 6 0 3 14 18 84 2 3 3 14 2 3 3 14 0 3 14 0 0 3 14 0 0 We strongly recommend you avoid mixed types e g use 2 0 3 0 3 14 instead D 25 Explicit Conversions count 10 avg total count avg is 9 0 value total 2 2 bad news implicit conversion to double implicit conversion to int drops D 26 fraction with no warning Using Casts Use a cast to explicitly convert the result of an expression to a different type Format type expression Examples double myage int balance deposit This does not change the rules for evaluating the expression itself types etc Good style because it shows the reader that the conversion was intentional not an accident int total count double avg total 97 count 10 explicit conversion to double right way avg double total double count avg is 9 7 explicit conversion to double wrong way avg double total count avg is 9 0 D 27 define Symbolic Constants Expressions in define Named constants define PI D 28 define PI 3 14159265 define HEIGHT 50 define WIDTH 80 define AREA HEIGHT WIDTH circle area PI radius radius volume length AREA 3 14159265 circle …


View Full Document

UW CSE 142 - Study Notes

Loading Unlocking...
Login

Join to view Study Notes 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 Study Notes 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?