Arithmetic Expressions and Evaluation Guidelines
Expressions in programming, specifically arithmetic expressions and evaluation rules. Understand how to combine constants, variables, and operators to form expressions. Learn about precedence, associativity, and evaluation principles for expressions. Delve into the evaluation of arithmetic expressions using various operators and operands, along with understanding the significance of precedence and associativity rules.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
COM101B LECTURE 3: EXPRESSIONS & STATEMENTS ASST. PROF. DR. HACER YALIM KELE REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
EXPRESSIONS A combination of constants and variables together with the operators is referred to as an expression. Constants and variables by themselves are considered as expressions An expression that contains only constants is called a constant expression Let s start with arithmetic expresssions.. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
ARITHMETIC EXPRESSIONS In an arithmetic expression; integer, character, or floating-point data can participate as operands The following are valid expressions: 012 n i -x 12.3/4-(i+1) i%j REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
EVALUATION OF AN EXPRESSION Every expression has a value The value of an expr. is determined by first binding the operands to the operators and then evaluatig the expression If an expression contains more than one operator, precedence rule is considerd for binding the operands Parantheses has highest precedence REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
PRECEDENCE AND ASSOCIATIVITY RULES Operators Type Associativity + - Unary Right to left * / % Binary Left to right + - The operators in the same row has equal precedence The ops. in the upper rows have higher precedence Binary Left to right REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
PRECEDENCE RULE Operators receive their operands in order of decreasing precedence If an expression contains two or more operators of equal precedence, their associativity determines their relative precedence REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
EVALUATION OF SOME ARTIHMETIC EXPR.S REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
REFERENCE: PROGRAMMING IN ANSI C, KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
THE PARANTHESIS RULE The paranthesis can be used to alter the order of precedence they can be used to force an operation, or a set of operations, to have a higher precedence order The operation will be performed in the innermost set of paranthesis, using the precedence and associativity rule where appropriate, and then in the next outer set, etc. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
ASSIGNMENT STATEMENTS An assignment expression: variable = expression An assignment expression followed by a semicolon becomes an assignment statement. = : is an operator that assigns the value of the expr. on the right hand side to the varible on the left hand side. This x = y and y = a produce different results. The value of an assignment expr. is the assigned value REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
ASSIGNMENT STMT REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
ASSIGNMENT STMT The precedence of the assignment is lower than that of the arithmetic operators: x = x + y is x = (x+y) ; NOT (x=x) +y It is mandatory that the left operand be an lvalue lvalue: an expression that refers to an object that can be examined as well as altered ex: a varible name rvalue: an expression that permits examination but not alteration ex: a constant 15 = n; // not valid: 15 is not an lvalue x+1.0 = 2.0; // not valid: x+1.0 is not an lvalue REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
INCREMENT AND DECREMENT OPERATORS Increment (++): increment the value by 1 (Unary op.) Decrement (--): decrement the value by 1 (Unary op.) Prefix form: ++variable, --variable the variable is incremented/decremented by 1, before it is used Postfix form: variable++, variable-- the variable is incremented/decremented by 1 after the use. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
INCREMENT AND DECREMENT OPS Example: n = ++i; -> i = i+1; n = --i; -> i = i-1; n = i; n = i; n = i++; -> n = i; n = i--; -> n = i; i = i+1; i = i-1; REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
EXERCISES REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
SOLUTIONS REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
COMPOUND ASSIGNMENT OPERATORS op= : compound assignment operators variable op= expression; means: variable = variable op expression Example: i += 6; // i = i+6 a *= x+y // a = a*(x+y) REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
COMPOUND ASSIGNMENT OPS All the compound assignment ops. have equal precedence, the same with the simple assignment Their precedence is lower than the arithmetic operators The value of a compound assignment expr. is the value that is assigned to the left hand side They associate from right to left (just like simple assignments) Example: i = j = k = 0; // i = (j = (k = 0)) i += j = k; // i += (j = k) REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
EXERCISES REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
SOLUTIONS REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992