1 / 11

142 C-1

Doing Arithmetic. Today’s lecture is in chapter 2. Assignment statement:. operator. double area, radius; area = 3.14*radius*radius;. expression. assignment statement. Expression:. anything that has a value e.g. radius, 3.14 or a combination e.g. 3.14*radius*radius.

brone
Download Presentation

142 C-1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement: operator double area, radius; area = 3.14*radius*radius; expression assignment statement Expression: anything that has a value e.g. radius, 3.14 or a combination e.g. 3.14*radius*radius The value of an expression depends on _ the data: operands radius, 3.14... _ the operators: *, + ... 142 C-1

  2. Processing an assignment statement 1 Evaluate the right hand side (=expression) 3.14*radius*radius 2 The value is stored in the left hand side, the assignment variable area depends on: • types in the right hand side • operators in the right hand side • type of the left hand side Consider:area = 3.14*radius*radius; Note: can have year = year + 1; (NOT a mathematical equation!) 142 C-2

  3. Expressions with doubles Operators on doubles acts on one piece of data -23.4 -temperature multiply divide add subtract acts on two pieces of data 1.609 * miles Reminder: double= type for a floating point variable e.g. 2.15, -1.28e+28, NOT 3 or -4 • unary: - • binary: +, -, /, * All operators in C are unary or binary If so, what happens in 3.14*radius*radius ? 142 C-3

  4. Expressions with ints Operators on ints (same as for doubles) • unary: - • binary: +, -, *, /, % modulus add multiply subtract integer division as usual BUT / is the integer division Reminder:int= type for an integer variable e.g. 3, -216, NOT 3.0 2/3 is 0, 3/2 is 1 % is the modulus operator: a%b is the remainder of the division of a by b 114%50 is 2%3 is 14 2 142 C-4

  5. Examples always 0! mathematically 2 . 3 . = 2 not 1.9999 1 3 convert inches to feet and inches e.g. 74’’ = 6’ 2’’ int total_inches, feet, inches; total_inches = 74; feet = total_inches/12; inches = total_inches%12; Beware: double half_distance, distance; half_distance = 1/2 * distance; Why using ints and not always doubles? int sometimes makes more sense int number_of_children; computations with ints are faster doubles may be inaccurate: 142 C-5

  6. Operator Precedence precedence rules: a + b*c is a + (b*c) What is a + b*c ? Is it (a+b)*c or a+(b*c) ? 1. evaluate expressions in parentheses: start with the innermost set of parentheses 2. do unary - 3. do *, /, % 4. do binary +, - When coding, do not hesitate to use parentheses avoid errors and clearer! 142C-6

  7. Associativity a*b/c is (a*b)/c What is a*b/c? Is it a*(b/c) or (a*b)/c ? C binary arithmetic operators associate left to right within the same precedence level (but, unary - is right associative: write -2 not 2-) Example Add parentheses to the following expression to show how it is evaluated: a + b - c + d Ans: ((a+b) - c)+d There are also C operators that associate right to left e.g. the assignment operator = (see table on the inside cover of the text) 142C-7

  8. What happens in expressions with ints and doubles double + double conversion occurs here NOT before 0.0*6.2 0.0 AVOID MIXED TYPES When adding an intand a double, the compiler automatically converts the intto a double int + double (also with -, * and /) Beware: it can be tricky! 6*6.2 6.0*6.2 37.2 2*3*6.2 Thus: 0*6.2 2/3*6.2 142C-8

  9. Conversions in assignments implicit conversion of int to double avg is 9.0 DO NOT DO double TO int may not do what you expect! val1 is not 40….0 28 zeros int total, count; double avg; total = 96; count = 10; avg = total/count; What is the value of avg? avg = total/count; int val1; double val2; val2 = 4.0e+28; val1 = val2; /* BAD */ 142C-9

  10. Explicit conversions (type) expression e.g. (double) number_of_children declared as an int Example int total, count; double avg1, avg2, avg3, avg4; total = 96; count = 10; avg1 = total/count; avg2 = (double)total/(double)count; avg3 = (double)(total/count); avg4 = (double)total/count; printf(“%.1f, %.1f, %.1f, %.1f”, avg1, avg2, avg3, avg4); 9.0, 9.6, 9.0, 9.6 To perform a conversion explicitly, use a cast 142C-10

  11. A few words of advice • know the type of the variables in your • program C cares about types … so should you! There are lots of cases where types have to match up (e.g. in functions as we will see) • Style counts !!! _ Be clear _ KIS: Keep It Simple (don’t write huge expressions, break them up) _ use parentheses and casts 142C-11

More Related