So far we have dealt with statements which can be executed and properties which can hold values. Often we will want to perform a series of mathematical operations on some set of values. In programming, a sequence of values and mathematical symbols is known as an expression. Expressions and blocks that "fit" into expressions have two shapes: rectangles with rounded ends and rectangles with pointed ends:
The difference between the rounded rectangles and the pointed rectangles is that rounded rectangles return numbers and text strings. Number values are exactly what you would expect, anything from integers (0, 1, 2, etc) to real numbers (0.1, 3.5, etc). String values are sequences of symbols (a, b, A, B, 1, 2, @, *, etc) which are also known as characters in most programming languages. Pointed rectangles return Boolean values. Boolean values can be only 1 of 2 possibilities: True or False.
While single blocks that return values can be quite useful, they are often combined into expressions using operators. A value that is passed to an operator is called an operand. If an operator takes two operands then it is called a binary operator. The following are the binary numeric operators in Scratch:
Most of these operators like + and - are obviously familiar. * stands for multiply and / stands for divide. The mod operator is less common but often very useful. It returns the remainder of a division. In other words 11 mod 4 is 3 because 11 is divisible by 4 twice with remainder of 3. You can see this in Scratch by placing two numbers in the less than operator's blank boxes and then double clicking on the operator.
The following blocks also "operate" on numbers in a sense but we have separated them into a different category because in most programming languages these are called functions:
The random function picks a number randomly between two values (1 and 10 at the moment). The round function rounds a number to the nearest integer. The sqrt takes the square root of a number as it is specified now, but it has a drop-down menu that allows the selection of several possible mathematical functions like sine, cosine, natural log, etc.
Another subtle difference between operators and functions is that the blank boxes in operator blocks are operands while the blank boxes in function blocks are arguments. There is absolutely no difference in how operands and arguments behave in Scratch, but you should be familiar with these terms because they are used extensively in discussing programming languages.
Excercise 2-1. Compute the values of the following expressions by entering values into the blocks on Scratch and then double-clicking the blocks:
16 mod 3 = 1 (15 is evenly divisible by 3 and 16 is 1 plus 15)
sqrt 72
8.49
Use the block that says sqrt and change the function to log. Then enter a 0 in the box (the argument). The solution is log 1000 = 3
The following are the 3 string functions in Scratch.
The first function joins two strings together. To join multiple strings, simply place more joins inside of joins:
The second function returns a single character from a certain position in a string. The first character is given the index 1 (although this may seem obvious, in some languages the first character is given the index 0). The third function returns the length of a string.
Relational operators link numbers and strings to Boolean values (values that are only True or False). The following are the relational operators equals, less than, and greater than:
Relational operators can take number arguments or string arguments. For numbers, the operator returns True if the relation is valid for two numbers and False if it is not. For example, 2 < 3 returns True but 4 < 3 returns False. You can see this in Scratch by placing two numbers in the less than operator's blank boxes and then double clicking on the operator. The Boolean operators in scratch are AND, OR, and NOT.
The AND operator returns True if and only if both operands are True:
True AND True => True
True AND False => False
False AND True => False
False AND False => False
The OR operator returns True if at least one operand is True:
True OR True => True
True OR False => True
False OR True => True
False OR False => False
The NOT operator simply negates the Boolean value:
NOT True => False
NOT False => True
Excercise 2-2. Build the following expressions in Scratch within the Scripts panel and determine the value returned by double clicking on the final result.
Simply drop the mod operator onto the Scripts panel and enter 5 and 2 in the blanks. An important thing to note about this expression is that mod can be used to determine if a number is even or odd. If a number is even it is always evenly divisible by 2 so mod will return 0 and if it is odd mod will return 1. You can try this out with different values in place of 5.
Return a random number between -10 and 10.
Simply drop the pick random block onto Scripts and enter -10 and 10.
Return a random number between -1 and 1 in which the values are in 10ths (i.e. 0.5, -0.3, etc).
First place the division block on Scripts. Then place the pick random block with values -10 and 10 into the first operand box and place the number 10 in the second. In other words, we pick a random number between -10 and 10 and then divide by 10 so that it is between -1 and 1 with values in 10ths.
Return a string that starts with the random number from the previous exercise ends with " is a random number."
Place the join function in Scripts. Drag the random block from the previous exercise into the first operand of join. Type the string " is a random number." in the second operand of join.
Simply use the relational operators and the boolean operators. The result is of course True.