We had covered some ground and some are still unsure about what the blocks do and mean. So in the next few days we will be looking at some of these and what they do. We will start with the Orange blocks at the top. These are variable blocks.
TYPES OF VARIABLES
Think of variables as place holders and these variables can be used to temporarily hold numerous values. There are four types of variables:
1. Boolean
2. Number Variable
3. String variable
4. Map variable
We'll cover Boolean Variables later. Today we'll cover Number, string and map variables.
BOOLEAN VARIABLES
A boolean is a data type that has only two possible values. For example a user's can be logged in or logged out. So the value of that boolean can only either be true or false. In the image above I have created a boolean variable called isRunning. I can use this to determine when the app is running by setting the value to true when the app starts(onStart). then I can also specify what happens when the app is running.
NUMBER VARIABLES
A Number variable only holds numerical values and is useful for performing calculations. Number variables can be easily identified using their oval shape. Remember back in high school when you were doing equations and were given something like x + y = z? Well, think of a variable as a placeholder similar to x in that equation. For example, if we are creating an app that adds 2 numbers, we need to allow the app the flexibility to function the same regardless what the user types into the field. So instead of calculating using a fixed number like 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 we replace the numbers with a variable whose value can change. We can give this variable the name x. That way even if x changes, the result still proves true and meets any set rules.
So in our app, instead of saying _2 + 4 = 6_ we would convert whatever text we receive in our edittext to a variable. We can give these variables simple names like:
a, b, c
num1, num2, num3
day1sales, day2sales, day3sales
This will make it easier to make calculations. So it will then look like this in our logic:
a + b = c
num1 + num2 = total
day1sales + day2sales = salestotal
When you add a number variable in Sketchware, in addition to the variable, 3 new blocks are added. These are
Set Number to- this sets the value of that specific variable to whatever number is placed in the blank oval. This can be another variable, a calculation or any number. PLEASE NOTE THAT THIS BLOCK DOES NOT DISPLAY A NUMBER. TO DISPLAY A NUMBER YOU WILL STILL NEED TO SET TEXTVIEW TO THE VALUE OF THAT VARIABLE.
Increase number by 1- as the name states, this block increases the number variable
Decrease number by 1- this block decreases your number variable by 1
STRING VARIABLES
Variables can also be used to replace alphanumeric text. String variables are rectangular in shape. This can be used in instances whereby the user enters lots of temporary data but each with different values. For example in a camera app, you can use string variables to store image names. Instead of you creating a list of individual names, you can use a string variable to collect time and location whenever a picture is taken and use these as the image name.
MAP VARIABLES
These are used to store data which contains multiple fields of data at each key. If you are creating a chat app, you will want to collect *date, time, message, user ID.* so instead of recording these and storing them in a list of textviews at each point a message is sent, you can store it temporarily in a map variable.
Comments