Presentation is loading. Please wait.

Presentation is loading. Please wait.

Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:

Similar presentations


Presentation on theme: "Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:"— Presentation transcript:

1 Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
double area, radius; area = 3.14*radius*radius; operator 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, _ the operators: *, + ... 142 C-1

2 Processing an assignment statement
Consider: area = 3.14*radius*radius; 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 Note: can have year = year + 1; (NOT a mathematical equation!) 142 C-2

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

4 Expressions with ints Reminder: int = type for an integer variable
e.g. 3, -216, NOT 3.0 Operators on ints unary: - (same as for doubles) binary: +, -, *, add subtract multiply as usual integer division modulus /, % BUT / is the integer division 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 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; always 0! 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: mathematically = 2 not 3 1 142 C-5

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

7 Associativity 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 a*b/c is (a*b)/c (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 When adding an int and a double, the compiler automatically converts the int to a double int + double double + double (also with -, * and /) Beware: it can be tricky! 2*3*6.2 6*6.2 6.0*6.2 37.2 conversion occurs here NOT before Thus: 2/3*6.2 0*6.2 0.0* AVOID MIXED TYPES 142C-8

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

10 Explicit conversions To perform a conversion explicitly, use a cast
(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 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


Download ppt "Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:"

Similar presentations


Ads by Google