DOC PREVIEW
UW CSE 142 - Study Notes

This preview shows page 1-2 out of 6 pages.

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

Unformatted text preview:

D-1D-1CSE 142Computer Programming IArithmetic Expressions© 2000 UW CSED-2Arithmetic expressionsInteger and floating-point (double) typesUnary and binary operatorsPrecedenceAssociativityConversions and castsSymbolic constantsReading: Text sec. 2.5.OverviewD-3We need precise rules that define exactly what an expression means:What is the value of 4 - 4 * 4 + 4?Arithmetic on a computer may differ from everyday arithmetic or math:(1.0 / 9.0) * 9.0 could be 0.999999982132 / 3 is zero in C, not .667 (!)Why Study Expressions?D-4double area, radius; area = 3.14 * radius * radius;assignment statementexpressionExecution of an assignment statement:Evaluate the expression on the right hand sideStore the value of the expression into the variable named on the left hand sideAssignment Statement: ReviewD-5Expressions are things that have valuesA variable by itself is an expression: radiusA constant by itself is an expression: 3.14Often expressions are combinations of variables, constants, and operators.area = 3.14 * radius * radius;ExpressionsD-6Expression EvaluationSome terminology:Data or operand means the integer or floating-point 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 usedAdditionally, the final result of an assignment statement will depend on the type of the assignment variable.D-2D-7Arithmetic Types: ReviewC provides two different kinds of numeric valuesIntegers (0, 12, -17, 142)Type intValues are exactConstants have no decimal point or exponentFloating-point numbers (3.14, -6.023e23)Type doubleValues are approximate (12-14 digits precision typical)Constants must have decimal point and/or exponentD-8Operator JargonBinary: operates on two operands3.0 * bzebra + giraffeUnary: operates on one operand-23.4C operators are unary or binaryPuzzle: what about expressions likea+b+c?Answer: this is two binary ops, in sequenceD-9Constants of type double:0.0, 3.14, -2.1, 5.0, 6.02e23, 1.0e-3not 0 or 17Operators on doubles:unary: -binary: +, -, *, /Note: no exponentiation operator in CExpressions with doublesD-10Declarationsdouble 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 * xExample Expressions with doubles D-11Constants of type int:0, 1, -17, 42 not 0.0 or 1e3Operators on ints:unary: -binary: +, -, *, /, %Expressions with intsD-12Integer operators include integer division and integer remainder: symbols / and %Caution: division looks like an old friend, but there is a new wrinkle!int Division and Remainder2 rem 1 100 )299 200992 rem 99 100 )299 20099D-3D-13/ is integer division: no remainder, no rounding299 / 1006 / 45 / 6int Division and Remainder% is mod or remainder:299 % 100 99 6 % 4 2 5 % 6 5210D-14Given: total_minutes 359Find: hours 5minutes 59Solution in C:Expressions with ints: Time Examplehours = total_minutes / 60 ;minutes = total_minutes % 60 ;D-15int radius;double volume;double pi = 3.141596;..volume = ( 4/3 ) * pi * radius *radius * radius; A Cautionary ExampleD-16Sometimes only ints make sensethe 15thspreadsheet cell, not the 14.997thcellDoubles may be inaccurate representing “ints”In mathematics 3 • 15 • (1/3) = 15But, 3.0 * 15.0 * (1.0 / 3.0) might be 14.9999997Last, and leastoperations with doubles is slower on some computersdoubles often require more memoryWhy Use ints? Why Not doubles Always?D-17Precedence 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 - 1Order of Evaluation 79(4 + 3) * (2 - 1) =4 + (3 * 2) - 1 =D-18Precedence rules:1. do ( )’s first, starting with innermost2. then do unary minus (negation): -3. then do “multiplicative” ops: *, /, %4. lastly do “additive” ops: binary+, -Operator Precedence RulesD-4D-19Precedence doesn’t help if all the operators have the same precedenceIs a / b * c equal to a / ( b * c ) or ( a / b ) * c ??Associativity determines the order among consecutive operators of equal precedenceDoes it matter? Try this: 15 / 4 * 2Precedence Isn’t EnoughD-20Associativity determines the order among consecutive operators of equal precedenceDoes it matter? Try this: 15 / 4 * 2(15 / 4) * 2 = 3 * 2 = 615 / (4 * 2) = 15 / 8 = 1Associativity MattersD-21Most C arithmetic operators are “left associative”, within the same precedence levela / b * c equals (a / b) * ca + b - c + d equals ( ( a + b ) - c ) + dC also has a few operators that are right associative.Associativity RulesD-22The Full Story...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 textbookLook in any C reference manualWhen in doubt: check the tableWhen faced with an unknown operator: check the tableD-23Mathematical formula:________-b + √b2-4 a c----------------------2 aC formula:(- b + sqrt ( b * b - 4.0 * a * c) ) / ( 2.0 * a )Precedence andAssociativity: ExampleD-24b*b- 4.0* a*cDepicting Expressions-67.05-1.0 15.2*-4.0*-60.82.5 2.5*6.25b = 2.5;a = -1.0;c = 15.2;D-5D-25What is 2 * 3.14 ?Compiler will implicitly (automatically) convert int to double when they occur together:int+ double double + double (likewise -, *, /)2*3 * 3.14 (2*3) * 3.14 6 * 3.14 6.0 * 3.14 18.842/3 * 3.14 (2/3) * 3.14 0 * 3.14 0.0* 3.14 0.0We strongly recommend you avoid mixed types:e.g., use 2.0 / 3.0 * 3.14 instead.Mixed Type ExpressionsD-26int total, count, value;double avg;total = 97 ; count = 10; avg = total / count; /*avg is 9.0!*/value = total*2.2; /*bad news*/Conversions in Assignmentsimplicit conversion to int – drops fraction with no warningimplicit conversion to doubleD-27Use a cast to explicitly convert the result of an expression to a different typeFormat: (type) expressionExamples (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 accidentExplicit Conversions D-28int total, count ;double avg;total = 97 ; count = 10 ;/* explicit conversion to double (right way)*/avg = (double) total / (double) count; /*avg is 9.7 */Using Casts/* explicit conversion to double (wrong


View Full Document

UW CSE 142 - Study Notes

Download Study Notes
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 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 2 2 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?