Variables & Data types Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer

Slides:



Advertisements
Similar presentations
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
Data Types.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in computer memory are called variables The values, or data,
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CSC 107 – Programming For Science. The Week’s Goal.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
1.2 Primitive Data Types and Variables
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Programming in AS3. AS3 vs MXML MXML is content/structure AS3 ties in with the MXML to create the a functioning program.
Variables & datatypes Flash ActionScript Introduction to.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Modern JavaScript Develop And Design Instructor’s Notes Chapter 4 – Simple Variable Types Modern JavaScript Design And Develop Copyright © 2012 by Larry.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Java Variables and Types
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
IDENTIFIERS CSC 111.
Chapter 2: Basic Elements of Java
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Primitive Data Types and Operations
Variables and Constants
Presentation transcript:

Variables & Data types Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer

Variables A variable is a place to store information A variable is a place to store information It has a name and a type It has a name and a type Variables are used to make the code dynamic Variables are used to make the code dynamic Values can generally be accessed or changed at any time Values can generally be accessed or changed at any time An identifier (usually a letter, word, or phrase) that is linked to a value stored in the system's memory or an expression that can be evaluated An identifier (usually a letter, word, or phrase) that is linked to a value stored in the system's memory or an expression that can be evaluated Depending on the type system of a programming language, variables may only be able to store a specified data type Depending on the type system of a programming language, variables may only be able to store a specified data type

Naming variables Variable names can only contain letters, numbers, and dollar signs ($) Variable names can only contain letters, numbers, and dollar signs ($) All variables must have unique names All variables must have unique names Start variables with a lowercase letter Start variables with a lowercase letter Variables are case-sensitive Variables are case-sensitive Use mixed case for concatenated words Use mixed case for concatenated words Don't use reserved words: this, menu, private, video, etc. Don't use reserved words: this, menu, private, video, etc. Don't use the same variable name with different cases Don't use the same variable name with different cases Keep variables as short as possible while retaining clarity Keep variables as short as possible while retaining clarity Example, with strict datatyping: var xSpeed:Number;

Data types The Data type defines the type of data a variable or ActionScript element can hold The Data type defines the type of data a variable or ActionScript element can hold Primitive data types (Top level data types: Primitive data types (Top level data types):  Boolean, int, Null, Number, String, uint, and void Complex data types: Complex data types:  Object, Array, Date, Error, Function, RegExp, XML, and XMLList

Variable syntax Example of the different parts and structure of a variable declaration/population Example of the different parts and structure of a variable declaration/population

Data type: String (1/2) Strings are sequences of characters, numbers and punctuation marks. These are enclosed within double (") quotation marks Strings are sequences of characters, numbers and punctuation marks. These are enclosed within double (") quotation marks The String data type represents a sequence of 16-bit characters The String data type represents a sequence of 16-bit characters The default value for a variable declared with the String data type is null The default value for a variable declared with the String data type is null//declaration var myURL_string:String; //assignment myURL_string = " //declaration and assignment var myURL_string:String = "

Example of some methods for the String object are: Example of some methods for the String object are: Substring, charAt, replace, toUpperCase, split, join etc //declare variable var my_string:String; //assignment and concatenation my_string = ”Hi ”+”there!”; //traces to Hi there! //getting substring var sub_string:String = my_string.substring(3, my_string.length); //making all characters uppercase var upper_string:String = sub_string.toUpperCase(); trace(upper_string); //yields THERE!  Tip! Declare your variables first (on top) Datatype: String (2/2)

Data type: Number (1/2) The Number data type can represent integers, unsigned integers, and floating-point numbers The Number data type can represent integers, unsigned integers, and floating-point numbers The Number data type uses the 64-bit double-precision format The Number data type uses the 64-bit double-precision format The default value is NaN The default value is NaN//declaration var length:Number; //assignment length = 1100; length = -22; length = ; length = 100/3; //traces to length = 1/0; //traces to Infinity

Variable declaration, assignment and initialization Variable declaration, assignment and initialization//declaration var height:Number; //assignment height = 200;//literal value height = anotherVariable;//value from another variable //initialization(declaration and assignment on the same code line) var width:Number = 300; //check max and min values Number.MAX_VALUE == e+308 Number.MIN_VALUE == e-324 Data type: Number (2/2)

Data type: int New data type in AS3 New data type in AS3 The int data type is a 32-bit integer between -2,147,483,648 and 2,147,483,647 The int data type is a 32-bit integer between -2,147,483,648 and 2,147,483,647 Integers only work in whole numbers and ignore the decimal value, always rounding down Integers only work in whole numbers and ignore the decimal value, always rounding down The default value for variables that are of data type uint is 0 The default value for variables that are of data type uint is 0 //example 1 var myInt:int = -15; var myInt2:int = 3500; //example 2 var num:int = 2.5; var product:int = num + 2; trace(product); //4, the decimal will be ignored

Data type: unit New data type in AS3 New data type in AS3 The uint (Unsigned Integer) data type is a 32-bit unsigned integer between 0 and 4,294,967,295 The uint (Unsigned Integer) data type is a 32-bit unsigned integer between 0 and 4,294,967,295 Unsigned integer or any non-negative whole number Unsigned integer or any non-negative whole number The default value for variables that are of data type uint is 0 The default value for variables that are of data type uint is 0 var myUnit:unit = 1; var mySecondUnit:unit = 3500; var unsignedInteger:uint; //0

Number, int or unit? To maximize performance, it’s recommended that we use the Number data type only for integer values larger than the int and uint types can store or for floating-point numbers. To maximize performance, it’s recommended that we use the Number data type only for integer values larger than the int and uint types can store or for floating-point numbers. To store a floating-point number, include a decimal point in the number To store a floating-point number, include a decimal point in the number If we omit a decimal point, the number will be stored as an integer If we omit a decimal point, the number will be stored as an integer

Data type: Boolean Boolean represents a boolean value, possible values: true or false Boolean represents a boolean value, possible values: true or false Converts the parameter expression to a Boolean value and returns true or false Converts the parameter expression to a Boolean value and returns true or false Default vaule is false Default vaule is false //declaration of a boolean variable var isLoaded:Boolean; //assignmet isLoaded = true;

Default Values (1/2) A Default value is the value that a variable contains before you set its value A Default value is the value that a variable contains before you set its value You initialize a variable when you set its value for the first time You initialize a variable when you set its value for the first time If you declare a variable, but do not set its value, that variable is uninitialized If you declare a variable, but do not set its value, that variable is uninitialized The value of an uninitialized variable depends on its data type The value of an uninitialized variable depends on its data type

Default Values (2/2) The following table describes the default values of variables, organized by data type The following table describes the default values of variables, organized by data type Note! For variables of type Number, the default value is NaN (not a number) Note! For variables of type Number, the default value is NaN (not a number)

Array (1/2) Arrays are lists of data under which each item is identified by its order within the list Arrays are lists of data under which each item is identified by its order within the list The first element in an Array has index 0 The first element in an Array has index 0 An array can be made up of primitive type values like strings, numeric values, booleans or complex type values like other arrays or objects An array can be made up of primitive type values like strings, numeric values, booleans or complex type values like other arrays or objects var music_array:Array = new Array(); music_array = ["Metallica", "Bruce Springsteen", "U2", "Iron Maiden", "David Gray", "Van Morrison"]; music_array.length //traces the length of the array music_array[1]; //traces Bruce Springsteen var my_array:Array = [5,"Hello!",{a:5, b:7}]; //complex array

Array (2/2) Example of some methods for the Array object are: Example of some methods for the Array object are: slice, join, concat, push, pop, reverse etc. var music_array:Array = new Array(); music_array = ["Metallica", "Bruce Springsteen", "U2", "Iron Maiden", "David Gray", "Van Morrison"]; //slice(startIndex, endIndex) music_array.slice(2,4); //traces U2, Iron Maiden

Typecasting Sometimes we need to typecast a data type into another (for some reson), for exampe if we load XML data (numbers) and Flash interpret it like string vaules Sometimes we need to typecast a data type into another (for some reson), for exampe if we load XML data (numbers) and Flash interpret it like string vaules Here’s an example of how we can typecast a string into a number Here’s an example of how we can typecast a string into a number var my_string:String = "50"; var my_num:Number = 20; var answer:Number; trace(my_string + my_num); //traces 5020 //typecast the string to a number data type trace(Number(my_string) + my_num); //traces 70

Scope Scope is the realm or space in wich an object (variable) lives Scope is the realm or space in wich an object (variable) lives In ActionScript 3.0, variables are always assigned the scope of the function or class in which they are declared In ActionScript 3.0, variables are always assigned the scope of the function or class in which they are declared Entrance into that scope typically begins a variable's lifetime and exit from that scope typically ends its lifetime Entrance into that scope typically begins a variable's lifetime and exit from that scope typically ends its lifetime function localScope(){ var strLocal:String = "local"; var strLocal:String = "local";}localScope(); trace(strLocal); //error because strLocal is not defined globally

Arithmetic operators Arithmetic operators Arithmetic operators +, -, *, /, % ++, - - +=, -=, *=, /=, %= Increment ++, and decrement - - Increment ++, and decrement - - Increments/decrements a variable by 1 var x:Number = 10; x++; trace(x); //traces 11 x--; trace(x); //traces 10

Precedence The answer depends on operator precedence The answer depends on operator precedence var i:Number; i = * 10 / 2; //traces 27 You can override precedence by using parenteses You can override precedence by using parenteses var i:Number; i=(12 + 3) * 10 / 2; trace(i); //traces 75