Download presentation
Presentation is loading. Please wait.
1
Computer Science 1620 Arithmetic
2
C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data we will look at some basic mathematical operators
3
Operators there are five arithmetic operators OperatorSymbol Addition+ Subtraction- Multiplication* Division/ Modulus%
4
Arithmetic Expression to use an arithmetic operator, use the following syntax: numeric expressionoperatornumeric expression A numeric expression is any expression whose value is a number. This is also called an operand. Operator in this case refers to one of the 5 arithmetic operators.
5
Arithmetic Expression Examples: add 3 and 4 subtract 40 from 75 multiply 36 to 97 divide 49 by 7 numeric expression operatornumeric expression 3 +4 75 -40 36 *97 49 /7 In short, exactly the infix notation that you are probably used to (just different symbols).
6
The statement: in C++ is an expression more specifically, an arithmetic expression the value of the expression is: whatever the formula evaluates to in the above example, the value of the expression is 7 3 + 4
7
Examples (from text) ExpressionValue 2 + 57 13 + 89102 34 – 2014 45 – 90-45 2 * 714
8
Where are arithmetic expressions used? anywhere an expression is valid we have seen two places so far where an expression is valid: in a cout statement in an arithmetic expression
9
In a cout statement #include using namespace std; int main() { cout << "14 + 27" << endl; cout << 14 + 27 << endl; return 0; } What will the output of this program be?
11
Why did we get this output? #include using namespace std; int main() { cout << "14 + 27" << endl; cout << 14 + 27 << endl; return 0; }
12
Why did we get this output? #include using namespace std; int main() { cout << "14 + 27" << endl; cout << 14 + 27 << endl; return 0; } The first expression is a string literal, because of the quotation marks. Remember that the value of a string literal is the text between its quotation marks
13
Why did we get this output? #include using namespace std; int main() { cout << "14 + 27" << endl; cout << 14 + 27 << endl; return 0; } The second expression is an arithmetic expression. Remember that the value of an arithmetic expression is the value of the formula.
14
Quick Note: More than one type of expression can be sent in the same cout command must be separated by << operator #include using namespace std; int main() { cout << 3 << "+" << 4 << "=" << 3+4 << endl; return 0; } IntegerString IntegerStringArith. Exp.
16
Integer Division what is the output of the following program? #include using namespace std; int main() { cout << 5.0 / 2.0 << endl; cout << 5 / 2 << endl; return 0; }
17
Integer Division 5 / 2 = 2 (the quotient) when two integers are divided, the result is an integer when a fraction (or remainder) occurs, it is simply ignored note that this ONLY APPLIES TO INTEGERS doubles are computed as expected
18
Examples: EquationResult 14 / 7 -5 / 2 -7 / -3 19 / 20 19.0 / 20.0 2 -2 2 0 0.95
19
Modulus the remainder of the division operation in mathematics, if we divide 34 by 5, we get: r 4 Quotient: Remainder: in C++ integer division gives us the quotient modulus gives us the remainder
20
Modulus #include using namespace std; int main() { cout << 34 / 5 << endl; cout << 34 % 5 << endl; return 0; }
21
Modulus has many uses in programming extracting digits determining a prime number hashing etc we will see modulus again when we reach conditionals
22
Embedded Arithmetic Expressions recall the format of an arithmetic expression recall that a numeric expression is an expression whose value is a number since an arithmetic expression's value is a number, we can use it in another arithmetic expression 3 + 4 numeric expression operatornumeric expression
23
Embedded Arithmetic Expressions recall the format of an arithmetic expression recall that a numeric expression is an expression whose value is a number since an arithmetic expression's value is a number, we can use it in another arithmetic expression 3 + 4 numeric expression operatornumeric expression 1 + 2 + 4 numeric expression operatornumeric expression
24
Embedded Arithmetic Expressions previous slide formally demonstrates that C++ can handle expressions with more than one operator important: this is nothing special, just one binary expression embedded in another each binary expression is executed individually cout << 1 + 2 + 4 << endl; 1 + 2 + 4 This expression is evaluated first Its value becomes the operand for the second expression.
25
Order and Precedence there are two possible orders of computation in the previous expression 1 + 2 + 4 3 + 4 7 Order 1: 1 + 2 + 4 1 + 6 7 Order 2: Which one does C++ use?
26
Order and Precedence there are two possible orders of computation in the previous equation 1 + 2 + 4 3 + 4 7 Order 1: Rule: arithmetic expressions are evaluated left to right when their operand is the same
27
Order and Precedence the order of evaluation becomes important for the – and / operators 1 - 2 - 4 -1 - 4 -5 Order 1: 1 - 2 - 4 1 - -2 3 Order 2:
28
Order and Precedence what about when the operators are not the same? 1 + 2 * 4 3 * 4 12 Order 1: 1 + 2 * 4 1 + 8 9 Order 2: Which one does C++ use?
29
Order and Precedence what about when the operators are not the same? 1 + 2 * 4 1 + 8 9 Order 2: RULE: When different operands are used, precedence rules take over. *, /, % are evaluated before any +, - operations
30
Order and Precedence what about when the operators have the same precedence? 1 - 2 + 4 -1 + 4 3 Order 1: 1 - 2 + 4 1 - 6 -5 Order 2: Which one does C++ use?
31
Order and Precedence what about when the operators have the same precedence? 1 - 2 + 4 -1 + 4 3 Order 1: RULE: When two arithmetic operands have the same precendence, they are evaluated left to right.
32
Order and Precedence what happens if I want the addition performed first? that is, parentheses override any precedence rule 1 + 2 * 4 (1 + 2) * 4
33
Examples: EquationResult 3 + 4 * 5 (3 + 4) * 5 8 / 2 * 4 8 / (2 * 4) 19 / 20 * 200 23 35 16 1 0
34
Mixed Mode Expressions when two integers are used in an arith. exp., the value of the operation is an integer as well 5 + 2 = 7 when two doubles are used in an arith. exp., the value of the operation is a double as well 5.0 + 2.0 = 7.0 what happens when an integer and a double are used in an operation? 5.0 + 2 = ????? 7 or 7.0?
35
Rules for Mixed Mode Arithmetic: (text) if the operator has the same types, then the value of the expression has the same type if one of the operands is a floating point number and the other an integer, then the integer is promoted to a floating point number. The value of the expression is a floating point number the precedence rules from before still apply
36
Evaluating mixed-mode expressions the rule is applied for each operation individually the type of the value of each expression must be remembered order of operations: usual precedence and left to right rules
37
Examples: EquationResult 3 / 2 + 5.5 15.6 / 2 + 5 4 + 5 / 2.0 4 * 3 + 7 / 5 – 25.5 4 + 3 / 5 + 3 6.5 12.8 6.5 -12.5 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.