160 likes | 168 Views
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 from a user in scratch How to display information in scratch. Computer Memory.
E N D
Section 2Variables National 4/5 Scratch Course
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
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.
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.
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)
Numeric • is used for calculations. • If you need to use a piece of data in a calculation you should use a numeric variable.
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
Scratch Variables 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 • Click on the variables tab • Click “Make a variable” • Type the name you want to use for the variable
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. As long as they program it in! Hi Standard grade the sprites will say anything you want them to!
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 • What does it do? • How can we make it say “Hello” before Scratch asks the question and when it say’s the user’s name?
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.
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”
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.
Review Questions Copy and answer the following in your jotter • What is a variable? • Where variables are stored? • What command do we use in scratch to get data from a user? • What command do we use in scratch to display information?
Tasks Create scratch programs to do the following (save each in your network area). • Display a users name and age in a single sentence. • Display a users full name separately. • Display a message from the user on the screen.