1 / 23

Understanding Arithmetic Operations and Variable Declaration in C++

This chapter provides a comprehensive review of arithmetic operations and variable declarations in C++. It covers essential topics such as assignment operators, data types, and the significance of operators in computing sums, differences, and more. You'll learn how to properly declare variables and understand the nuances of integer versus float division. Examples clarify how different operators function and highlight the importance of operator precedence, syntax, and meaningful naming conventions. Master these fundamental concepts to enhance your programming skills in C++.

alexia
Télécharger la présentation

Understanding Arithmetic Operations and Variable Declaration in C++

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 Arithmetic Operations

  2. Review • function • statement • input/output • comment • #include • data type • variable • identifier • constant • declaration

  3. Declaration Statements • Variable: a memory location to store data • Variable value: the content in the location • Identifier: the symbolic name of a variable • Data Type: the type of data the variable is for • A declaration tells the compiler to allocate enough memory to hold a value of this data type and to associate the identifier with this location string name; DataType Identifier , Identifier, … ; string firstName, lastName; int num = 10;

  4. Arithmetic Operations

  5. Add Two Numbers In Math S = x + y Or x + y = S How to add two numbers in C++?

  6. Assignment Operator: = num1 + num2 = sum; // Valid in C++? // No • Assignment statement MUST be from right to left sum = num1 + num2; // Correct! // Not “equal” • Assignment statement: variable = expression;

  7. Are the two statements the same? Sum = Num1 + Num2; Sum = num1 + num2; // NO! // C++ is case sensitive!

  8. Arithmetic Operators • Addition: + • to compute Sum • Subtraction: - • to compute Difference • Multiplication: * • to compute Product • Division: / • to compute Quotient • Modulus: % • to compute the remainder from integer division You MUST use operators to compute in C++!

  9. Arithmetic Operators • What is ** in C++? result = x ** y; // Not good! • What is ^ in C++? result = x ^ y; // Not good! S = x ( y + z); //Is it good in C++? //Missing operator! S = x * ( y + z); // Good!

  10. Unary Operator • Unary operator: operator that has only one operand validCount++; // validCount = validCount + 1; totalCount --; // totalCount = totalCount - 1; • Binary operator: operator that has two operands num1 + num2

  11. Semantics, Syntax, and Style total = total + 2; • Semantics Increase total by 2 Assignment (right to left) Not equal • Syntax Statement terminator Case sensitive • Style one space before and after any operator meaningful name

  12. Precedence Rules • From high to low: • ( ) • *, /, % • +, -

  13. Examples x = 8 - 4 * 2; // result: x = (8 - 4) * 2; // result: y = 8 / 4 * 2; // result: y = 8 / (4 * 2); // result:

  14. More Examples z = 8 / 4 ^ 2; // No! z = 8 / 4 ** 4; // No! z = 8 / 4 * 4 // Result: ? X = 5(3 – 7); //Result: ? z = 5 / (2 * 5); // Result: 0.5 or 0?

  15. Integer Division vs. Float Division In Math 5 is almost always the same as 5.0 In C++ 5 is almost never the same as 5.0 Why? Store value in computer as bits, bytes. 5 is stored as an integer 5.0 is stored as a float number

  16. Integer Division We can only get integers! 7 / 5 Quotient: 1 Remainder: 2 5 / 7 Quotient: 0 Remainder: 5

  17. Float Division We get float result! 7.0 / 5.0 Quotient: 1.4 7 / 5.0 (Same) 7.0 / 5 (Same) 5.0 / 7 Quotient: 0.714285… As long as there is a float number in the expression, the result is a float number.

  18. The Cast Functions int num1, num2; float quotient; cin >> num1 >> num2; quotient = num1 / num2; // what’s the value of quotient? // How to get float quotient? quotient = float(num1) / num2; // Use cast function float(variable) quotient = float(num1 / num2); // Integer division or float division? // Integer division!

  19. Quotient (Division) Operator: / Result 1 0 2 0 14 0 Expression 7 / 5 5 / 7 14 / 5 5 / 14 143 / 10 10 / 143

  20. Remainder (Modular) Operator: % Result 2 5 4 5 3 10 Expression 7 % 5 5 % 7 14 % 5 5 % 14 143 % 10 10 % 143

  21. Exercises Expression Result 12 % 20 * 3 (12 % 20) * 3 12 * 3 20 % 12 * 3 12 / 20 * 3 20 / 12 * 3 20 / 12 % 3 20 % 12 % 3

  22. Why Integer Division? after class exercise: write this solution into a C++ program Get $143 from ATM 143 / 50 Number of $50 bills: 2 143 % 50 Amount left after $50 bills: 43 43 / 10 Number of $10 bills: 4 43 % 10 Amount left after $10 bills: 3 Number of $1 bills: 3

  23. Summary • Assignment Operator total = total + num1; // from right to left • Arithmetic Operators and Precedence Rules () /, %, * -, + • Semantics, Syntax, and Style

More Related