WEEK 3- Performing-Arithmetic-Operations-Meeting-6.pptx
Document Details
Uploaded by ColorfulEarth
Tarlac State University
Tags
Full Transcript
Performing Arithmetic Operations Meeting 6 PLF: Program Logic Formulation College of Computer Studies Tarlac State University Arithmetic Operators + (plus sign) – addition - (minus sign) – subtraction * (asterisk) – multiplication / (slash) – division...
Performing Arithmetic Operations Meeting 6 PLF: Program Logic Formulation College of Computer Studies Tarlac State University Arithmetic Operators + (plus sign) – addition - (minus sign) – subtraction * (asterisk) – multiplication / (slash) – division % (percent) – modulus Rules of Precedence Also called the order of operations, dictates the order in which the operations in the same statement are carried out. Expressions within the parentheses are evaluated first. If there are multiple sets of parentheses, the expression within the innermost parentheses is evaluated first. Multiplication and division are evaluated next, from left to right. Addition and Subtraction are evaluated next from multiplication and division, and from left to right. BEDMAS – Brackets, Exponents, Division, Multiplication, Addition, Subtraction Arithmetic operators with the same precedence have left-to-right associativity Assignment operator – very low precedence, the operations on the right of the assignment operator are always performed first. Summary of Precedence and Associativity Operator Operator Name Precedence Associativity Symbol = Assignment Lowest Right-to-Left + Addition Medium Left-to-Right - Subtraction Medium Left-to-Right * Multiplication Highest Left-to-Right / Division Highest Left-to-Right % Modulus Highest Left-to-Right Combinations of Assignment and Arithmetic Operators Arithmetic Assignment Equivalent Code age += 14 age = age + 14 age -= 14 age = age - 14 age *= 14 age = age * 14 age /= 14 age = age / 14 age %= 14 age = age % 14 Effects of data type on arithmetic operations In Addition, Subtraction, Multiplication, and Division the combination of data types for numeric values (Integer and Floating Point) will work as expected. However, for division, in some programming languages such as Java, C#, and C++ the resulting quotient is truncated or cutoff when the numerator and denominator are both integers but not perfectly divisible. int 5 / int 3 = 1 (the decimal part 0.66667 has been truncated) Illustrations 1. A = A – B + A * C / D If A = 5, B = 2, C = 3, and D = A, what is the new assigned value1st of A?2nd Solution: 1. A = 5 – 2 + 15 / 5 A=5–2+5*3/5 2. A = 5 – 2 + 3 3rd 4th 3. A = 3 + 3 4. A = 6 5. NEW ASSIGNED VALUE OF A IS 6 5th What if you ran the code again, What is the new value of A? Is there a way to simplify the code? Evaluate the following: Will there be a difference in the result between the expressions (1 VS 2)? Numbe Expression 1 Expression 2 r 1. A=B+C–D A=C–D +B 2. A=B+B*C A = B + (B * C) 3. A=B–C*D/E A = B – C * (D / E) 4. A=B–C*D/E A = B – (C * D / E) 5. A=B–C*D/E A = B – (C * D) / E Evaluate the following: What are the results of the following expressions? 1. Result = 6 * (5 – 1) / 2 2. Result = 100 – 25 * 3 % 4 3. Result = ( 100 – (25 * 3)) % 4 4. Result = 1 + 4 / 2 * 2^2 – 2 % 3 5. Result = 1 + ((4 / 2 * 2)^2 – 2) % 3