summer computing workshop. introduction boolean expressions – in programming, a boolean...

19
PROGRAMMING WITH SCRATCH Summer Computing Workshop

Upload: rudolph-mitchell

Post on 04-Jan-2016

226 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

PROGRAMMING WITH SCRATCH

Summer Computing Workshop

Page 2: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Introduction

Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In Scratch, any block shaped like an elongated diamond is a Boolean expression. The three types of advanced Boolean operators we will discuss are not operator, and operator, and or operator.

Messages – Message passing is a very useful tool in Scratch. It is a way that sprites can specifically start a script by broadcasting a message, either a script belonging to itself or a script belonging to another sprite can be executed when it receives that message. This is the only way Scratch allows one sprite to run scripts in the other. Interesting enough, like stated before, it can also be used to run scripts from the same sprite that broadcasted the message.

Arrays – In programming, arrays are used to store multiple data of the same type in order to easily and quickly find and use the data later in the program. In Scratch, since there are no defined types, arrays are groups of variables that can contain both words and numbers.

Page 3: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Session 6

Page 4: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Boolean Review

Some basic Boolean expressions we discussed in Session 3 are:

The above operators return true or false and can be used in structures we covered earlier such as

There are three other commonly used Boolean operators that can be combined to create extremely flexible expressions.

Page 5: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Boolean Expressions

Now we will discuss other useful Boolean expressions and some examples of each.

This operator is used when two conditions need to be met in order to execute specified blocks. If the first condition is true and the second condition is true, then this operator returns true. If either of the conditions are false, the expression returns false. For example:

Since the 3 < 4 evaluates to true and 8 = 8 evaluates to true, then the predicate part of the if block evaluates to true and the sprite says “Hello!”

Page 6: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Boolean Expressions

In this example, 5 isn’t less than 4 so it evaluates to false. Even though one condition is true (8 = 8), using the and operator requires both conditions to return true

This operator is very similar to the and operator except it only requires one condition to be met.

Even though in this example 5 = 1 evaluates false, all the block needs in order to execute is one condition to evaluate true. The full expression evaluates true.

Page 7: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Boolean Expressions

This is another useful operator that changes true to false and false to true, for example

In the above example, because 5 is not equal to 3, and the not block was used, the expression evaluates to true. The not operator in the next example will show the if block not executing.

Because the expression 5 = 5 is true, using the not operator causes the predicate part of the if block to evaluate to false. The say block will not be executed. These Boolean operators can be used in the conditional part of a repeat until control structure but most commonly are found in the predicate part of an if control structure.

Page 8: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Message Passing

There are three blocks that are used in message passing. They are found in the control blocks category. Two of these blocks are used to broadcast messages and the other is used to receive messages.

One of the control blocks used for broadcasting messages is . By clicking the down arrow, we can create a new message to be broadcasted.

Another of the control blocks used for broadcasting messages is the

block. This is very similar to the broadcast block except the sprite that is broadcasting the message waits until the scripts it triggered have finished running. It then continues with the rest of the current script.

The final control block we will discuss is actually necessary in order to properly use the previously discussed control blocks. The allows a sprite to receive a specific message and respond accordingly.

Page 9: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Broadcasting/Receiving

Our example will require the Scratch cat and the bat. We will also introduce costume changing but not in great detail. We will build a script moving the bat towards the cat (changing costumes during the movement to make it more realistic). When the two sprites touch, the bat will broadcast a message and the cat will receive the message and execute the script written for it.

In order to utilize both bat costumes, click the costumes tab in the middle scripts area. If the other bat isn’t imported do this now. Now we should have two bat costumes that belong to one bat sprite. Now we will construct the example (see example 6.1 on the Constructed Examples page at the end of this section).

The same thing can be done with many sprites. Not all sprites have multiple images of themselves so some may not have any other costume choices.

Page 10: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Message Passing

For our next example, we will show the difference between the broadcast and broadcast and wait blocks.

We will be using the same example except we will take the broadcast block out and replace it with the broadcast and wait block.

What is the difference? Now, go into single stepping mode and notice what is being executed.

This could be very useful if we wanted a Sprite to broadcast a message, then continue with it’s script after the receiving Sprite is finished with it’s script.

Passing messages is a way two sprites can communicate. Its a way to start another Sprite’s script inside of a script.

Page 11: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Broadcasting/Receiving

Now we will discuss the only block in Scratch that allows another sprite or itself to receive messages that the previous sprite sent.

For this to work, the first sprite has to broadcast a message using the broadcast block in its scripts. The second sprite (which has different scripts) needs to have a when I receive block. This is the only block in Scratch that can actually receive messages, so the two previous ways of broadcasting messages are useless unless we have a sprite receiving the message broadcasted.

It is possible, however, to have the same sprite broadcast, receive its own broadcasted message, and execute scripts accordingly.

Ok, let’s construct this short example (see example 6.2 on the Constructed Examples page at the end of this section)

Page 12: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Project 6.2

Create a short animated story (similar to the project in session 2) that uses broadcasting and receiving to queue cinematic events (you needn’t be too detailed).

Page 13: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Arrays

Arrays are a very useful programming tool. As stated previously, Arrays can store multiple values that you may need later in your program. Creating an array is, in a way, like creating multiple variables. For example, if we were to have an array of length 5. That means we could have 5 different values held within the array. That doesn’t mean we can’t have 4 values within the array with one blank spot for a future value.

Each value in the array is indexed by a number. In programming, indices begin at zero so the first number is indexed with the number 0 and the second number is indexed with the number 1. In Scratch however, the creators make it more non-programmer friendly and start indexing at 1. Indexing is the way we keep track of exactly where a specific value is in the array.

The length of an array in programming is specified by the programmer. The Scratch creators make things more convenient for us by giving arrays the ability to dynamically change in size. Unlike arrays in other languages, arrays in Scratch have no definite size.

Page 14: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Creating an Array in Scratch

To create an array in Scratch, click on the section where we created variables in session 5. Then create a new list named groceryList. This will be our first example.

We add items to the list by using the block.

We delete items from the list by using the block. We do this by specifying the index of the item we want to delete. If the block above were executed, the first item of the list would be deleted because of the index 1. All other items are then shifted down by one; 2 becomes 1, 3 becomes 2, etc.

We can use the block to actually use an item from the list. Here are some other blocks in which it can be used.

Page 15: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Grocery List Example

This example only requires one sprite. We will continue asking the user to enter an item to add to the list until they enter done. After the user enters done, have the sprite say each item and delete that item. We will need a variable to hold our index value. Let’s construct this example.

Page 16: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Project 6.3

Modify Project 5.1 so that all the values entered by the user are stored in a list. When the user enters “done” the program goes through the array and calculates the average, then displays it along with the number of values entered by the user.

Page 17: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Constructed Examples

Example 6.1 Bat script

Example 6.1 Cat script

Page 18: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Constructed Examples

Example 6.2 cat script

Example 6.2 bat script

Page 19: Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In

Session 6 Questions

1. Which six Boolean operators do we cover?

2. What is message passing used for in Scratch?

3. Where are Boolean expressions most commonly used?

4. You have a program that has an if/else structure, and two variables, X and Y. The only time you want the alternate block(s) to execute is when both variables equal 10. What Boolean expression could you use in this situation?

5. In your own words, explain the purpose and structure of an array.

6. Explain the difference between the broadcast and broadcast and wait blocks.