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:

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

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 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

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.

Another subtle difference between operators and functions is that the blank boxes in operator blocks are

Compute the values of the following expressions by entering values into the blocks on Scratch and then double-clicking the blocks:

- 16 mod 3
- sqrt 72
- log 1000
- sin 90

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 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

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

Build the following expressions in Scratch within the Scripts panel and determine the value returned by double clicking on the final result.

- 5 mod 2
- Return a random number between -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).
- Return a string that starts with the random number from the previous exercise ends with " is a random number."
- (3 < 4) and (2 > -1)

Freetext © CC:BY-SA-NC · Contact