Flash Variables and Operators
This is a simple concept but it is really important. Programming without variables and operators is useless.
Declaring Variables
Before we look at operators, we are going to look at how to declare variables. We use the var statement to declare variables. We have to provide the variable a name, and follow the variable name with a data type.
Example:
After the var line you provide the name followed be a semi-colon. After the semi-colon you specify the data type. Optionally, you can specify a default value for the variable. In the above line I create a String variable called sName and it is defaulted to an empty string.Code:var sName:String = "";
Some other data types include:
int - An integer variable. You can use them to perform math but they can not contain a decimal value. Ex. 5, 3, 2, 4. Not an int: 5.5
Number - The floating point variable. Ex. 5.5, 3.5, 4.2
boolean - A true or false value.
Creating instance of objects is a little different. You must call the constructor to initalize an object. A constructor is a special function in a class that is used to set up an object.
Example:
The constructor is called when you see a new keyword. We will look into objects later.Code:var txtInput:TextField = new TextField();
Operators
The addition operator simply adds two items together.
Ex:
The output is going to be 8.Code:trace(String(5 + 3));
Subtraction
The subtraction operator simply subtracts two values.
Ex:
MultiplicationCode:trace(String(5 - 3));
The multiplication operator simply multiples two values.
DivisionCode:trace(String(5 * 3));
The division operator allows you to divide two values. If you divide two integers any floating point value will be truncated. This makes division great for us in accessing items in arrays.
What is the result? 2.5? Wrong. It is 2. Since 5 and 2 are integers, this is integer division and there is no decimal value. If this was 5.0 / 2.0 the result would be 2.5. Since there is a decimal value.Code:5 / 2
Equality
To test equality of variables, we use the == operator. The = operator is the assignment operator. If you do this:
You will always get test = 5 because the assignment operation will always return true.Code:if (test = 5) { trace("test = 5."); } else { trace("test != 5."); }
The way to test this is as follows:
This tests the equality of the test variable. Assuming that test is an integer variable.Code:if (test == 5) { trace("test = 5."); } else { trace("test != 5."); }
Inequality
The inequality operator is !=.
Example:
Greater than and less than operatorsCode:if (test != 5) { trace("test != 5."); } else { trace("test = 5."); }
These test if an operand is greater than or less than some other operand. One use of these is in sorting an array.
Ex.
Similary, we have greater than or equal to operators (>=) and less than or equal to operators (<=).Code:if (test < 5) { trace("test < 5"); } else if (test > 5) { trace("test > 5"); } else { trace("Test must be equal to 5."); }
Modulus operator
The modulus operator returns the remainder of a division. If you divide 5 by 2, this is integer division and the result is 2. However, there is 1 remaining.
This operator allows you to test the divisiblity of a number. This might be useful in determine if a number is prime or not.
Example:
If when you divide by 2, there is no remainder the number is even. Otherwise, the number is not even.Code:if (n % 2 == 0) { return true; } else { return false; }
This tutorial is simple, but the concepts of variables and operators is really important.
Last edited by chili5; 08-30-2009 at 03:14 AM.
This code looks just like C code (except the generic "var" keyword) .
+rep!
It's sort of a Pascal/C hybrid+rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks