Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a...

16
Section 2 Variables National 4/5 Scratch Course

Transcript of Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a...

Page 1: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Section 2Variables

National 4/5

Scratch Course

Page 2: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

What you should know after this lesson

• What a variable is

• Where variables are stored

• How to get data from a user in scratch

• How to display information in scratch

Page 3: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Computer Memory

• A computer program needs to store all the information it is working with in the computers RAM. It does this using memory addresses, these addresses are numbers and because a computer is very good with numbers, this is fast and efficient.

Page 4: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Storing things in memory

• Humans are not good at remembering lots of number (How many phone numbers can you remember without using your mobile?).

• So to help humans, high level languages use variables to name the addresses.

Page 5: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Variables

These are storage locations in RAM, controlled by the programming language.

What types of data might we need to use with our programs?

The main ones we will be using are– Numeric (numbers)– String (text)

Page 6: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Numeric

• is used for calculations.

• If you need to use a piece of data in a calculation you should use a numeric variable.

Page 7: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

String

• These are normally letters and other non numeric characters.

• Numbers can be stored as strings if they are not used in calculations.– Phone numbers– House numbers

Page 8: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Scratch Variables

1. Click on the variables tab

2. Click “Make a variable”

3. Type the name you want to use for the variable

The scratch language is able to work out the type of variable we are using and as a result it only has the one type of variable

Page 9: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Displaying Variable

• We can get any sprite to speak by using the say block from inside looks.

• Variables can be displayed on the stage or hidden. It is often better to hide variables at the start of a program.

Hi Standard grade the sprites will say anything

you want them to!

As long as they program it in!

Page 10: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Using a String Variable

• Copy the program show on the right into your jotter, with the heading.

• Enter the program into scratch

• Remember to make the variable name

Answer these questions in your jotter

1. What does it do?2. How can we make it say

“Hello” before Scratch asks the question and when it say’s the user’s name?

Page 11: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Copy this into your jotter

Edit your program in scratch to make it the same as above.

Write a description of what join does in your jotter.

Page 12: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Concatenation (Join)

• Programmers often require to join two or more strings of text together, this is called concatenation. It is most often used when displaying text but can also be used at other times.

For Example The text string “Hello Bob Roberts”

can also be written as “Hello” & “ “ & “Bob” & “ “ & “Roberts”

Page 13: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

More than one variable

• Programs can make use of lots of variables. Just make a new variable in the variable tab

• Programmers use sensible names to keep track of what variable does what.

• Answer only stores one value at a time so remember to set a variable to answer after using ask.

• Remember the sequence of a program is very important, so make sure you think about what you want the program to do.

Page 14: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

An example of two variables

Page 15: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Review Questions

Copy and answer the following in your jotter

1. What is a variable?

2. Where variables are stored?

3. What command do we use in scratch to get data from a user?

4. What command do we use in scratch to display information?

Page 16: Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.

Tasks

Create scratch programs to do the following (save each in your network area).

1. Display a users name and age in a single sentence.

2. Display a users full name separately.

3. Display a message from the user on the screen.