2533CIT Iteration, Arrays, Static Properties and Math - PowerPoint PPT Presentation

1 / 59
About This Presentation
Title:

2533CIT Iteration, Arrays, Static Properties and Math

Description:

We access the items in an array using the square brackets and a numbered index. ... We must use array notation when accessing the properties. Array Iteration Example ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 60
Provided by: hobbitIct
Category:

less

Transcript and Presenter's Notes

Title: 2533CIT Iteration, Arrays, Static Properties and Math


1
2533CIT Iteration, Arrays, Static Properties
and Math
  • Coridyn Fitzgerald-Hood (2007)

2
Arrays
  • Arrays are a simple data structure used to group
    related information.
  • Instead managing multiple variables we can now
    just manage 1.

3
  • This is longwinded
  • And unnecessarily repetitious.

4
  • This is a little better
  • At least we only have to pass one parameter to
    the function.

5
  • We can still do better.
  • Array shorthand notation
  • Use the square brackets
  • item1, item2, , itemN

6
  • var appleBoxArray
  • We now have a number of objects stored in their
    own discrete location within the array.

7
Accessing Array Items
  • Once we have items in the array we need to get
    them back out
  • We access the items in an array using the square
    brackets and a numbered index.

8
  • Array indices (or indexes) start counting from 0
    (zero)

Item 0
Item 1
9
  • And continue to length - 1.

Item 4 (length - 1)
10
  • If we run off the end of the array we receive the
    value undefined
  • appleBox5

0
1
2
3
4
5
?
11
  • Code example

12
Array Properties
  • Arrays are complex data types so they have
    properties and functions defined on them
  • Array.length The number of items in the array.

13
  • Array.push(objInst)
  • Add a new item to the end of the array.

14
  • Array.pop()Object
  • Remove an item from the end of the list (and
    return it).

15
  • Array.unshift(objInst)
  • Add a new item to the start of the array.

16
  • Array.shift()Object
  • Remove an item from the start of the array (and
    return it).

17
  • Array.splice(startIndex, deleteCountNumber,
    valueObject)
  • Adds or removes items from the array.

18
  • Delete n items
  • Insert item

19
Array Overview
  • Arrays hold multiple values in an indexed
    variable.
  • Valid indices are between 0 and length 1
  • We can add and remove items after creating the
    array.

20
Array Exercises
21
  • Using the Array functions from the previous
    slides how could we mimic the following
    Collection data structures?
  • Stack Last on, first off
  • Queue First on, first off

22
Iteration
  • iteration - it-uh-rey-shuh n
  • noun
  • 1.the act of repeating a repetition.

23
Iteration
  • We use loops for performing repetitive tasks.
  • There are three types of loops
  • While,
  • Dowhile,
  • For.

24
Terminology
  • The continuation test performed in a loop is also
    known as the guard.
  • i.e. it guards the contents of the loop.

25
While Loop
  • The while loop is a test-first loop
  • The guard is evaluated before entering the loop.
  • The loop is executed zero or more times.

26
Dowhile Loop
  • The dowhile loop is a test-last loop
  • The guard is evaluated after the previous
    iteration.
  • This loop is executed one or more times.

27
For Loop
  • The for loop is semantically equivalent to a
    while loop
  • Adds three statements to the head of the loop,
  • Considered clearer and less error-prone than
    while loops,
  • Executed zero or more times.

28
For Loop continued
  • Initialisation
  • Setup loop variables prior to execution,
  • Comma-separated values,
  • Code executed once only.
  • Guard
  • Continuation test.
  • Tested before every iteration.
  • Post
  • Executed after each iteration.

29
For Loop continued
30
While Loop Example
31
Dowhile Example
32
Storing Results
  • We often need to store the results of repetitive
    actions
  • Individual variables are obviously unwieldy,
  • Arrays are perfect for this

33
  • Now that we have a collection of MovieClips we
    can perform batch processing.

34
(No Transcript)
35
Iterators
  • There is a special fourth type of loop called a
    Property Iterator or forin loop.
  • Can be applied to Arrays and Object property
    lists.

36
  • The forin loop accesses the property names of an
    object instance, which we use to access the
    object values.
  • The iterator (the i value) is a string.
  • We must use array notation when accessing the
    properties.

37
Array Iteration Example
38
(No Transcript)
39
Static properties
40
  • Static properties are defined as properties that
    belong to the class definition not an instance
    of the class.
  • Static properties are shared amongst all instance
    of the class
  • a change made in one instance is reflected in all
    others.

41
Definition of static property
42
Changing a static property
43
Examples of Static Properties
  • Static properties are often used for values that
    dont change, or properties that need to be
    easily accessible from outside a class.
  • The stage exposes some static properties
  • Stage.width
  • Stage.height
  • Note we did not instantiate the Stage class.

44
Static Functions
  • We can also define static functions on a class.

45
Factory class example
46
Limitations of Static Functions
  • Static functions cannot access instance
    variables.

47
The Math Class
48
  • Actionscript provides us with a Math class that
    defines a number of useful (mathematically
    related) properties and functions.
  • Well only be looking at a few )

49
  • Math.min(nNumber, mNumber)Number
  • Returns the smaller of the two numbers
  • Math.max(nNumber, mNumber)Number
  • Returns the larger of the two numbers

50
  • The max and min functions are useful for clamping
    a range of values,
  • That is, ensuring values remain within specified
    values.
  • Can you think of any useful examples?

51
Long and ugly
52
Simplified
53
Further simplified
54
  • Semantically
  • Return a value no larger than Stage.width.
  • Semantically
  • Return a value no less than 0.

55
  • Note The order of the nested min and max
    functions doesnt matter, the comparison values
    do.

56
  • Math.floor(nNumber)Number
  • Return the closest integer less than or equal to
    n.
  • Math.ceil(nNumber)Number
  • Return the closest integer greater than or equal
    to n.

57
  • Math.floor(1.2) // Returns 1
  • Math.floor(5.0) // Returns 5
  • Math.ceil(2.03) // Returns 3
  • Math.ceil(-3.3) // Returns -3

58
  • Math.random()Number
  • Return a random value where 0 lt n lt 1

59
  • Returns a random value between 4 and 11 inclusive.
Write a Comment
User Comments (0)
About PowerShow.com