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: Loop

The second control flow structure that you will learn is looping. There are different ways to get a program to repeat a block of instructions over, and over again...

The basic structure of a "for" loop:

for(nNum = 0;nNum<5;nNum++){
nSum +=nNum;
trace(nNum+ "\t" + nSum);
}

First, notice that nNum is initialized to 0. The program will stay in the loop while nNum is less than 5. Each time through the loop, nNum is incrememted by 1 (nNum++).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.

Do and While statements:

while(condition) Many statements that will occur while the condition is met. For example:
        nNum = 0;
nMax = 6;
while(nNum <nMax){
trace(nNum);
nNum++;
}
or:  
do{     
     //stuff  
}while(condition); // notice the semicolon here
nNum = 0;     
nMax = 6;     
do{     	
		trace(nNum);     	
		nNum++;     
}while(nNum <nMax);

The difference between the “while” loop and the “do/while” loop is that in the “while” loop the condition is checked before entering the loop. The “do/while” loop will be entered and then the condition is checked after the first run through the loop.

One way of entering a “while” loop is to create a boolean variable and set it to a default value that will allow the “while” loop to be entered, only when its value changes within the loop, will the loop be exited. For example:

bValid = false;     
while(!bValid){          // stuff that will happen until bValid is true     }  

while(true): Using this while statement means "while true", which is an endless loop. This is the fastest way to enter a loop, end then gives you strict control of the exact line that you exit the loop by way of the "break" command. Don't do the "while(true)" structure without a "break".

break You can always exit any type of looping structure by using an if structure with a break command. For example:

if(sExit=="yes") break;

This is my favourite flavour of  breaking out of a loop, since it gives you perfect control of when the loop will be broken out of. When using a "for" or "while" loop structure, the loop will only be broken out of when the condition is tested at the end or beginning of the loop. I like to control the exit from within the loop. Therefore, I like the while(1) with a controlled break.