Вы находитесь на странице: 1из 7

Chapter 5

Basics
In every programming language, there can be times when one needs to compute things or store some results during runtime. For this purposes, variables are used. Variables are nothing but certain named memory locations which are capable of storing data. These data can be modified or changed during the execution of program. In actionscript 3.0, these variables store chunks of information known as objects. In actionsctipt 3.0, a variable can be declared using the keyword var. the syntax is as follows: var <variable name>: <data type>; Data type represents what kind of information the variable is going to store, for example string(array of characters), numbers etc. var rAM: number; declares a variable rAM of data type number. Notice that the variable has its first letter in lower case. This tells us that there are restrictions to variable names. This occurs as the program is first converted to assembly language by the compiler then executed. So if the variables are named out of format, compiler may show some errors. The restrictions while naming the variables are: They should start with a letter in lower case or an underscore They can be named on a keyword(words reserved for the compilers) Now consider the following code. var clocktime:number= 10; trace(the time is + clocktime); this prints the value assigned to the clocktime that is 10 on the screen. Values can also be later assigned to any variable after its declaration as <variable name>=<value>. trace() prints whatever is inside it on the output screen. Another feature of actionscript 3.0 is that there is a score of making a variable constant. It means that this variable will now hold a constant value throughout the program and its value will not change. They can made using the keyword const and adding it before variable name during declaration. Apart from variables, we will also use literals in the programs. Literals are values that are explicitly added in the code during compile-time. Various literals which actionscript 3.0 include are: Numeric values Boolean values of truth and false Strings Empty values NULL and NaN Array literals Generic objects Regular expressions enclosed in forward slashes / Elements written in XML

SCOPE OF A VARIABLE
Now it is even possible that in some parts of the program a variable cannot be used. For this it is necessary to know the scope of a variable. The scope decides the parts where a variable can be used. A variable can either be global or local. A variable which is globally defined can be used anywhere in the function. It can be declared at a place which is not inside any block. Local variables can only be used in a specific function where it is declared and vanishes as the program moves out of a function. Scopes are not just restricted to variables. They are even implemented in classes, packages where we will revisit again in later chapters.

DATA TYPES
Another fundamental thing regarding the variables are the data types. Data types represent what kind of values are going to be stored in a variable. The various types of data types are as follows: Int- integer values Number- floating point nos String- for characters Boolean- truth or false Void All the data types above are primitive data types. Various other complex data types also exist which will be discussed later. It is also possible that a variable be declared such that its type is known during run-time. For this Actionscript 3 supports a feature of wildcard type(*). It denotes that the data type of that variable is unknown till runtime or will accept values from more than two data type.

CHAPTER 8
Actionscript 3.0 , as we mentioned earlier, is based on OOP. It resembles more towards our day to day scenarios. It may so happen that we want some parts of the programs to execute based on some conditions just as in real life our decisions are based on situations. This problem is handled in AS3 using the conditional statements. The various conditional statements used in AS3 are if-else, switch case and conditional operator.

If- else:
The if condition allows the programmer to get a block of executed only when a certain condition is met. Otherwise it moves to block which is under else just as the name suggests. 1 is considered as being truth whereas binary 0 represents a false condition. Syntax: If(condition) { } Else { } Note that there is no semicolon at the end of if <condition>. Example: Var rate:number =10; if(rate==10) { trace( rate is as expected as + rate); } else { trace (rate is not 10); } will print the statement rate is as expected as 10. Had the value of rate be other than 10, it would have printed rate is not 10. Just as mentioned earlier, if the condition is true, the if block is executed. So if(1) {} will always execute whatever is written under { }. Now there can also be times when we need to check multiple conditions at same time. For this purpose nested if-else is used. They can be of two forms: If(condition1) {

If(condition2) {. If(condition 3) { . } Else{} Else{} Else{}

The else are executed in order from the innermost ifs else to be executed first . If(condition){} else{..} If(condition){} Else{..} ..

SWITCH STATEMENT
Switch statements are used for compact comparisons and executions. For example: In if-else: If(dayofweek==Sunday){Sunday();} Else If(dayofweek==Monday){Monday();} Else If(dayofweek==Tuesday){Tuesday();} Else If(dayofweek==Wednesday){Wednesday();} Else If(dayofweek==Thursday){Thursday();} Else If(dayofweek==Friday){Friday();} Else {saturday();} In switch case: Switch(dayofweek) { Case Sunday: Sunday(); break; Case Monday: Monday(); break; Case Tuesday: Tuesday(); break; Case Wednesday: Wednesday(); break; Case Thursday: Thursday(); break; Case Friday: Friday(); break; Default : Saturday(); break; }

Notice the break statements at the end of each case. If this is not done, then each case below the true case will get executed. This is called fall-through. To come out of switch after condition is met, we use break.

CHAPTER 10
It may so happen that we want to execute a certain block for code for a certain number of times. For this purpose we take use of loops. Loops are used to execute a block of code a certain number of times based on some conditions. There are mainly three kinds of loops that we are going to discuss:

For Loop:
It is one of the most commonly used loop in AS3. Its syntax is as follows. for(var <variable name>:<data type>=<value>;<condition till loop will run>;<test changes>) { //do some action} Here the test changes signifies the the part which will run after the block of code is executed and the program again returns to the for loop statement. var <variable name>:<data type>=<value> signifies the initialisation of the loop. There are various other variants of for loop as: 1. For.in : It uses the property of an object to iterate. So for every property inside an object, it iterates once. For example For(var name:string in object) {//do some action} will iterate once for every property in object. Variable name stores the name of the current property in object as iteration goes on. 2. For eachin: Only a slight different is there between these two. For each in loop retrieves all the values of the properties associated with the object whereas for in retrieves only the names. Syntax:
var myObject:Object = new Object(); myObject.myName = "AJM"; myObject.myAge = 18; for each (var prop in myObject){ trace(prop); } prints AJM and 18 whereas var myObject:Object = new Object(); myObject.myName = "AJM"; myObject.myAge = 18; for(var prop in myObject){ trace(prop); } prints myName and myAge .

While loop:
The basic structure of a while loop is While(condition)

{//do some action} It executes the block of statement as long as the condition is true. For example: Var i:int=1; While(i<10) { Trace(i); i++; } Prints 1 to 9 on the screen.

Do- while loop:


Do while loop is an exit controlled loop where the condition is checked after the block has been executed. Syntax: Do {//do some action }while(condition) It also works similar to other loops.

Now we are going to discuss two keywords: break and continue. Break forces an iteration to terminate. That is, when it is encountered, the control goes out of the loop. It is useful when we want to get out a loop under some situation. Continue forces the next iteration to occur. That is, it forces the present loop to terminate and starts the next iteration. It is useful when we dont want a particular iteration to run.

Вам также может понравиться