ppt - U of L Class Index

Computer Science 1000
Algorithms III

Multiple Inputs

suppose I ask you to write a program that computes
the area of a rectangle



area = length * width
in other words, we will have to read two values from
the user
how do we do this?

Multiple Inputs


our process seems correct, as the system is asking
the correct questions, but our output is wrong.
why?





the answer block can only hold one value
when we ask the first question, it reads the length, and
stores it in answer
when we read the width, it clobbers the value that was
read for length
hence, it is incorrectly computing area as width x width
to see this, we can watch what value is stored in answer
by checking the checkbox beside its block

Multiple Inputs

if answer can only hold one value at once,
how can we read in two values?
we need to store the first value somewhere
where it won’t get overwritten
 to do this, we will declare a variable


Variable



a variable is a named location in memory for storing
a value
simply stated, it is a place for our program to store
a value
to use a variable, we must first declare it


you must give your variable a name
it is helpful to use a name that you will remember, and that
is meaningful

Variable

to store a value in a variable, use the following
block:

in the right input, you can put a value, or an
expression
that value will be stored in the variable

Notice that the value changed.

Obtaining the value of a variable
the variable’s block can be used as the
input to another block
 modify our previous example so that the
sprite says what is stored by the variable.


Back to our example …
recall that in our previous example, we
could not store two inputs, as the second
input clobbered the first
 to mitigate this, let’s store our first input in a
variable called length
 let’s store the second input in a variable
called width


Variables
notice that the answer block and a variable
block behave very similarly
 in fact, answer is just a predefined variable,
with the exception that you cannot delete it


Boolean Expressions


like Excel, Scratch supports Boolean operators
two values



true
false
three operators



less than
equal
greater than

Write a Scratch program that asks the
user for their age, and prints true if that
user is old enough to vote in Canada,
and false otherwise

Boolean Operators

like Excel operators, we can use the
Boolean operators to compare strings

note that they are case-insensitive

Example: Write a program that asks for
the user’s name, and prints true if the
user has the same name as you, and
false otherwise.

Events and Booleans


you may have noticed that both the Boolean operators and
the event operators have the same hexagon shape
in fact, the two are very similar

the event operators can be considered to be true/false
statements



true when the event occurs
false otherwise
for example, consider the touching event

this event is true when Sprite 1 is touching Sprite 2, and false otherwise

Events and Booleans

because of their similarity, we can use
events in a similar way to Booleans
recall how we could print true and false from our
sprites using Boolean expressions
 write a program for Sprite 1 that prints true if it is
touching Sprite 2, and false otherwise


Events and Booleans
in some cases, we can use Booleans where
we previously used events
 example:


the y position block holds the vertical position of
the sprite


higher the sprite, greater than y
we can see the value in this block by checking
the box beside it

Events and Booleans

write a program where the sprite prints “Put
me down” whenever it is lifted above 50
units