Shawn Decker - ATS 3137
School of the Art Institute of Chicago
Javascript/Actionscript: Basic Statements
There are only a small number of basic operators that you need to learn to get started in Javascript and Actionscript. These include looping, conditional execution, and the use and definition of functions. Here we will cover all of these you will need to know for a while except functions, which will be covered a little later.
Conditional Statements:
The if Statement
Conditional statements are simply statements which allow a set of computer instructions to only be executed by the computer if a particular conditional statement returns a (Boolean) value of true. These statements are structed like this:
if (conditional statement) single computer instruction;or
if (conditional statement){ first computer instruction; second computer instruction; third computer instruction; }In these statements, the single computer instruction, or a group of instructions (which must be enclosed in curly braces - this is called an "instruction block" and may be substituted for a single instruction) are only executed by the computer if the conditional statement given is true. An example:
var a;a = 5 + 7;if (a > 15) document.write("We are Done Now");
The else Statement
The else statement can only follow an if statment. The else statement is used if there is an action that whould be taken in the case that the conditional statement in the if statement is false. This allows then for one of two instruction(s) to be executed depending on the result of the conditional statement. The format is:
if (conditional statement) single computer instruction;else single computer instruction;or
if (conditional statement){ first computer instruction; second computer instruction; third computer instruction; } else { first computer instruction; second computer instruction; third computer instruction; } The if else Statement
The if else statement can only follow an if statment. The if else statement is used if there are several conditions which need to be tested, and a seperate action for each. Any number of if else statements can be strung together. The main feature of these statements is that as soon as one of the conditional statments tested is true, none of the other if else statments will be executed, so that never more than one of the statments (or statement blocks) will be executed by the computer. An else statment optionally can be included at the end, which will only execute if all the preceding conditional statements were false. The format is :
if (conditional statement) single computer instruction;else if (conditional statement 2) single computer instruction; else if (conditional statement 3) single computer instruction; . . . else single computer instruction;or
if (conditional statement){ first computer instruction; second computer instruction; third computer instruction; } else if (conditional statement 2){ first computer instruction; second computer instruction; third computer instruction; } else if (conditional statement 3){ first computer instruction; second computer instruction; third computer instruction; } . . . else{ first computer instruction; second computer instruction; third computer instruction; }
Loops:
There are two basic kinds of loops in Javascript and Actionscript, the for loop and the while or do/while loop. Both are similar,and indeed are nearly interchangable, so in general you can use whichever of these seems to fit a specific need best. Most often, you will probably end up using the for loop.
Each of these loops has a common function and structure. The main object of a loop statement is to repeat a set of instructions over and over as long as a conditional statement which is tested each time through the loop remains true. Often, however, the programmer would like to execute this loop some exact number of times. So, in this case, a counter is used to count how many times the loop has been executed. The counter is initialized to a value (usually 0), and then is incremented each time through the loop, and finally is tested each loop to see if it has reached the desired number. The for loop is designed to automate these stages of initialization, change of a counter each loop, and test of a counter each loop in the following syntax:
for (initilization statement; continue-looping-if-true statement; done once-per-loop statement)
computer instruction to loop; or for (initilization statement; continue-looping-if-true statement;done once-per-loop statement){
first computer instruction to loop;
second computer instruction to loop;
third computer instruction to loop; } Here, an example is probably much easier to understand: for(i = 0; i < 100; i++){ document.write(i); } This example literally reads: "set i to zero, then loop as long as i is less than 100, incrementing i by one each time through the loop."
The while loop is much easier, with a syntax that simply says, while a conditional statement is true loop on a set of instructions:
while(conditional statement) { first computer instruction to loop; second computer instruction to loop; third computer instruction to loop; }
An alternate version of this loop was added in Javascript 1.2 t allow C and Java programmers to use a more familiar syntax. Other than the order change and the addition of the "do" in front, the loop works exactly the same way.
do { first computer instruction to loop; second computer instruction to loop; third computer instruction to loop; } while( conditional statement)
The while loop will be covered further in the first set of examples.
Topic Heading
Topic content. Highlighted content.