190 likes | 206 Views
Explore the formal use of operators in C++ programming, learn about expressions, operands, and results. Understand unary and binary operators, arithmetic operators, and boolean operators, with examples and explanations.
E N D
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 • 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
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
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
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
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)
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.
Arithmetic Operators Unary operators: + does not change its operand - negates the expression
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))
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
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
Arithmetic Operators • Parenthesize the following to show how it’s evaluated 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2
Boolean Operators • We touched on these on Friday • Let’s get more detailed about them, particularly the comparison operators
Boolean Operators These take operands that are arithmetic or pointers, and return values of type bool.
Boolean Operators • We’ve seen how the boolean operators operate by themselves • truth tables • How do others work?
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
Boolean operators • How would we write a while loop that reads values until the value read is 42?
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?