Introduction to Variables

advertisement
Programming with App Inventor
Computing Institute for K-12 Teachers
Summer 2012 Workshop
Variables
Session SA-1
Introduction to Variables
 Variables are used in every aspect of programming. They
are used to store data the programmer needs later in the
program. In the App Inventor, a variable can store most
any data. In this workshop the following 3 data types will
be covered.
 Numbers – A number variable can contain almost any
value. (e.g. 1, 0.01, -5,+/- 10 million if not more)
 String (text) – A string variable can contain letters, words,
numbers, symbols, punctuation or sentences.(e.g. “s”,
“string”, “This is still a string”, “sessionSA-0”)
 Boolean – A Boolean variable contains a true or false value.
Introduction to Variables
 In the Blocks Editor, you may create a variable by clicking on the Built-In
tab on the left and then selecting Definition. (know as the Built-In
Definitions)
 This will give you the option of creating a variable (as well as Procedure,
Procedure Argument Names or a dummy stub – which will be explained
later)
 This is the block for a variable:
 The App Inventor will create a unique default name for the variable
(which you may change) and add the variable to your My Blocks, My
Definitions. These are your defined variables and procedures.
 To change the value of the variable, click on My Definitions and select the
set global block for your variable.
 All variables are global in scope, meaning they can be accessed from
anywhere in your program.
Introduction to Variables
 This shows the blocks the may be added from the Definition drawer under
the Built-In Tab. Variables and Procedures are added from here.
Procedure(s) will be covered later. Just click and drag.
Introduction to Variables
 This shows where several variables have been added to a project and then renamed
to varShowFirst (Boolean set to True), varFirstName (String set to George), varAge
(Number set to 57), etc. You cannot create two variables with the same name.
Boolean
String
Number
Introduction to Variables
 Once you add variables to you project, those variables and associated will be shown
when clicking on the My Blocks, then My Definitions drawer in the Blocks Editor. For
each variable two blocks are created, an assignment block and a set to block to
change the variable’s value.
assignment block
set to block
Introduction to Variables
 Variables can be used to manipulate data. Number variables can be
summed or multiplied. Strings can be joined, searched and formatted.
 Errors will arise if trying to multiply a number by a string, for example.
When using a predefined function, the App Inventor will not allow type
mismatches like this.
 Variables can be used in any of the App Inventor Built-In functions shown
in the Block Editor. The functions are used to manipulate strings,
numbers and Booleans (and also elements of a list that are of these three
types).
 Numbers can be referenced as number or as a string and will be treated
accordingly by their use.
Project SA-1 – Variables
 Create an app that changes the contents of two different
kinds of variables using a button click event to trigger the
change.
Download