number MOD 1 = 0
Mod
OperatorIn this lab, you will program procedures to carry out mathematical tasks.
On this page, you create a predicate to check whether one number is divisible by another.
mod
block reports the remainder when the first input is divided by the second. For example, reports 2 because when 17 is divided by 5, the remainder is 2. When one number divides another evenly, the remainder is 0. So for example, reports 0.mod
operator: The expression would be written as 17 MOD 5on the exam. If you see an expression with variables as input to
mod
, such as a MOD b, you can assume that a is zero or positive, and b is strictly positive (because you can't divide by 0).
+,
-,
*,
/(plus, minus, times, divide) as well as
MOD. Arithmetic operators are part of most programming languages. (Most text languages use
*rather than
×for multiplication because
×isn't on most keyboards, and because it looks too much like the letter x.)
+
block is an input to the ×
block, so the expression means 3×(5+4). Similarly, means (3×5)+4. In Snap!, it's as if there are parentheses around every operation. But in text languages, you can write 3 * 4 + 5without parentheses, so they need the rules you learn in math class (multiplication before addition, and so on). The
mod
operator is like multiplication and division; it happens before addition and subtraction. So for example, 7 + 5 MOD 2 - 6means
7 + 1 - 6, which is, of course, 2.
even?
block to draw a brick wall because the even and odd numbered rows are different. mod
to build a predicate that tests for divisibility.divisible by?
predicate to build a predicate that tests whether its input is even (divisible by 2).Katherine Johnson (1918–2020) was an aerospace technologist with a Ph.D. in mathematics who worked for NASA calculating the motion of spacecraft. Johnson, originally hired as a human computer, verified the calculations of digital computers, helped calculate the trajectory of Apollo 11 (the first time humans walked on the Moon), worked on plans for a mission to Mars, and encouraged students to pursue careers in science, technology, engineering, and mathematics.
NASA's memorial video: "Katherine Johnson: An American Hero"
move()and
turn_clockwise()aren't built in to the AP's language so they are written in lower case like other programmer-defined procedures.
IF(size > 15) { REPEAT 4 TIMES { move(size) turn_clockwise(90) } }
As in Snap!, if the condition (
size) > 15
is true
, the code inside the if
statement runs; if the condition is false
, the code does not run.
numberis odd:
IF (MISSING CONDITION) { DISPLAY "It is odd." }
number MOD 1 = 0
MODreturns the remainder when the first input is divided by the second. Whether a number is odd or even depends on divisibility by 2.
number MOD 1 = 1
MODreturns the remainder when the first input is divided by the second. Whether a number is odd or even depends on divisibility by 2.
number MOD 2 = 0
number MOD 2returns the remainder when
numberis divided by 2. If that remainder is zero, is the number odd or even?
number MOD 2 = 1
number MOD 2returns the remainder when
numberis divided by 2. If
number MOD 2is 1, then the number is odd.
Mod
gives you the remainder, not the quotient.
Open your U2L3-Dots project, and save it again as U2L4-DotsTIF. Remind yourself about how the pictures are determined by the Boolean expression used.