SlideShare a Scribd company logo
Nafis’s 
Classroom 
ahmdnafis@gmail.com 
ActionScript Primer 
Chapter - 2
Overview : What is ActionScript 
• An object-oriented programming language used to build Flash 
content 
• Developed by Adobe and recent version is ActionScript 3.0 
• Can also be used to create APIs that are presentations, games, 
toys, websites or experiments 
• Enables the ActionScript programmer to draw 
programmatically 
• Has similarities with Ajax or JavaScript because it relies on 
DOM (Document Object Model) and responds to clicks or 
mouse events, for example.
Variables, constants, scopes, operators, conditional statements, loops 
and comments 
TIME FOR SOME BASICS!
Variables / Storage System 
• They are temporary storage 
systems that hold string of 
characters, numbers or 
other values 
• They contain information 
that are called objects when 
the program is running 
• They are also known as 
properties when they are a 
part of a class 
• Usage of underscores, 
numbers and letters are 
allowed 
• To declare a variable and 
assigning a value: 
 var city:String; 
 city = “dhaka”; 
Or 
 var city:String = “dhaka”; 
Several declaration at once 
 var city:String, country:String, 
myName:String; 
• Camel case should be used 
when naming variables ex: 
 myName = “whatever”
Constants 
• A special kind of variable 
that never changes when 
the program is running 
• Produces an error when an 
attempt is made to change 
the constant’s value 
• Constant’s name should be 
all uppercase letters 
(recommended) 
• Can be declared at the 
beginning of a class or in 
the constructor for the class 
• To declare a constant: 
 const DAYS:int = 7;
Be Literal Buddy! (Literals) 
• Literal means the value that is 
explicitly included in the code 
when it’s compiled 
• Beware: use of literals is not 
recommended ‘cause they can be 
hard to track when program 
needs to be debugged 
• Regular expressions with forward 
slashes 
 like /href=“#”/ 
• XML elements 
 like <location><city 
name=“Dhaka”/></location> 
• Generic objects 
 like {city: “Dhaka”, name: “Nafis”} 
• Array literals 
 like [“a”, “b”, “c”] 
• Empty values 
 like void, undefined or null 
• Strings with double or single 
quotes 
 Like ‘literals’ 
• Boolean values 
 Like true and false 
• Numbers of type int or uint 
 like 100, 3.22
Access Controls 
• Written before a 
property/variable or a 
method/class-function 
• Four types of access control 
attributes: 
 public 
 private 
 internal 
 protected 
• To use a access control keyword: 
 private var name:String = “Nafis”; 
 public class anything { 
//code goes here 
} 
• Public: Any code can access the 
property or the method it marked 
with this keyword 
• Private: Property or method 
named private can only be 
accessed within a class 
• Internal: Neither public nor 
private but internal objects are 
accessible to classes within the 
same class package/ any class 
that has the same package 
declaration 
 This is the default access control for 
all objects in AS 3.0 
• Protected: Protected objects can 
be accessed by child or 
subclasses/ when a class is 
extended
Introduction to Scope 
• Variables and functions in AS exist inside a specified scope 
• Two types of scope 
– Global scope: Any code defined outside a class or function will be 
regarded as in the global scope and can be accessed from anywhere 
– Local scope: Objects inside any class or function can be accessed only 
in their given scope/space not from everywhere. Local scope has 
numerous layers
Layer’s of Local Scope 
• Class level/Static variables and 
methods exist in the class itself, 
not requiring an instantiation of 
the class 
• Instance level variables & 
functions exists in every single 
instance of a class 
• Objects defined in functions that 
are housed inside classes are only 
available temporarily 
– Variables or properties inside the 
class is available to methods or 
functions 
– Functions’ parameters exists only 
inside the scope of the function 
– Objects in more localized scopes will 
override objects in more global 
scopes 
• Ex: 
 public class scope1 { 
public static var name:String = “Nafis”; 
} 
• To create an instance of a class: 
 var newS:scope1 = new scope1(); 
• When static keyword is used: 
 trace(scope1.name); // produces: Nafis 
• To access an instance variable: 
– public class scope2 { 
public var postalCode:Number = 1200; 
} 
– var newS2:scope2 = new scope2(); 
– trace(newS2.postalCode); // outputs 
1200
• Scope inside a function: 
– public class scope3 { 
public function hello():void { 
var msg:String = “What’s Up!”; 
trace(msg); 
} 
} 
– var newS3:scope3 = scope3(); 
– newS3.hello(); // outputs: What’s Up! 
– trace(newS3.hello); // undefined
Explaining Data Types 
• A data type refers to the data that 
can be stored in the object for 
example 
• Every object/variable or 
function’s parameter or return 
type need to have a data type 
• Objects with unknown type can 
have a wildcard(*) data type 
• Usage of colon operator is 
compulsory when declaring a 
data type of a variable 
– var months:Number; 
– No problem! 
• x = 22; 
• x = -33; 
• x = 3.3; 
– Problems…..Errrr… 
• x = “cat”; 
• x = “dog”; 
• To assign a wildcard data type: 
– var unknown:* = “any value”;
Introduction to Operators 
• They are built-in commands used for operating on one or more values 
• Three types of operators: 
– Unary: Requires single argument to operate, like decrement operator (--) 
– Binary: Requires two arguments to operate, like arithmetic's addition operator (+) 
– Trinary: Requires three arguments to evaluate (?:) 
• Operator orders: 
• Functional results are evaluated after their arguments are evaluated 
• Expressions that are more deeply nested are executed first, like: ((2-3)+6)*4 = 20 
• Multiplication, division and modulus operators are given equal significance (*, /, %) and 
are evaluated from left to right 
• Addition and subtraction have equal importance (+, -) and are evaluated from left to 
right
Let’s Meet the Operators! 
• The commonly used arithmetic 
operators 
– + Addition 
– - Subtraction 
– * multiplication 
– / division 
• % modulo operator returns the 
leftover or the remainder after 
dividing two numbers like 4 % 3 = 1 
• Compound assignment operators 
perform arithmetic operation on 
numbers and reassign the answer 
to the number, like: x += 1, which 
is also x = x + 1 
• Compound assignment operators 
are 
– += Add and reassign 
– *= Multiply and reassign 
– -= Subtract and reassign 
– /= Divide and reassign 
– %= Get the remainder from division 
and reassign it to the variable
Conditionals 
• Conditionals help to produce different outcomes for varied circumstances 
by evaluating the truth of logical expression 
• Every conditional statement relies on Boolean operation. Booleans are 
objects that can either have the value of true or false 
• Conditionals include 
– If / else if / else statements 
– If statements rely on several comparison operators 
– Switch / case / default statements
If / else if / else 
• If statements only evaluates when a 
certain/given condition is true 
• Additional if statements can be 
added with else if if the first 
condition in the first statement is 
not true 
• Lastly, an else statement can be 
added as a default block if all the 
other if statements are evaluated 
to be false 
• var today:String = “Saturday”; 
• Format of if statement 
– if (logical expression) 
{ 
//code to run if condition is met 
} else if (logical expression) { 
//code to run if the first condition is 
false 
} else { 
//this will run if all the above is false 
} 
– Ex: 
• if (today == “Friday”) { 
goToMosque(); 
} else if (today == “Saturday”) { 
readABook(); 
} else { 
startCoding(); 
} 
In human terms: ‘If today is Friday then go 
to the Mosque, if it’s Saturday then read a 
book otherwise start writing codes’
Comparison Operators 
• == Equality operator checks 
whether the value on the left is 
equal or equivalent to the value 
on the right 
• > Greater than and < less than 
operator checks whether a value 
is greater than or less than 
another number 
– Like: if (examMark > 50) {//you have 
passed} 
• >= Greater than equal to and <= 
less than equal to 
• Functions that check whether a 
value is boolean can be used in if 
statements 
• if(isNaN(today)) { checkTheDay(); } 
• Check for a null object by 
– if (today) { checkTheDay(); } 
– if (today != null) { checkTheDay(); } 
• ‘if today is not null then check the day of the week’ 
• && And and || or operators are used to 
join together multiple conditions 
– if(today == “Monday” || today == “Sunday”) 
{ startCoding(); } 
– if(today == “Saturday” && today == “Sunny”) 
{ startCycling(); } 
• != Not equal to will produce the 
opposite of the expression 
– An equality can be negated directly: 
if(!(today == “Friday”)) {readABook();} 
• ‘if today is not Friday then read a book’ 
– if(today != “Friday”) {readABook();} 
• ‘if today is not Friday then read a book’ 
Remember: 
null or undefined 
will always return 
false in if 
statements
Conditional operator 
• This operator takes three operands and works similarly like the if 
statement 
• Format: (logical expression) ? if true : if false; 
– (today == “Sunday”) ? startCoding() : readABook(); 
• ‘if today is Sunday then start writing codes but if today is not Sunday then read a book’ 
Same as: 
if(today == “Sunday”) { 
startCoding(); 
} else { 
readABook(); 
} 
• The entire statement can be assigned to a variable at once 
– whatToDo = (today == “Sunday”) ? startCoding() : readABook(); 
– today = today ? today : “Saturday”; 
• ‘if the variable today is not null then set the default value otherwise set the string “Saturday ” to it’
Switch / Case / Default 
• Giant block of if…else statement 
• It tests a single expression against several possible outcomes and if none is 
exact then set a default value 
• If a match is found the chain is broken using the keyword break 
• switch(today) { 
case “Friday”: 
goToMosque(); 
break; 
case “Sunday”: 
startCoding(); 
break; 
default: 
readABook(); 
} 
Equivalent to writing 
if (today == “Friday”) { 
goToMosque(); 
} else if (today == “Sunday”) { 
startCoding(); 
} else { 
readABook(); 
}
Loops 
• Loops count up or down through a set of data 
• Allows us to repeat a given action multiple times 
• Ex: count through a list, search through an array of items or output a set of 
characters like A-Z on the screen 
• Three distinct type of looping structures 
– For loop: for loop has two sub-categorical structures 
• for…in loop 
• for…each loop 
– While loop 
– Do..while loop
For Loops 
• Has similarity with C, Java, PHP and 
other languages 
• The variable that is defined in the 
loop is explicitly available for that 
scope only 
• i stands for index 
– Conventional variable name but 
anything name can be used 
• for….in loop iterates through every 
value stored in an object 
• for each…in iterates the properties 
or functions directly rather than 
iterating the property-names 
• for (var i:int = 1; i <= 100; i++) { 
//put some code that will print out 100 
times 
} 
‘First declare a new variable with an integer type and 
assign a value of 1 and repeat an action 100 times by 
incrementing or adding 1 to the previous value’ 
• for (var value:String in targetObject) { //do 
something} 
• var weekDays:Object = new Object(); 
• weekDays.first = “Sunday”; 
• weekDays.second = “Monday”; 
• weekDays.third = “Tuesday”; 
• for (var value:String in weekDays) { 
trace(value +” : ”+ weekDays[value]) 
} 
• for each (var value:String in targetObject) { //do 
something} 
– for each (var value:String in weekDays) { 
trace(value); 
}
While and Do…while Loops 
• While loop will iterate as long as 
the given condition is true 
• While loops are useful when the 
number of iteration is unknown 
• Do…while loop checks the 
condition after one iteration is 
complete 
• while (condition == true) { //perform 
some action } 
• var i:int = 0; 
while (i <= 100) { 
//perform some action 
i++; 
} 
• do { 
//perform an iteration 
} while (condition == true); 
• var i:Number = 1; 
do { 
i++; 
} while(i <= 100); 
trace(i +" is greater than 100"); 
for (var i:int = 0; i <= 100; i++) { 
//perform some action 
} 
Same as
Comments 
• Comments are codes that are not executed when a compiler is compiling 
the code 
• They can be referred to as sticky notes that explains the code 
that we write into further details to help our fellow 
programmers 
• Add comments where necessary because they can be useful if we haven’t 
looked into a piece of code for a long time 
• Types of comments: 
– Single line comment: 
• var i:int = 0; // this is an integer-type variable 
– Block comment: 
• /* this is a block comment where large texts can be added */
Happy Coding 
& 
Salam

More Related Content

What's hot (20)

PPTX
Java Tutorial Lab 3
Berk Soysal
 
PPT
Helberg acl-final
Clay Helberg
 
PPT
Chelberg ptcuser 2010
Clay Helberg
 
PPTX
Scala, Play 2.0 & Cloud Foundry
Pray Desai
 
PPTX
Local variables Instance variables Class/static variables
Sohanur63
 
PPTX
Quick Scala
Puneet Kumar
 
PPTX
Java 8 lambda
Manav Prasad
 
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens
 
PDF
ITFT - Java
Blossom Sood
 
PDF
Programming with Lambda Expressions in Java
langer4711
 
PPTX
Scala basic
Nguyen Tuan
 
PDF
Java 8 Lambda Expressions & Streams
NewCircle Training
 
PDF
Java 8 - Project Lambda
Rahman USTA
 
PPTX
PCSTt11 overview of java
Archana Gopinath
 
PPTX
Java 8 Lambda and Streams
Venkata Naga Ravi
 
PPTX
Java 8 Features
Leninkumar Koppoju
 
PDF
Lambda Expressions in Java
Erhan Bagdemir
 
PDF
Functional programming in scala
Stratio
 
PPTX
introduction to c #
Sireesh K
 
PPTX
Functional programming for the Advanced Beginner
Luis Atencio
 
Java Tutorial Lab 3
Berk Soysal
 
Helberg acl-final
Clay Helberg
 
Chelberg ptcuser 2010
Clay Helberg
 
Scala, Play 2.0 & Cloud Foundry
Pray Desai
 
Local variables Instance variables Class/static variables
Sohanur63
 
Quick Scala
Puneet Kumar
 
Java 8 lambda
Manav Prasad
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens
 
ITFT - Java
Blossom Sood
 
Programming with Lambda Expressions in Java
langer4711
 
Scala basic
Nguyen Tuan
 
Java 8 Lambda Expressions & Streams
NewCircle Training
 
Java 8 - Project Lambda
Rahman USTA
 
PCSTt11 overview of java
Archana Gopinath
 
Java 8 Lambda and Streams
Venkata Naga Ravi
 
Java 8 Features
Leninkumar Koppoju
 
Lambda Expressions in Java
Erhan Bagdemir
 
Functional programming in scala
Stratio
 
introduction to c #
Sireesh K
 
Functional programming for the Advanced Beginner
Luis Atencio
 

Similar to Adobe Flash Actionscript language basics chapter-2 (20)

PPSX
DIWE - Programming with JavaScript
Rasan Samarasinghe
 
PPTX
Introduction to Client-Side Javascript
Julie Iskander
 
PDF
Ch. 17 FIT5, CIS 110 13F
mh-108
 
PPT
data-types-operators-datatypes-operators.ppt
Gagan Rana
 
PPT
13665449.ppt
JP Chicano
 
PDF
Javascript breakdown-workbook
HP IT GROUP (TEBIM TEBITAGEM) TTGRT HP E-TİCARET
 
PDF
Javascript - Tutorial
adelaticleanu
 
PPTX
Javascript 101
Shlomi Komemi
 
PPTX
Php + my sql
Ashen Disanayaka
 
PPTX
Paca java script slid
pacatarpit
 
PPTX
CSC PPT 13.pptx
DrRavneetSingh
 
PPTX
ExpressionsInJavaScriptkkkkkkkkkkkkkkkkk
kamalsmail1
 
PDF
Introduction to Javascript and Typescript.pdf
rony setyawansyah
 
PPT
Javascript
Manav Prasad
 
PDF
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
AlexShon3
 
PPTX
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
PDF
JavaScript for beginners
Shahrukh Ali Khan
 
PPTX
JavaScript / Web Engineering / Web Development / html + css + js/presentation
M Sajid R
 
PPT
Javascript
vikram singh
 
DOC
Java Script Language Tutorial
vikram singh
 
DIWE - Programming with JavaScript
Rasan Samarasinghe
 
Introduction to Client-Side Javascript
Julie Iskander
 
Ch. 17 FIT5, CIS 110 13F
mh-108
 
data-types-operators-datatypes-operators.ppt
Gagan Rana
 
13665449.ppt
JP Chicano
 
Javascript - Tutorial
adelaticleanu
 
Javascript 101
Shlomi Komemi
 
Php + my sql
Ashen Disanayaka
 
Paca java script slid
pacatarpit
 
CSC PPT 13.pptx
DrRavneetSingh
 
ExpressionsInJavaScriptkkkkkkkkkkkkkkkkk
kamalsmail1
 
Introduction to Javascript and Typescript.pdf
rony setyawansyah
 
Javascript
Manav Prasad
 
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
AlexShon3
 
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
JavaScript for beginners
Shahrukh Ali Khan
 
JavaScript / Web Engineering / Web Development / html + css + js/presentation
M Sajid R
 
Javascript
vikram singh
 
Java Script Language Tutorial
vikram singh
 
Ad

Recently uploaded (20)

PPTX
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
PDF
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
PDF
Rapid Mathematics Assessment Score sheet for all Grade levels
DessaCletSantos
 
PPTX
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
PDF
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
PPTX
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
PPTX
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
PPTX
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PDF
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PDF
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
PPTX
week 1-2.pptx yueojerjdeiwmwjsweuwikwswiewjrwiwkw
rebznelz
 
PPTX
Matatag Curriculum English 8-Week 1 Day 1-5.pptx
KirbieJaneGasta1
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
PDF
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
Rapid Mathematics Assessment Score sheet for all Grade levels
DessaCletSantos
 
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
week 1-2.pptx yueojerjdeiwmwjsweuwikwswiewjrwiwkw
rebznelz
 
Matatag Curriculum English 8-Week 1 Day 1-5.pptx
KirbieJaneGasta1
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
Ad

Adobe Flash Actionscript language basics chapter-2

  • 1. Nafis’s Classroom [email protected] ActionScript Primer Chapter - 2
  • 2. Overview : What is ActionScript • An object-oriented programming language used to build Flash content • Developed by Adobe and recent version is ActionScript 3.0 • Can also be used to create APIs that are presentations, games, toys, websites or experiments • Enables the ActionScript programmer to draw programmatically • Has similarities with Ajax or JavaScript because it relies on DOM (Document Object Model) and responds to clicks or mouse events, for example.
  • 3. Variables, constants, scopes, operators, conditional statements, loops and comments TIME FOR SOME BASICS!
  • 4. Variables / Storage System • They are temporary storage systems that hold string of characters, numbers or other values • They contain information that are called objects when the program is running • They are also known as properties when they are a part of a class • Usage of underscores, numbers and letters are allowed • To declare a variable and assigning a value:  var city:String;  city = “dhaka”; Or  var city:String = “dhaka”; Several declaration at once  var city:String, country:String, myName:String; • Camel case should be used when naming variables ex:  myName = “whatever”
  • 5. Constants • A special kind of variable that never changes when the program is running • Produces an error when an attempt is made to change the constant’s value • Constant’s name should be all uppercase letters (recommended) • Can be declared at the beginning of a class or in the constructor for the class • To declare a constant:  const DAYS:int = 7;
  • 6. Be Literal Buddy! (Literals) • Literal means the value that is explicitly included in the code when it’s compiled • Beware: use of literals is not recommended ‘cause they can be hard to track when program needs to be debugged • Regular expressions with forward slashes  like /href=“#”/ • XML elements  like <location><city name=“Dhaka”/></location> • Generic objects  like {city: “Dhaka”, name: “Nafis”} • Array literals  like [“a”, “b”, “c”] • Empty values  like void, undefined or null • Strings with double or single quotes  Like ‘literals’ • Boolean values  Like true and false • Numbers of type int or uint  like 100, 3.22
  • 7. Access Controls • Written before a property/variable or a method/class-function • Four types of access control attributes:  public  private  internal  protected • To use a access control keyword:  private var name:String = “Nafis”;  public class anything { //code goes here } • Public: Any code can access the property or the method it marked with this keyword • Private: Property or method named private can only be accessed within a class • Internal: Neither public nor private but internal objects are accessible to classes within the same class package/ any class that has the same package declaration  This is the default access control for all objects in AS 3.0 • Protected: Protected objects can be accessed by child or subclasses/ when a class is extended
  • 8. Introduction to Scope • Variables and functions in AS exist inside a specified scope • Two types of scope – Global scope: Any code defined outside a class or function will be regarded as in the global scope and can be accessed from anywhere – Local scope: Objects inside any class or function can be accessed only in their given scope/space not from everywhere. Local scope has numerous layers
  • 9. Layer’s of Local Scope • Class level/Static variables and methods exist in the class itself, not requiring an instantiation of the class • Instance level variables & functions exists in every single instance of a class • Objects defined in functions that are housed inside classes are only available temporarily – Variables or properties inside the class is available to methods or functions – Functions’ parameters exists only inside the scope of the function – Objects in more localized scopes will override objects in more global scopes • Ex:  public class scope1 { public static var name:String = “Nafis”; } • To create an instance of a class:  var newS:scope1 = new scope1(); • When static keyword is used:  trace(scope1.name); // produces: Nafis • To access an instance variable: – public class scope2 { public var postalCode:Number = 1200; } – var newS2:scope2 = new scope2(); – trace(newS2.postalCode); // outputs 1200
  • 10. • Scope inside a function: – public class scope3 { public function hello():void { var msg:String = “What’s Up!”; trace(msg); } } – var newS3:scope3 = scope3(); – newS3.hello(); // outputs: What’s Up! – trace(newS3.hello); // undefined
  • 11. Explaining Data Types • A data type refers to the data that can be stored in the object for example • Every object/variable or function’s parameter or return type need to have a data type • Objects with unknown type can have a wildcard(*) data type • Usage of colon operator is compulsory when declaring a data type of a variable – var months:Number; – No problem! • x = 22; • x = -33; • x = 3.3; – Problems…..Errrr… • x = “cat”; • x = “dog”; • To assign a wildcard data type: – var unknown:* = “any value”;
  • 12. Introduction to Operators • They are built-in commands used for operating on one or more values • Three types of operators: – Unary: Requires single argument to operate, like decrement operator (--) – Binary: Requires two arguments to operate, like arithmetic's addition operator (+) – Trinary: Requires three arguments to evaluate (?:) • Operator orders: • Functional results are evaluated after their arguments are evaluated • Expressions that are more deeply nested are executed first, like: ((2-3)+6)*4 = 20 • Multiplication, division and modulus operators are given equal significance (*, /, %) and are evaluated from left to right • Addition and subtraction have equal importance (+, -) and are evaluated from left to right
  • 13. Let’s Meet the Operators! • The commonly used arithmetic operators – + Addition – - Subtraction – * multiplication – / division • % modulo operator returns the leftover or the remainder after dividing two numbers like 4 % 3 = 1 • Compound assignment operators perform arithmetic operation on numbers and reassign the answer to the number, like: x += 1, which is also x = x + 1 • Compound assignment operators are – += Add and reassign – *= Multiply and reassign – -= Subtract and reassign – /= Divide and reassign – %= Get the remainder from division and reassign it to the variable
  • 14. Conditionals • Conditionals help to produce different outcomes for varied circumstances by evaluating the truth of logical expression • Every conditional statement relies on Boolean operation. Booleans are objects that can either have the value of true or false • Conditionals include – If / else if / else statements – If statements rely on several comparison operators – Switch / case / default statements
  • 15. If / else if / else • If statements only evaluates when a certain/given condition is true • Additional if statements can be added with else if if the first condition in the first statement is not true • Lastly, an else statement can be added as a default block if all the other if statements are evaluated to be false • var today:String = “Saturday”; • Format of if statement – if (logical expression) { //code to run if condition is met } else if (logical expression) { //code to run if the first condition is false } else { //this will run if all the above is false } – Ex: • if (today == “Friday”) { goToMosque(); } else if (today == “Saturday”) { readABook(); } else { startCoding(); } In human terms: ‘If today is Friday then go to the Mosque, if it’s Saturday then read a book otherwise start writing codes’
  • 16. Comparison Operators • == Equality operator checks whether the value on the left is equal or equivalent to the value on the right • > Greater than and < less than operator checks whether a value is greater than or less than another number – Like: if (examMark > 50) {//you have passed} • >= Greater than equal to and <= less than equal to • Functions that check whether a value is boolean can be used in if statements • if(isNaN(today)) { checkTheDay(); } • Check for a null object by – if (today) { checkTheDay(); } – if (today != null) { checkTheDay(); } • ‘if today is not null then check the day of the week’ • && And and || or operators are used to join together multiple conditions – if(today == “Monday” || today == “Sunday”) { startCoding(); } – if(today == “Saturday” && today == “Sunny”) { startCycling(); } • != Not equal to will produce the opposite of the expression – An equality can be negated directly: if(!(today == “Friday”)) {readABook();} • ‘if today is not Friday then read a book’ – if(today != “Friday”) {readABook();} • ‘if today is not Friday then read a book’ Remember: null or undefined will always return false in if statements
  • 17. Conditional operator • This operator takes three operands and works similarly like the if statement • Format: (logical expression) ? if true : if false; – (today == “Sunday”) ? startCoding() : readABook(); • ‘if today is Sunday then start writing codes but if today is not Sunday then read a book’ Same as: if(today == “Sunday”) { startCoding(); } else { readABook(); } • The entire statement can be assigned to a variable at once – whatToDo = (today == “Sunday”) ? startCoding() : readABook(); – today = today ? today : “Saturday”; • ‘if the variable today is not null then set the default value otherwise set the string “Saturday ” to it’
  • 18. Switch / Case / Default • Giant block of if…else statement • It tests a single expression against several possible outcomes and if none is exact then set a default value • If a match is found the chain is broken using the keyword break • switch(today) { case “Friday”: goToMosque(); break; case “Sunday”: startCoding(); break; default: readABook(); } Equivalent to writing if (today == “Friday”) { goToMosque(); } else if (today == “Sunday”) { startCoding(); } else { readABook(); }
  • 19. Loops • Loops count up or down through a set of data • Allows us to repeat a given action multiple times • Ex: count through a list, search through an array of items or output a set of characters like A-Z on the screen • Three distinct type of looping structures – For loop: for loop has two sub-categorical structures • for…in loop • for…each loop – While loop – Do..while loop
  • 20. For Loops • Has similarity with C, Java, PHP and other languages • The variable that is defined in the loop is explicitly available for that scope only • i stands for index – Conventional variable name but anything name can be used • for….in loop iterates through every value stored in an object • for each…in iterates the properties or functions directly rather than iterating the property-names • for (var i:int = 1; i <= 100; i++) { //put some code that will print out 100 times } ‘First declare a new variable with an integer type and assign a value of 1 and repeat an action 100 times by incrementing or adding 1 to the previous value’ • for (var value:String in targetObject) { //do something} • var weekDays:Object = new Object(); • weekDays.first = “Sunday”; • weekDays.second = “Monday”; • weekDays.third = “Tuesday”; • for (var value:String in weekDays) { trace(value +” : ”+ weekDays[value]) } • for each (var value:String in targetObject) { //do something} – for each (var value:String in weekDays) { trace(value); }
  • 21. While and Do…while Loops • While loop will iterate as long as the given condition is true • While loops are useful when the number of iteration is unknown • Do…while loop checks the condition after one iteration is complete • while (condition == true) { //perform some action } • var i:int = 0; while (i <= 100) { //perform some action i++; } • do { //perform an iteration } while (condition == true); • var i:Number = 1; do { i++; } while(i <= 100); trace(i +" is greater than 100"); for (var i:int = 0; i <= 100; i++) { //perform some action } Same as
  • 22. Comments • Comments are codes that are not executed when a compiler is compiling the code • They can be referred to as sticky notes that explains the code that we write into further details to help our fellow programmers • Add comments where necessary because they can be useful if we haven’t looked into a piece of code for a long time • Types of comments: – Single line comment: • var i:int = 0; // this is an integer-type variable – Block comment: • /* this is a block comment where large texts can be added */
  • 23. Happy Coding & Salam