Skill Sets:

Home

  1. Intro to Flash
  2. User Input
  3. Fancy Controls 1
  4. Control Flow: if
  5. Control Flow: loop
  6. Nested Structures
  7. Graphics 1
  8. Arrays
  9. Strings
  10. Mouse Movement
  11. Functions
  12. Grouping Images
  13. Graphical Support
  14. Animation
  15. Physics
  16. 2010 Grade 10 Summative showcase
  17. 2010 Grade 11 Summatives
  18. 2011 Grade 10 Summatives
  19. 2011 Grade 11/12 Summatives
  20. Sept 2011 Grade 11 Summatives
  21. Sept 2011 Grade 10 Summatives
  22. Feb 2012 Grade 10 Summatives
  23. Sept 2012 Summatives
  24. Feb 2013 Grade 12 Summatives
  25. Feb 2013 2OI Summatives
  26. Sept 2013 Summatives
  27. Feb 2014 Summatives

Control Flow: if

So far, we've used different ways for the user to input information, and then did some calculuations based on this information. Now, we're going to move into controlling the flow of the program. The two main structures for this are "if" and "loop". "If" checks the value of a variable and then branches to different parts of the program based on the value. "Loop" (the next skill set) allows the program to repeat instructions for a specified number of times.

Whenever possible, I will try to introduce new components with each new programming concept. The two components that I will introduce in this section include the "check box" and the "radio button". Both of these components support the concept of "if" structures.

The basic structure of “if”:

if(condition) {       
	// stuff  }  
else if( next condition)  {      
	 // other stuff  }  
else  {       
	// stuff to do if the first two conditions aren't met  
}

Notice that the condition that you are testing is inside round brackets. IF that condition is true, then the program will execute what is within the brace brackets that follow. Also notice that all actions within brackets are indented. This must be done so that your code is clear to read. It will work if you don't indent, but it is important to be read by others. For example:

if(nMark>80)     {        
	trace("You got an A" );     
}     
else if( nMark>49)     {        
	trace("You passed!!" );     
}     
else     {        
	trace("You failed!!" );     
}   

You can see here that the "condition" that is tested is the nMark value.

A quick note on equality
In AS, you must be explicit about equality. In AS, the equal sign is referred to as the “assignment operator”; you assign a value to a variable. To check for equality, you need to use double equal signs. For example:

if(nMark==50)  {     
	trace("You barely passed!!" );  
}

Part of predicting your output would be testing the above if statement with a single equals sign, therefore:

if(nMark=50)  {     
	trace("You barely passed!!" );  
}

Test it out. Did it work? Good, now try checking for nMark = 21.
What happened?

Brace brackets are only required if more than one line is to be executed within the "if" structure. For example:

if(nMark>80)   	
	trace("You got an A!!" );   
else if( nMark>49)   	
	trace("You passed!!" );   
else   	
	trace("You failed!!" );

If your program had to do 2 or more lines of code based on a student getting more than 80, then brace bracketes are required.

Relational Operators: relational operators will always return either true or false:
= = The double equal sign returns a value of true if the values on both sides of the equal signs are equal
!= not equal to
< Less Than and all the others
>= Greater than OR equal to

Logical Operators:
&& Logical AND (Conjunction)
|| Logical OR (Disjunction)
! Logical NOT: !bCondition

Wikipedia provides a great example of conjunction, and disjunction.

The switch Statement

The switch statement is a conditional control structure that uses the result of an expression to determine which statement to execute. The switch statement is sometimes preferable to the if-else-if statement because code may be easier to read.

Important points to remember when using a switch statement:

The following example demonstrates the general layout of a switch statement.  Notice that this switch statement falls through to the default case:

 var switchExpression:int = 3; 
 switch (switchExpression) { 
               case 0: 
               trace(0); 
               break; 
               case 1: 
               trace(1); 
               break; 
               case 2: 
               trace(2); 
               break; 
               default: 
               trace("Not 0, 1, or 2"); 
} 

The following switch statement prints the day of the week based on the day number returned by the Date.getDay() method. 

The getDay method is a method from the Date class.  This method returns the day of the week (0 for Sunday, 1 for Monday, and so on) of the specified Date object according to local time. Local time is determined by the operating system on which Flash Player is running.

 // This statement retrieves the current date and time and stores it in the variable someDate. 
 //var someDate:Date = new Date(); 
 //  This statement uses the getDay method to return an integer (0-6) to  //represent the day of week. The getDay method uses the information  //stored in the someDate variable that was just retrieved in the previous  //statement. 
 var dayNum:uint = someDate.getDay(); 
 switch(dayNum) 
 { 
               case 0: 
               trace("Sunday"); 
               break; 
               case 1: 
               trace("Monday"); 
               break; 
               case 2: 
               trace("Tuesday"); 
               break; 
               case 3: 
               trace("Wednesday"); 
               break; 
               case 4: 
               trace("Thursday"); 
               break; 
               case 5: 
               trace("Friday"); 
               break; 
               case 6: 
               trace("Saturday"); 
               break; 
               default: 
               trace("Out of range"); 
               break; 
}