1 / 19

C++ crash course

C++ crash course. Class 6 animal type, operators. Operators. You ’ ve seen these: +, -, *, / Let ’ s take a look at them a little more formally. Operators. An expression is composed of one or more operands , combined by operators Every expression yields a result

vita
Download Presentation

C++ crash course

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. C++ crash course Class 6 animal type, operators

  2. Operators • You’ve seen these: • +, -, *, / • Let’s take a look at them a little more formally.

  3. Operators • An expression is composed of one or more operands, combined by operators • Every expression yields a result • In an expression with no operator, the result is the operand itself (literal constant or variable) line 1 int ival; line 2 if(ival) { … On line 2, ival in this case is an expression which evaluates to the variable ival

  4. Operators • The result of expressions involving operators is determined by applying each operator to its operands – result is usually an rvalue, meaning we can read from it but we can’t assign to it int var; 3+5 = var; // INVALID

  5. Operators • Operators are specifically defined for the type of the operands it’s handed i + j …could be integer addition concatenating strings floating point addition ??? …depending on the types of i and j

  6. Operators • Operators can be unary (operates on one operand) or binary (takes two operands) + is binary: must have two things (3 + 5) * can be binary or unary: *ptr * is acting as a unary operator 3 * 5 * is acting as a binary operator

  7. Operators • Using an operator is exactly like using a function, except with a funny sort of syntax c = a + b is the same as c = operator+(a,b)

  8. Operators • So far we’ve only seen code dealing with one operator at a time • When we deal with multiple operators, we have to keep in mind the following notions: • precedence • associativity • order of evaluation Knowing these things helps us understand how the computations will proceed to evaluate an expression.

  9. Arithmetic Operators Unary operators: + does not change its operand - negates the expression

  10. Arithmetic Operators • These can be applied to any of the arithmetic types (int, float, etc) • also any type that can be converted into an arithmetic type The table from the last slide is in order by precedence unary operators happen first then multiplication and division then addition and subtraction 5 + 10 * 20/2; will be evaluated as (5 + ((10 * 20)/2))

  11. Arithmetic Operators • The result of combining an operator and operand(s) is the same type as the operands, or the more complex of the two types int * int = int double * int = double This means if you try to do division with integers that don’t divide evenly, you’ll get a funny answer int val = 7/3; cout << val; 2

  12. Arithmetic Operators • The division and modulus operators are also a little funny when it comes to negation • When both are positive, the result is positive; if both are negative, division comes out positive and the result of modulus is negative 21 % 6; // ok: result is 3 21 % 7; // ok: result is 0 -21 % -8; // ok: result is -5 21 % -5; // machine dependent: result is 1 or -4 21 / 6; // ok: result is 3 21 / 7; // ok: result is 3 -21 / -8; // ok: result is 2 21 / -5; // machine-dependent: result -4 or -5

  13. Arithmetic Operators • Parenthesize the following to show how it’s evaluated 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2

  14. Boolean Operators • We touched on these on Friday • Let’s get more detailed about them, particularly the comparison operators

  15. Boolean Operators These take operands that are arithmetic or pointers, and return values of type bool.

  16. Boolean Operators • We’ve seen how the boolean operators operate by themselves • truth tables • How do others work?

  17. Boolean Operators • We use these to compose conditions (things that go in while loops and if statements) • Note: relational operators don’t chain! if (i < j < k) { // this is NOT what you want; it does not check to see if j is between i and k (What does it do instead?) We’d instead have to ask: if (i < j && j < k) { // this gets you what you want

  18. Boolean operators • How would we write a while loop that reads values until the value read is 42?

  19. Boolean Operators • How can we write an expression that tests four values a, b, c and d, making sure that a is great than b, which is greater than c, which is greater than d?

More Related