C++ Expressions and Arithmetic Operations

expressions n.w
1 / 17
Embed
Share

Learn about expressions in C++ programming, including arithmetic expressions, integer division, modulus operations, and increment/decrement. Discover how mixed-type expressions work in C++. Explore examples and concepts explained by Professor John Carelli from Kutztown University.

  • C++
  • Expressions
  • Arithmetic
  • Operations
  • Programming

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation be performed to produce a result of a specified type Arithmetic expression Performs a numeric computation producing a numerical result Logical expression Produces a Boolean result evaluating to true or false String expression Produces a result of type string Expressions may contain other expressions which are evaluated in order producing an overall result Professor John Carelli Kutztown University Computer Science Department

  2. Arithmetic Expressions Basic arithmetic operations + Addition - Subtraction * Multiplication / Division % Modulus (remainder operator) integers only! Each is a binary operator Operate on 2 numbers, left and right Examples: 2+2 4/2 2*5 Professor John Carelli Kutztown University Computer Science Department

  3. Integer Division The result is always an integer!!! Any remainder (or fraction) gets truncated NOT rounded! Examples: o 15 / 3 is 5 o 15 / 2 is 7 o 0 / 15 is 0 o 15 / 0 is undefined To get decimals, use floating point numbers 15.0 / 2.0 -> 7.5 Professor John Carelli Kutztown University Computer Science Department

  4. Modulus for Integers Used only with integers Get a compiler error if used with a float Yields remainder instead of quotient the result is integer Examples: o 7 % 2 -> 1 o 299 % 100 -> 99 o 49 % 5 -> 4 o 15 % 0 undefined o 15 % -7 system dependent Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  5. Increment and Decrement Increment ++ Increment adds one to an existing variable: x++ If x was 2, it becomes 3 post-increment Can also use ++x (pre-increment) Decrement -- Decrement subtracts one from an existing variable: x-- If x was 2, it becomes 1 post-decrement Can also use --x (pre-decrement) Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  6. Mixed-type Expressions Example: 4.6 / 2 evaluates to 2.3 Rule: when an integer and a floating point operand are combined by an operator, the integer gets converted to the floating point type Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  7. Assignment (= =) The value of a variable can be set using the assignment operator (=) It is NOT an equals operator Examples: x= 3 +5; y= x/2; Also: y= 3*y; Note: This is NOT saying that y is equal to 3*y (which would not make much sense). It is multiplying y by 3 and assigning the result back to y. Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  8. Mixed-type Assignments If the variable on left side of assignment is of different type than the type of the evaluated expression on the right side of =, the result of the expression must be converted to the appropriate type Conversion from a float to an int involves dropping (i.e. truncating, not rounding!) the decimal part Example: int n; float x=2.7; n= x; // n will be assigned the value 2 (trick: to round, add 0.5 first!) Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  9. Mixed-type Assignment Examples float a, b, x; int m, n; a=10; b = 3.5; m=5; n = 10; x = m / n; // result is 0 assigned to x m = b * 3; // result is 10 assigned to m // result is 10.0 stored in a Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  10. Casting Type casting allows for explicit conversion from one data type to another Accomplish by using the type name directly int sum=10, n=4; float average= float(sum) / float(n); result is: 2.5 (not 2!) Professor John Carelli Kutztown University Computer Science Department

  11. Order of Operator Precedence Highest ( ) nested expressions evaluated inside out unary +, - *, /, % binary +, - Associativity Lowest Warning: watch out for the types of operands and the type of the result when evaluating each operand! Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  12. PEMDAS Order of operations: 1. Parentheses: () 2. Unary Negation (-) also increment (++) and decrement (--) 3. Exponentiation: x2 (not in C++) 4. Multiplication (*) or Division (/) - also Modulus (%) 5. Addition (+) or Subtraction (-) If operations are at the same level, they are evaluated left to right Except for unary operations which go right to left Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  13. Step-by-Step Expression Evaluation float area, PI=3.14159, radius; radius= 2.0; Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  14. Step-by-Step Expression Evaluation: z z - - (a +b / 2) + w * (a +b / 2) + w * - -y y Note: all variables are type int Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  15. Evaluation tree for: m = x + k / 2; m = x + k / 2; int m, k=5; float x=5.5; Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  16. Mathematical Formulas in C++ a = bc not valid C++ syntax! Must use * operator a = b * c; m =y b x a Must use ( ) and / m = (y - b) / (x - a); . why? Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

  17. Pre Pre vs vs. . Post Increment and Decrement Post Increment and Decrement Pre-increment operations occur before the rest of the expression is evaluated Do the increment first, then use that result in the remaining expression Pre-increment: int x=2, y; y =1 + ++x; Result: x: 3 y: 4 Post-increment operations occur after the rest of the expression is evaluated Evaluate the expression first with the initial value of the variable to be incremented. Then, do the incrementation on the variable. Same applies to decrement Post-increment: int x=2, y; y =1 + x++; Result: x: 3 y: 3 Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Related


More Related Content