+ Reply to Thread
Results 1 to 3 of 3

Thread: Flash Variables and Operators

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Flash Variables and Operators

    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:

    Code:
    var sName:String = "";
    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.

    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:

    Code:
    var txtInput:TextField = new TextField();
    The constructor is called when you see a new keyword. We will look into objects later.

    Operators

    The addition operator simply adds two items together.

    Ex:

    Code:
    trace(String(5 + 3));
    The output is going to be 8.

    Subtraction

    The subtraction operator simply subtracts two values.

    Ex:

    Code:
    trace(String(5 - 3));
    Multiplication

    The multiplication operator simply multiples two values.

    Code:
    trace(String(5 * 3));
    Division

    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.

    Code:
    5  / 2
    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.


    Equality

    To test equality of variables, we use the == operator. The = operator is the assignment operator. If you do this:

    Code:
    if (test = 5) {
    	trace("test = 5.");
    } else {
    	trace("test != 5.");
    }
    You will always get test = 5 because the assignment operation will always return true.

    The way to test this is as follows:

    Code:
    if (test == 5) {
    	trace("test = 5.");
    } else {
    	trace("test != 5.");
    }
    This tests the equality of the test variable. Assuming that test is an integer variable.

    Inequality

    The inequality operator is !=.

    Example:

    Code:
    if (test != 5) {
    	trace("test != 5.");
    } else {
    	trace("test = 5.");
    }
    Greater than and less than operators

    These test if an operand is greater than or less than some other operand. One use of these is in sorting an array.

    Ex.

    Code:
    if (test < 5) {
    	trace("test < 5");
    } else if (test > 5) {
    	trace("test > 5");
    } else {
    	trace("Test must be equal to 5.");
    }
    Similary, we have greater than or equal to operators (>=) and less than or equal to operators (<=).

    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:

    Code:
    if (n % 2 == 0) {
    	return true;
    } else {
    	return false;
    }
    If when you divide by 2, there is no remainder the number is even. Otherwise, the number is not even.

    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Flash Variables and Operators

    This code looks just like C code (except the generic "var" keyword) .
    +rep!

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Flash Variables and Operators

    It's sort of a Pascal/C hybrid +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 10-27-2010, 10:15 PM
  2. Question about operators
    By DarkLordoftheMonkeys in forum C and C++
    Replies: 4
    Last Post: 01-28-2010, 07:59 PM
  3. Boolean Operators
    By chili5 in forum Java Tutorials
    Replies: 10
    Last Post: 01-13-2010, 03:31 PM
  4. setting variables in flash using visual basic 6
    By pitchfork_it in forum Visual Basic Programming
    Replies: 10
    Last Post: 05-29-2008, 12:07 PM
  5. Operators
    By clookid in forum PHP Tutorials
    Replies: 1
    Last Post: 01-11-2007, 06:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts