Presentation is loading. Please wait.

Presentation is loading. Please wait.

4. EXPRESSIONS. Display the value of pi, 3.14159265 to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.

Similar presentations


Presentation on theme: "4. EXPRESSIONS. Display the value of pi, 3.14159265 to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using."— Presentation transcript:

1 4. EXPRESSIONS

2 Display the value of pi, 3.14159265 to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using a single quotation mark for feet and double quotation mark for inches and prints the person’s height in centimeters to two decimal places

3 /* Computes a Universal Product Code check digit */ #include int main(void) { int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total; printf("Enter the first (single) digit: "); scanf("%1d", &d); printf("Enter first group of five digits: "); scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5); printf("Enter second group of five digits: "); scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5); first_sum = d + i2 + i4 + j1 + j3 + j5; second_sum = i1 + i3 + i5 + j2 + j4; total = 3 * first_sum + second_sum; printf("Check digit: %d\n", 9 - ((total - 1) % 10)); return 0; }

4 Arithmetic Operators The arithmetic operators are: + unary plus - unary minus +addition - subtraction * multiplication / division % remainder (modulus)

5 Arithmetic Operators The value of i % j is the remainder when i is divided by j. For example, the value of 10 % 3 is 1; the value of 12 % 4 is 0. The % operator requires integer operands; all other operators allow either integer or floating-point operands. If both operands are integers, / truncates the result toward 0. There is no exponentiation operator. Must use “pow” function defined in

6 Operator Precedence When an expression contains more than one operator, the meaning of the expression may not be immediately clear: Does i + j * k mean (i + j) * k or i + (j * k) ? In C, this potential ambiguity is resolved by operator precedence. Precedence of the arithmetic operators: Highest: + - (unary) * / % Lowest: + - (binary)

7 Operator Precedence Examples : i + j * k means i + (j * k) -i * -j means (-i) * (-j) +i + j / k means (+i) + (j / k)

8 Associativity Operator precedence rules alone aren’t enough when an expression contains two or more operators at the same level of precedence. The associativity of the operators now comes into play. The binary arithmetic operators are all left associative (they group from left to right); the unary operators are right associative. Examples of associativity: i - j - k means (i - j) - k i * j / k means (i * j) / k i - j * i + k means (i - (j * i)) + k - - i means -(-i)

9 Simple Assignment One way to change the value of a variable is by assignment. Simple assignment has the form v = e, where v is an lvalue and e is an expression. Examples: i = 5; /* i is now 5 */ j = i; /* j is now 5 */ k = 10 * i + j; /* k is now 55 */ If v and e don’t have the same type, then the value of e is converted to the type of v before assignment: int i; float f; i = 72.99; /* i is now 72 */ f = 136; /* f is now 136.0 */

10 Simple Assignment (Continued) Assignment is a true operator, just like + and *. The value of v = e is the value of v after the assignment. Assignments can be chained together: i = j = k = 0; Since the = operator is right associative, this statement is equivalent to i = (j = (k = 0)); The assignment operator may appear in any expression: i = 1; k = 1 + (j = i); printf("%d %d %d\n", i, j, k); /* prints "1 1 2" */ Hiding an assignment inside a larger expression usually isn’t a good idea.

11 Lvalues The increment and decrement operators require an lvalue (pronounced “Lvalue”) as their operand. An expression has an lvalue if it represents a storage location in computer memory. Variables are lvalues, since a variable is just a name for a storage location. Constants and most expressions containing operators are not lvalues, including the following: 12 i + j -i

12 Compound Assignment In addition to the simple assignment operator =, C provides ten compound assignment operators, including these five: += -= *= /= %= The expression v += e is equivalent to v = v + e, except that v is evaluated only once in the former expression. The other operators have similar meanings.

13 Increment and Decrement Operators The ++ and -- operators increment and decrement variables. Both operators have the same precedence as negation. Either operator can be prefix or postfix: ++i i++ --i i-- When used as a prefix operator, ++ increments the variable before its value is fetched: i = 1; printf("i is %d\n", ++i); /* prints "i is 2" */

14 Increment and Decrement Operators When used as a postfix operator, ++ increments the variable after its value is fetched: i = 1; printf("i is %d\n", i++); /* prints "i is 1" */ printf("i is %d\n", i); /* prints "i is 2" */ The -- operator has similar properties: i = 1; printf("i is %d\n", --i); /* prints "i is 0" */ i = 1; printf("i is %d\n", i--); /* prints "i is 1" */ printf("i is %d\n", i); /* prints "i is 0" */

15 Compound Assignment Examples: i = 5; j = 2; i += j; /* i is now 7 */ i = 5; j = 2; i -= j; /* i is now 3 */ i = 5; j = 2; i *= j; /* i is now 10 */ i = 5; j = 2; i /= j; /* i is now 2 */ i = 5; j = 2; i %= j; /* i is now 1 */

16 Compound Assignment The compound assignment operators have the same properties as the = operator; in particular, they’re right associative and can be used in arbitrary expressions.

17 Expression Statements Any expression can be used as a statement: ++i; The value of the expression is discarded; the statement has no effect unless the expression modifies one of its operands: i = 1;/* stores 1 into i, then evaluates i and discard the result */ i--; /* fetches i, discards the result, then decrements i */ i + j; /* adds i and j, then discards the result */

18 Expression Statements Expression statements are typically used to perform assignments or to increment or decrement variables. Warning: A minor typing mistake can turn a meaningful statement into a meaningless expression statement. For example, the statement i = j; could easily become i + j;

19 Partial List of C OPerators PrecedenceOperatorAssociativity 1++ (postfix) -- (postfix) Left 2++ (prefix) -- (prefix) + - Right 3*/%*/% Left 4+-+- 5= *= /= %= += -+right

20 Expression Evaluation a = b += c++ - d + --e / -f Order of subexpression evaluation a = 5; c = (b = a + 2) - (a = 1);

21 #include int main() { printf("01(a). %d\n", 3 -2 / 4); printf("01(b). %f\n", 3 -2.0 / 4); printf("\n02. %d\n", -27 / -5 + 4 / 3); printf("\n03. %d\n", 16 % -5 + 7 * 6); printf("\n04(a). %d\n", -12 * 3 % 2 * -23 / +6 - 5 * 2); float x = 3; printf("\n05(a). %f\n", x-- * 2 + 5); printf("05(b). %f\n", x); printf("05(c). %f\n", --x * 2 + 5); printf("05(d). %d\n", 3 % 5 / (5 % 3)); return 0; }


Download ppt "4. EXPRESSIONS. Display the value of pi, 3.14159265 to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using."

Similar presentations


Ads by Google