Download presentation
Presentation is loading. Please wait.
Published byChastity Melton Modified over 5 years ago
1
Programming Techniques :: Arithmetic & Boolean Operators
Last modified: 1st June 2019
2
www.drfrostmaths.com ? Everything is completely free.
Why not register? Registering on the DrFrostMaths platform allows you to save all the code and progress in the various Computer Science mini-tasks. It also gives you access to the maths platform allowing you to practise GCSE and A Level questions from Edexcel, OCR and AQA. With Computer Science questions by: Your code on any mini-tasks will be preserved. Note: The Tiffin/DFM Computer Science course uses JavaScript as its core language. Most code examples are therefore in JavaScript. Using these slides: Green question boxes can be clicked while in Presentation mode to reveal. Slides are intentionally designed to double up as revision notes for students, while being optimised for classroom usage. The Mini-Tasks on the DFM platform are purposely ordered to correspond to these slides, giving your flexibility over your lesson structure. ?
3
π π₯ =2π₯ π₯ 2π₯ π π₯,π¦ = π₯ 2 + π¦ 2 π₯ π₯ 2 + π¦ 2 π¦ π π
Functions and Operators in Maths π π₯ =2π₯ In a few lessons time, weβll look more thoroughly at functions; in maths these take some input(s) and produce an output. You may have encountered these as βnumber machinesβ. Input Output π₯ π 2π₯ Code: function f(x){ return 2*x } π π₯,π¦ = π₯ 2 + π¦ 2 Functions could potentially have multiple inputs: Inputs Output π₯ π π₯ 2 + π¦ 2 π¦
4
πππ π₯,π¦ =π₯+π¦ Functions and Operators in Maths
Addition is a function: it takes two numbers as inputs and outputs a number. But we would never write β4 added to 2β as πππ(4,2) or +(4,2), but as 4+2, i.e. where the + βfunctionβ appears between the two arguments. + is an example of an operator. Operators are just functions, but notationally are different, in that the inputs can appear either side of the operator, and we donβt need brackets around the inputs, unlike we did with add(x,y). ! An operator takes two values and performs some mathematical function on them.
5
Operators in Maths Prefix Infix Postfix ? ? Unary +3 N/A -4 ? Binary
(operator appears before argument) Infix (operator appears between arguments) Postfix (operator appears after argument) ? ? Unary (one argument/input) +3 5! π΄β² N/A -4 ? π΄β©π΅ Binary (two arguments) 3β4 N/A N/A 1Γ5 3<5 4=4 3βπ΄ Negation (not the same as subtraction!) Set complement (the set of all things not in A) Factorial (e.g. 5!=5Γ4Γ3Γ2Γ1)
6
Arithmetic Operators in Programming
Function Typical Operator Example Result Addition + 3 + 4 7 Subtraction - 7 - 2 5 Multiplication * 7 * 2 14 Division / 7 / 2 3.5 Exponentiation ^ or ** 7 ** 2 49 Quotient DIV 7 DIV 2 3 Remainder (modulus) % or MOD 7 % 2 1 Some programming languages cast the result to an integer if dividing integers, in this case giving 3. You could avoid this using 7.0 / 2.0 ? ? ? ? ? ? ? JavaScript uses **. You can also use Math.pow(7,2) The quotient is the whole number times 2 goes into 7. JavaScript doesnβt have this operator; we could use: Math.floor(7/2) The remainder when we divide 7 by 2. This is very useful for determing whether a n number is even or odd. n % 2 will be 0 if n is even, and 1 if n is odd.
7
Comparison Operators Input: two values of any type Output: boolean ? ?
The previous operators all inputted two numbers and outputted a number. For comparison operators (like those below), Input: two values of any type Output: boolean ? ? Name Operator Example Result Less than < 3 < 5 true Less than or equals <= 4 <= -3 false Equals == 5 == 5 Not equals != or <> βcβ != βcβ ? ? ? ? We will see that comparison operators are exceedingly important when we encounter if and while statements and for loops. Notes: In programming, = and == have very different meanings. =, as we previously saw, is assignment, e.g. x = 5 assigns the variable x the value 5. == however is equality in the usual mathematical sense, i.e. βis the left-hand thing equal to the right-hand thing?β. In maths, you have probably been taught the distinction between an expression (e.g. 2π₯+1) and equality (e.g. 2π₯+1=5). But in programming, equations are actually expressions, whose values are either true (if the equation is true) or false otherwise.
8
Triple Equals We have previously seen that weakly-typed languages such as JavaScript attempt to βcoerceβ/cast values used in operators so that they are of a consistent type. e.g. ? ? 3 + "4" ο "3" + "4" ο "34" 3 * "4" ο 3 * 4 ο 12 ? ? The problem is that comparison operators such as == will also attempt to coerce its arguments: The condition here will be true, as 3=="3" will become "3"=="3" which evaluates to true. This might be undesirable, as we might want to check that x actually is the number 3, rather than a string which just happens to be 3. var x = 3; if(x == "3") { ... } The solution is to use ===, which checks whether the values are strictly equal without any type conversion. 3 === β3β ο false ?
9
Increment Operators var x = 3; x++; console.log(x); x--; x+=5;
In programming itβs incredibly common to want to add or subtract 1, e.g. each time an building entrance system detects someone entering or exiting, where we have a variable to maintain the number of people in the building. We will return to these when we cover if/while/for constructions, but for now... var x = 3; x++; console.log(x); x--; x+=5; x++ increments x by 1, i.e. it is equivalent to x = x + 1; x-- decrements x by 1. x+=5 increases x by 5. The outputs are therefore 4, 3 and 8. x*=6 is equivalent to x = x * 6. These operators are both statements and expressions: x++ for example increments x by 1, but then returns to old value of x: var x = 3; var y = x++; console.log(x); console.log(y); The outputs are 4 and 3. The x++ increments x by 1 but the value of this expression is the old value of x, i.e. 3. Therefore y is assigned to 3. We could similarly use ++x to increment x but return the new value of x.
10
What happens? ? ? ? var x = 10; console.log(++x); console.log(x);
2 var x = 10; console.log(x++); console.log(++x); ? ? Outputs 11 and 11. Outputs 10 and 12. 3 var x = 10; var y = 20; x = 2 * y++; y = ++x; console.log(x); console.log(y); ? Outputs 40 and 41.
11
Boolean Operators ? ? ? ? ? ? Example Evaluates to⦠4>3 && 4==3
Again, we will cover these in more detail when we cover if/for/while/do constructions. In real life we often combine true/false statements with words like βandβ and βorβ, e.g. βAshwin is cool and he has good hairβ. We can also negate statements, βAshwin is not coolβ will be true if Ashwin is indeed not cool, and false if Ashwin is in fact cool. Example Evaluates toβ¦ 4>3 && 4==3 false !(3==4) true (4>3 || 3>5) && 2==2 true || true && ο and || ο or ! ο not ? ? ? βBIDMASβ-like rules apply to Boolean operators too! We need brackets as && has a higher precedence than ||. ? Challenge 1: Without using brackets, rewrite: !(a && b) !a || !b ? In words, this like saying βIf raccoons are not both cute and cuddly, then theyβre either not cute or not cuddly.β Challenge 2: Exclusive or (often xor) is when either value is true, but not both. Write an expression for the exclusive-or of a and b. (a || b) && !(a && b) ?
12
Summary of Types of Operators
Input Type Output Type Arithmetic (+, *, %) Number Comparison (<, ==, !=) Boolean Boolean (&&, ||, !) ? ? ? ? ? ?
13
Review ? ? ? ? ? List all the comparison operators in JavaScript.
<=, <, >=, >, ==, !=, ===, !== Why might we use ===? It checks whether two values are strictly equal, without converting type. What is wrong with the code: if(x = 4) β¦ x = 4 is an assignment not an equality comparison. The student should have written if(x == 4). Advanced: What would actually happen with the code: if(x = 4) { console.log("Hello") } Interestingly, assignments return the value that was just assigned: so x = 4 ο 4 ο true, because the βifβ expects a Boolean as its condition, and thus the 4 is coerced to true (recall that any value other than 0 converts to true). The effect therefore is that x is assigned to 4, and Hello will be outputted. How could we determine if an integer n is even, using arithmetic operators? Even numbers give a remainder of 0 when divided by 2. n % 2 == 0 will return true if n is even and false is n is odd. ? ? ? ? ?
14
Coding Mini-Tasks Return to the DrFrostMaths site to complete the various mini-coding tasks on functions.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.