CSE142 Outline Computer Programming I • Expressions overview • Operators & Operands Expressions • Precedence & Associativity • Type conversion Or… a (r(o(s)))(e) with any other • #define parenthesization would smell as sweet (assuming spelling is associative). • The Way D-1 D-2 4/1/01 4/1/01 Assignment Statement: Review Expressions double area, radius; Expressions are things that have values area = 3.14 * radius * radius; – A variable by itself is an expression: radius – A constant by itself is an expression: 3.14 expression assignment statement Often expressions are combinations of Execution of an assignment statement: variables, constants, and operators. 1. Find value of expression on the right – area = 3.14 * radius * radius; 2. Store the expression’s value into the variable named on the left hand side D-3 D-4 4/1/01 4/1/01 What’s hard about expressions? What are expressions? The programmer’s view 4 + 3 * 2 - 1 variables numbers a 5 operations on numbers What does this mean? 3 + 7 (4 + 3) * (2 - 1) 7 sequences of operations on numbers and variables ((4 + 3) * 2) - 1 13 4 + (3*2) - 1 9 4 * a / 6.0 + 12 Which of these is Right? None of them is inherently correct. seqs. of ops. on numbers and variables and functions (oh my!) Which of these is right? In C and mathematics, the third is. 1 + pow(population, 1.0 / 3.0) D-5 D-6 4/1/01 4/1/01 1
What’s hard about expressions? Expression Evaluation The computer’s view result = 4 + 3 * 2 – 1; Some terminology: – Operators are things like addition and multiplication. How must we say this to the computer? – Operands (or data) are the things the operators work on: variables, real and integer constants, etc. result = 3 * 2; – The value of an expression will depend on the data result = 4 + result; types, the values, and the operators used. result = result – 1; The computer does all its calculations and – Additionally, the final result of an assignment statement will depend on the type of the assignment variable . operations on a pair of numbers (or just one). D-7 D-8 4/1/01 4/1/01 Arithmetic Types: Review Operator Jargon C provides two kinds of numeric values • Binary: operates on two operands – Integers ( 0 , 12 , -17 , 142 ) 3.0 * b zebra + giraffe • Type int • Unary: operates on one operand • Values are exact - 23.4 • Constants have no decimal point or exponent • C operators are unary or binary – Floating-point numbers ( 3.14 , -6.023e23 ) • Type double • Puzzle: what about expressions like a+b+c? • Values are approximate (~12-14 digits precision) This expression has two binary operators, • Constants must have decimal point and/or exponent executed one after the other: (a+b)+c D-9 D-10 4/1/01 4/1/01 Expressions with doubles Some Expressions w/Doubles Declarations: Constants of type double: double height = 10.0, base = 2.5; – 0.0 , 3.14 , -2.1 , 5.0 , 6.02e23 , 1.0e-3 double radius = 0.2; – not 0 or 17 double x = 2.0, coeff1 = 8.0, coeff2 = 0.0; Operators on doubles: – unary: - Sample expressions (not statements): – binary: + - * / 0.5 * height * base ( 4.0 / 3.0 ) * 3.14 * radius * radius * radius - 3.0 + coeff1 * x - coeff2 * x * x Note: there’s no exponentiation operator in C! D-11 D-12 4/1/01 4/1/01 2
Expressions with ints int Division and Remainder Constants of type int: Integer operators include: – 0 , 1 , -17 , 42 – integer division written as ‘ / ’ – not 0.0 or 1e3 – integer remainder written as ‘ % ’ Operators on ints: Caution! Division is an old friend, but it’s a really old friend…remember long division? – unary: - – binary: + - * / % 2 rem 99 100 299 -200 99 D-13 D-14 4/1/01 4/1/01 Expressions with int s: int Division and Remainder Time Example / is integer division: no remainder, no rounding Given: total_minutes 359 299 / 100 2 Find: hours 5 1 6 / 4 minutes 59 0 5 / 6 % is mod or remainder: Solution in C: 299 % 100 99 hours = total_minutes / 60 ; 6 % 4 2 minutes = total_minutes % 60 ; 5 % 6 5 D-15 D-16 4/1/01 4/1/01 Why Use ints? Why A Cautionary Example Not doubles Always? int radius; Sometimes only ints make sense – the 15th spreadsheet cell, not the 14.997th cell double volume; Doubles may be inaccurate representing “ints” double pi = 3.14159635; – In mathematics 3 * 15 * (1/3) = 15 – But, 3.0 * 15.0 * (1.0 / 3.0) might be 14.9999997 – Then again, with ints: 3 * 15 * (1/3) = 0 volume = (4/3) * pi * radius * radius * Other (lesser) reasons also exist: radius; – Operations on doubles are slower on some computers. – Doubles often require more memory. Danger, Will Robinson: – “double” requires more keystrokes than “int” 4/3 is 1! – etc. D-17 D-18 4/1/01 4/1/01 3
Order of Evaluation Operator Precedence Rules Precedence determines the order of evaluation Precedence rules: of operators. 1. do ( )’s first, starting with innermost Remember 4 + 3 * 2 - 1 ? Which is it equal to? 2. then do unary minus (negation): - – ( 4 + 3 ) * ( 2 - 1 ) 3. then do “multiplicative” ops: *, /, % – 4 + ( 3 * 2 ) – 1 4. lastly do “additive” ops: binary +, - * has higher precedence than + or – . So, it gets to go first! Is there a way to overcome precedence? Sure! Use parentheses: (4+3) * (2-1) is 7 . D-19 D-20 4/1/01 4/1/01 Precedence Isn’t Enough Associativity Rules Remember a + b + c ? Precedence is no help! Most C operators are left associative, within How about: a / b * c / d ? Is it equal to: the same precedence level: – ( ( a / b ) * c ) / d or – a / b * c equals (a / b) * c – ( a / b ) * ( c / d ) or – a + b - c + d equals ( ( a + b ) - c ) + d – something else entirely? Associativity determines the order among But… C has a few operators that are right consecutive operators of equal precedence associative. Does it matter? Try this: 15 / 4 * 2 D-21 D-22 4/1/01 4/1/01 The Bottom Line Functions C has about 50 operators & 18 precedence levels… C includes functions for additional calculations that are not available using operators like +, -, *, /, etc. A "Precedence Table" shows all the operators, their root2 = sqrt(2.0); precedence and associativity. x = 2.1 * sin(theta/1.5) + 17.0; – Look on inside front cover of our textbook Functions can be used in expressions just like – Look in any C reference manual constants or variables. When in doubt you can do two things: We’ll find out how to create new functions a bit later – check the table in the course!! – use parentheses Which should you really do? D-23 D-24 4/1/01 4/1/01 4
Precedence and Function Libraries - #include Associativity: Example Standard C functions are organized into libraries. Mathematical formula: To use a library function, specify the library that contains it (using #include ) at the top of the program. 2 − b b 4 ac − + Look in the textbook (appendix C) or a C manual for lists 2 a of available libraries and functions. #include <math.h> C formula: The <math.h> library int main(void) { (- b + sqrt ( b * b - 4.0 * a * c) ) / ( 2.0 * a ) contains sqrt, sin, … cos, tan, etc. But this is bad… why? root2 = sqrt(2.0); D-25 D-26 … 4/1/01 4/1/01 b = 2.5; Precedence and a = -1.0; Depicting Expressions c = 15.2; Associativity: Example Mathematical formula: b b 4.0 a c 2 − b b 4 ac − + 2.5 2.5 4.0 -1.0 15.2 2 a * * -4.0 C statements: * 6.25 discriminant = b*b – 4.0 * a * c; -60.8 root = (-b + sqrt(discriminant)) / (2.0 * a); - 67.05 D-27 D-28 4/1/01 4/1/01 Choose Your Own Adventure int Multiplication 2 * 3.14 2 * 3.14 What happens when an integer meets a double? You decide… Heading north, you realize that you’ve lost something important to you. It’s your .14 ! What happened to it? If you choose “int multiplication”, go forward one slide . If we try to use integer multiplication, we’ll have to make If you choose “double multiplication”, go forward two slides. 3.14 an integer. When we do that, we lose data! If you choose “syntax error”, go forward three slides. Otherwise, go forward four slides. D-29 D-30 4/1/01 4/1/01 5
Recommend
More recommend