1 / 15

Chapter 3

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.

miriamhang
Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Math Operations

  2. 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.

  3. 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;

  4. Arithmetic Operators • Arithmetic operators are used to perform calculations in C++. • The arithmetic operators available in C++: • + Addition • - Subtraction • * Multiplication • / Division • % Modulus

  5. 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;

  6. 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;

  7. The Modulus Operator • The modulus operator (%) returns the remainder rather than the result of division. • Examples: • 9 % 2 would result in 3. • 10 % 2 would result in 0. • 20 % 6 would result in 2.

  8. 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.

  9. 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.

  10. 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.

  11. 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).

  12. 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.

  13. 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.

  14. 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.

  15. 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.

More Related