Using Variables
So, we have variables and we know how to create them but how can we actually use them and for what? We agree, it's time to see them in action.
The earlier videos explained what a variable is, but the discussions were brief and limited to initial declarations. Now, we will see how to use variables in the context of Flash. Basically, a variable doesn't belong to any specific data type. We can assign any type of data to a variable but we must ensure that the variable is declared initially using the same data type.
The most important feature of variables which makes them so popular is their re-usability. Variables are just like containers in which anything can be saved. For example, imagine that we need to perform multiplication on 3 sets of numbers containing 3 integers each: [(1,2,3)(2,3,4)(7,8,9)]. Now to multiply these we need to write the trace line for each set.
Instead, if we use the variables (a,b,c ) and assign the first set of values to these variables then we can perform the multiplication of the first set using abc. Now, for the multiplication of the second set, nothing much changes but we assign the values of the second set to a,b and c. This way we can reuse the variables any number of times and reduce the length of the code.
Instead, if the values that are being operated are continuous numbers, then we can use the property of incrementing and looping with the help of variables which reduces the bulk of the code to a single loop. When there are many values to be calculated, using variables is the best option.
The use of variables is not just limited to numbers; strings can be assigned to them as well. We assign a string to a variable at the beginning and can perform any needed modifications on it. Later, if we want another string to be used instead of the present string, there is no need to rewrite the whole program. We just change the string that is assigned to the variable. Moreover, addition of strings seems difficult when we output the sum directly. Using variables for the addition of strings is the best option.