2. What is Global Variable?
ï” A global variable is a variable that can be accessed in multiple scopes. This means that wherever you are in the
program you can use that variable; get its current value or set its value to something else. Global variables are created
using the initialize global name to block found in the Variables drawer.
3. What is Local Variable?
ï” A local variable is a variable that is declared within a function or it is an argument passed into a function. This means
that you can only access these variables in that specific function where they are declared or passed in as an argument.
4. Variables Overview
ï” The Variables drawer provides blocks to create, get, and set both global and local variables.
Explanations of all of the blocks available in this drawer can be found on the Variables page.
6. âą initialize global name to
âą get
âą set
âą initialize local name to in (do)
âą initialize local name to in (return)
7. âinitialize global name toâ
This block is used to create global variables. It takes in any type of value as an
argument. Clicking on name will change the name of this global variable. Global
variables are used in all procedures or events so this block will stand alone.
Global variables can be changed while an app is running and can be referred to
and changed from any part of the app even within procedures and event handlers.
You can rename this block at any time and any associated blocks referring to the
old name will be updated automatically.
9. âsetâ
This block follows the same rules as âgetâ. Only variables in scope will be
available in the dropdown. Once a variable v is selected, you can attach a
block to give v a new value.
10. âinitialize local name to - in (do)â
ï” This block is a mutator that allows you to create new variables that are only used in the procedure you run in the DO part of the
block. This way all variables in this procedure will all start with the same value each time the procedure is run. NOTE: This
block differs from the block described below because it is a DO block. You can attach statements to it. Statements do things.
That is why this block has space inside for statement blocks to be attached.
ï” You can rename the variables in this block at any time and any corresponding blocks elsewhere in your program that refer to
the old name will be updated automatically
11. âinitialize local name to - in (return)â
ï” This block is a mutator that allows you to create new variables that are only used in the procedure you run in the RETURN
part of the block. This way all variables in this procedure will all start with the same value each time the procedure is run.
NOTE: This block differs from the block described above because it is a RETURN block. You can attach expressions to it.
Expressions return a value. That is why this block has a socket for plugging in expressions.
ï” You can rename the variables in this block at any time and any corresponding blocks elsewhere in your program that refer
to the old name will be updated automatically
12. Global Variable Example
Take out a get block and click the dropdown. There will be no variables to select. Create a global variable and
name it count and initialize it to 0. Click on the dropdown of the get block. You can now see count available to
choose. Once you have created a global variable, it will always be available in the dropdown of get .
13. Local Variable Example
Create a local variable using initialize local name to in do block and name it a. Now drag out a set block and put it outside of
the initialize local name to in do block. Click on the dropdown of the set block. You will not see a as a choice in the dropdown.
This is because the set block is out of the scope of the local variable's domain. Move the block inside of the do part of
the initialize local name to in do block. Click the dropdown of the set block. Now a is available to choose.
14. Variable Labels
ï” Notice how when you use a get or set block for a global variable. The block will
say global name.
ï” When using get or set blocks for local variables, the block will only say name. Remember
that local variables include variables created from arguments to procedures or event
handlers, variables created for use in for loops, or initializing local variables for an
expression or return statement by using the orange local variable initialize blocks.
15. Why would I ever need to use local variables?
Sometimes you may need to create a new variable within a procedure and only want that procedure
to be able to use it.
In this example, we use a local variable, height, to store the height of the triangle whose hypotenuse
and base we're given as arguments. We might have a different procedure that uses height as an
argument so we would not be able to use a global variable. To make sure of that, we use local
variables so that the height in the right context is only available in the procedure where it is used.
Sometimes it might just be easier to have local variables rather than creating many new global ones.
Or it might allow us to use less blocks.
17. âif & else ifâ
Tests a given condition. If the condition is true, performs the
actions in a given sequence of blocks; otherwise, the blocks
are ignored.
Tests a given condition. If the condition is true, performs the
actions in the -then sequence of blocks; otherwise, performs
the actions in the -else sequence of blocks.
Tests a given condition. If the result is true, performs the actions in the
-then sequence of blocks; otherwise tests the statement in the -else if
section. If the result is true, performs the actions in the -then sequence
of blocks; otherwise, performs the actions in the -else sequence of
blocks.
18. âfor each number from toâ
Runs the blocks in the do section for each numeric value in the range starting from from and ending at to,
incrementing number by the value of by each time. Use the given variable name, number, to refer to the current value.
You can change the name number to something else if you wish.
19. âfor each item in listâ
Runs the blocks in the do section for each item in the list. Use the given variable name, item, to refer to the current list
item. You can change the name item to something else if you wish.
20. âfor each key with value in dictionaryâ
Runs the blocks in the do section for each key-value entry in the dictionary. Use the given variables, key and value, to refer to the key
and value of the current dictionary entry. You can change the names key and value to something else if you wish.
21. âwhileâ
Tests the -test condition. If true, performs the action given in -do , then tests again. When test is false, the block ends and the
action given in -do is no longer performed.
if then else
Tests a given condition. If the statement is true, performs the actions in the then-return sequence of blocks and returns the then-
return value; otherwise, performs the actions in the else-return sequence of blocks and returns the else-return value. This block is
similar to the ternary operator (?:) found in some languages.
22. âdo with resultâ
Sometimes in a procedure or another block of code, you may need to do something and return something,
but for various reasons you may choose to use this block instead of creating a new procedure.
23. âevaluate but ignore resultâ
Provides a âdummy socketâ for fitting a block that has a plug on its left into a place where there is no
socket, such as one of the sequence of blocks in the do part of a procedure or an if block. The block you
fit in will be run, but its returned result will be ignored. This can be useful if you define a procedure that
returns a result, but want to call it in a context that does not accept a result.
24. open another screen
Opens the screen with the provided name.
The screenName must be one of the Screens created using the Designer. The screenName should be
selected from the connected screen name dropdown block.
If you do open another screen, you should close it when returning to your main screen to free system
memory. Failure to close a screen upon leaving it will eventually lead to memory problems.
App developers should never close Screen1 or use this block to return to Screen1. Use the close screen block
instead.
25. open another screen with start value
Opens another screen and passes a value to it.
26. get plain start text
get start value
Returns the plain text that was passed to this screen when it was started by another app. If no value was passed, it returns the
empty text. For multiple screen apps, use get start value rather than get plain start text.
Returns the start value given to the current screen.
This value is given from using open another screen with start value or close screen with value.
27. close screen
close screen with plain text
close screen with value
close application
Closes the current screen.
Closes the current screen and passes text to the app that opened this one. This command is for returning text to non-App
Inventor activities, not to App Inventor screens. For App Inventor Screens, as in multiple screen apps, use close screen
with value, not close screen with plain text.
Closes the current screen and returns a value to the screen that
opened this one.
Closes the application.
28. break
When looping using the for range, for each, or while blocks it is sometimes useful to be able to exit the loop early.
The break allows you to escape the loop. When executed, this will exit the loop and continue the app with the statements
that occur after the loop in the blocks.