Scr0340: Arithmetic Operators in Scratch 2.0: R.G. (Dick) Baldwin
Scr0340: Arithmetic Operators in Scratch 2.0: R.G. (Dick) Baldwin
Abstract
The purpose of this module is to teach you about operators and operands in general and arithmetic
operators in particular. You will also learn about expressions and statements, and you will learn how to
write a Scratch program that illustrates the use of arithmetic operators in Scratch.
1 Table of Contents
• Preface (p. 2)
· Viewing tip (p. 2)
* Images (p. 2)
• General background information (p. 3)
Operators (p. 3)
·
Operands (p. 3)
·
Expressions (p. 3)
·
Statements (p. 3)
·
A brief word about type (p. 4)
·
Unary, binary, and ternary operators (p. 4)
·
* Some operators can be either unary or binary (p. 4)
* The minus character as a unary operator (p. 4)
* Binary operators use inx notation (p. 4)
· General behavior of an operator (p. 5)
· Operator categories (p. 5)
• Preview (p. 5)
• Discussion and sample code (p. 5)
· Four variables (p. 6)
· A button (p. 7)
· Two scripts in the rightmost panel (p. 7)
· Variables with sliders (p. 7)
* How to create a slider (p. 7)
* Version 1.1: May 13, 2013 11:52 am +0000
http://creativecommons.org/licenses/by/3.0/
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 2
2 Preface
Scratch 2.0 1 (released May 9, 2013) is the second major version of Scratch to be released during the life of
the product. Among other things, it features a redesigned editor and website, and allows you to edit projects
directly from your web browser.
This module is one in a collection of modules designed to help beginners of all ages (8 and up) learn
how to create the code for computer programs using Scratch 2.0 2 . Information is provided not only for the
beginners themselves but also for their parents and teachers where appropriate.
The purpose of this module is to teach you about operators and operands in general and arithmetic
operators in particular. You will also learn about expressions and statements, and you will learn how to
write a Scratch 3 program that illustrates the use of arithmetic operators in Scratch.
I will also provide the specications for a student-programming project for you to complete in order to
demonstrate your understanding of what you learned from the rst program.
2.1.1 Images
• Image 1 (p. 6) . Reduced screen shot of Arithmetic01 programming interface.
1 http://scratch.mit.edu/
2 http://scratch.mit.edu/
3 http://scratch.mit.edu/
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 3
3.1 Operators
Operators are the action elements of a computer program. They perform actions such as adding two variables,
dividing one variable by another variable, comparing one variable to another variable, etc.
3.2 Operands
According to the current jargon, operators operate on operands .
For example, in the following expression, the plus character is an operator while x and y are operands.
x+y
Assuming that x and y are numeric variables, this expression produces the sum of the values stored
in the variables named x and y . The variable x would be called the left operand and the variable y
would be called the right operand .
3.3 Expressions
Computer programs in many languages consist of statements, which in turn, consist of expressions. (Pro-
gramming blocks substitute for statements in Scratch.)
An expression is a specic combination of operators and operands that evaluates to a particular
result. The operands can be variables, literals, or method calls. (Note that Scratch v1.4 didn't
support methods. However, Scratch 2.0 allows you to design and create your own blocks, which
helps to alleviate that deciency.)
In your past experience, you may have referred to expressions by the names formulas or equations .
Although formulas and equations are not exactly the same thing as expressions, they are close enough to
help you understand what expressions are and how they are used.
3.4 Statements
A statement is a specic combination of expressions. The following is an example of a statement comprised
of expressions in a text-based language such as Java, C++, or C#.
z = x + y;
Operationally, values are retrieved from the variables named x and y in the above statement. These
two values are added together. The result is stored in (assigned to) the variable named z , replacing
whatever value may previously have been contained in that variable.
The plus character (+) would commonly be called the addition operator or the concatenation operator,
depending on the type of data stored in the variables named x and y . The equal character (=)
would commonly be called the assignment operator in Java, Alice, C#, and C++, but we will see in a future
module that it is also used as a relational operator in Scratch.
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 4
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 5
4 Preview
In this module, I will present and explain a Scratch program named Arithmetic01 . This program
illustrates the use of the following arithmetic operators (see the bottom script in the rightmost panel of
Image 4 (p. 13) ):
• + (addition)
• - (subtraction)
Variables with the following names are created and displayed on the stage (see Image 5 (p. 14) ):
• LeftOperand - has a slider
• RightOperand - has a slider
• Sum
• Di
In addition, a button is displayed on the stage.
The user adjusts the values of the LeftOperand and RightOperand variables with a pair of sliders.
When the user clicks the button, the variable named Sum displays the sum of the values of the left and
right operands. The variable named Di displays the result of subtracting the right operand from the left
operand.
I'm going to walk you through the steps required to develop this program, being brief on those things that
you already know about and being more verbose on the new material.
To begin with, Image 1 (p. 6) shows a reduced screen shot of the online programming interface in the
browser window. Note that the source code in the rightmost panel was reduced in size to help it t into
this presentation format. The stage was also switched to the small format by clicking the little triangle
immediately below the stage.
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 6
Image 1 (p. 6) lacks detail due to the requirement to reduce the size of the image for publication in this
format, but there are some aspects of Image 1 (p. 6) that can be seen.
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 7
5.2 A button
Skipping to the area immediately below the Stage, you can see that a button sprite has been added to the
program. That button has been selected in Image 1 (p. 6) , making it possible to drag blocks from the
toolboxes into the rightmost panel to control the behavior of the button.
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 8
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 9
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 10
http://cnx.org/content/m46239/1.1/
Figure 3: Image 3. Preparing to use the arithmetic operators.
OpenStax-CNX module: m46239 11
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 12
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 13
Image 4. Result of dropping addition and subtraction operators into variable blocks.
http://cnx.org/content/m46239/1.1/
Figure 4: Image 4. Result of dropping addition and subtraction operators into variable blocks.
OpenStax-CNX module: m46239 14
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 15
Then you can move the sliders back and forth to manually set the values for the variables named Left-
Operand and RightOperand .
If you click immediately to the right or left of the thumb on a slider, that will cause the thumb
to move a small distance in that direction.
Then when you click the button at the bottom of Image 5 (p. 14) , the variable named Sum will display
the sum of the values of the top two variables, and the variable named Di will display the value of
LeftOperand minus the value of RightOperand .
5.12 An online version of this program is available
A copy of this program has been posted online for your review (see Resources (p. 18) for the URL) . If
you don't nd the program using that URL, search the Scratch site for the user named dbal.
I encourage you to use the information provided above to write this program. Experiment with the code,
making changes, and observing the results of your changes. Make certain that you can explain why your
changes behave as they do.
Just for fun, move the button a little to the right and cause it to say Ouch for about ve seconds each
time you click it as shown in Image 6 (p. 16) . Hint: See the purple button labeled Looks in Image 3 (p.
10) .
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 16
Write a Scratch program named Arithmetic02 that produces the output shown in Image 7 (p. 17) when
the user adjusts the sliders to the values shown and clicks the button. Make the word Ouch! appear and
then go away after about ve seconds.
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 17
A copy of this program has been posted online for your review (see Resources (p. 18) for the URL) . If
you don't nd the program using that URL, search the Scratch site for the user named dbal .
8 Summary
I began by teaching you about operators and operands in general. I also taught you about expressions
and statements . I gave a very brief introduction of the concept of type .
I presented and explained a sample Scratch program that illustrates how to use arithmetic operators in
Scratch.
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 18
Finally, I provided the specications for a student-programming project for you to write in order to
demonstrate your understanding of what you learned from the rst program.
Copies of both programs have been posted online for your review (see Resources (p. 18) for the URL)
. If you don't nd the program using that URL, search the Scratch site for the user named dbal.
9 What's next?
This module concentrated on arithmetic operators. The next few modules will deal with relational and
logical operators as well as selection and loops.
10 Resources
• Scratch home 4
• Scratch tutorials 5
• Scratch explanatory video 6
• Scratch tutorials from MIT 7
• Scratch tutorial - Digital Art for All 8
• Scratch for Budding Computer Scientists 9
• Learn Scratch at learnscratch.org 10
• Scratch Tutorial - Space Shuttle Mission STS-2020 11
• SCRATCH TUTORIALS ICT In Primary Education 2012-2013 12
• Scratch Wiki 13
• Scratch WikiTable of ContentsWebsite - Scratch Wiki 14
• Scratch WikiTable of ContentsTutorials - Scratch Wiki 15
• Scratch WikiTable of ContentsProgram - Scratch Wiki 16
• Tutorial about Variables - Scratch Wiki 17
• Scratch support MIT web site 18
• Scratch resources at Scratch.ie 19
• ScratchEd Resources 20
• Scratch project Day Dream 21
• Scratch project Son of String Art 22
• Scratch Project Scratch Tutorial 23
• Variable01 24 - Online version of program
4 http://scratch.mit.edu/
5 http://mit.edu/∼sil/www/scratch/
6 http://www.code.org/learn/scratch
7 http://info.scratch.mit.edu/Video_Tutorials
8 http://www.digitalartforall.com/category/scratchtutorials/
9 http://cs.harvard.edu/malan/scratch/
10 http://learnscratch.org/
11 http://scratched.media.mit.edu/resources/scratch-tutorial-space-shuttle-mission-sts-2020
12 http://ictinprimary.webnode.es/scracth-tutorials/
13 http://wiki.scratch.mit.edu/wiki/Main_Page
14 http://wiki.scratch.mit.edu/wiki/Scratch_Wiki:Table_of_Contents/Website
15 http://wiki.scratch.mit.edu/wiki/Scratch_Wiki:Table_of_Contents/Tutorials
16 http://wiki.scratch.mit.edu/wiki/Scratch_Wiki:Table_of_Contents/Program
17 http://wiki.scratch.mit.edu/wiki/Tutorial_about_Variables
18 http://info.scratch.mit.edu/Support
19 http://www.scratch.ie/resources
20 http://scratched.media.mit.edu/resources
21 http://scratch.mit.edu/projects/cremeglace/40150
22 http://scratch.mit.edu/projects/Paddle2See/89023
23 http://scratch.mit.edu/projects/turkey3/2576871
24 http://scratch.mit.edu/projects/dbal/154446
http://cnx.org/content/m46239/1.1/
OpenStax-CNX module: m46239 19
11 Miscellaneous
note: Disclaimers: Financial : Although the Connexions site makes it possible for you to
download a PDF le for this module at no charge, and also makes it possible for you to purchase a
pre-printed version of the PDF le, you should be aware that some of the HTML elements in this
module may not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
25 http://scratch.mit.edu/projects/dbal/154452
26 http://scratch.mit.edu/projects/dbal/154457
27 http://scratch.mit.edu/projects/dbal/155966
28 http://scratch.mit.edu/projects/dbal/155971
29 http://scratch.mit.edu/projects/dbal/156614
30 http://scratch.mit.edu/projects/dbal/156618
http://cnx.org/content/m46239/1.1/