150 likes | 157 Views
Chapter 3. Math Operations. Objectives. Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division by zero. Increment and decrement variables. Explain the order of operations. Properly mix data types in calculations.
E N D
Chapter 3 Math Operations
Objectives • Use the assignment and arithmetic operators. • Use operators in output statements. • Explain the problem with division by zero. • Increment and decrement variables. • Explain the order of operations. • Properly mix data types in calculations. • Avoid overflow and underflow in calculations. • Explain floating-point rounding errors.
Assignment Operator • Recall, the assignment operator changes the value to the left of the variable. • Several variables can be declared in one statement: int i,j,k; • Several variables may also be initialized in one statement: i=j=k=25; • Variables can be declared and initialized in one statement: float n = 4.5;
Arithmetic Operators • Arithmetic operators are used to perform calculations in C++. • The arithmetic operators available in C++: • + Addition • - Subtraction • * Multiplication • / Division • % Modulus
Using Arithmetic Operators • Arithmetic operators are used with two operands. • The minus sign can be used alone to change the sign of a value. • The portion to the right of the operator is the expression. • The assignment operator is different than the equals sign in algebra: x = x + 10;
Example Statements What does each one mean? • cost = price + tax; • owed = total - discount; • area = l * w; • one_eighth = 1 / 8; • r = 5 % 2; • x = -y;
The Modulus Operator • The modulus operator (%) returns the remainder rather than the result of division. • Examples: • 9 % 2 would result in 1. • 10 % 2 would result in 0. • 20 % 6 would result in 2.
Incrementing and Decrementing • Adding one to a variable is called incrementing. • In C++ you increment with the ++ operator. • Subtracting one from a variable is called decrementing. • In C++ you decrement with the -- operator.
Variations of Increment and Decrement k = j++; • In the case of the statement above, k is assigned the value of the variable j before j is incremented. k = ++j; • In this second example, k is assigned the value of j after j is incremented.
Order of Operations 1. Minus sign used to change sign (-) 2. Multiplication and division (* / %) 3. Addition and subtraction (+ -) You can use parentheses to override the order of operations.
Mixing Data Types • C++ allows you to mix data types in calculations. • C++ can automatically handle the mixing of data types (called promotion), or you can direct the compiler on how to handle the data (called typecasting).
Overflow and Underflow • Overflow is the condition where a value becomes too large for its data type. • Underflow occurs with floating-point numbers when a number is too small for the data type.
Summary • The assignment operator (=) changes the value of the variable to the left of the operator to the result of the expression to the right of the operator. • You can initialize multiple variables to the same value in a single statement. • The arithmetic operators are used to create expressions. • The modulus operator (%) returns the remainder of integer division.
Summary • The ++ and -- operators increment and decrement arithmetic variables respectively. • The placement of the ++ and -- operators becomes important when the operators are used as part of a larger expression or in an output statement. • C++ calculations follow an order of operations. • C++ allows data types to be mixed in calculations.
Summary • Overflow is a condition where an integer becomes too large for its data type. • Underflow occurs when a floating-point number is so small that a data type interprets it as zero.