1 / 9

ICS 3U

ICS 3U. The Counted Loop FOR LOOPS. For Loops - Introduction. Loops are used to repeat a block of statements. A For Loop is used to execute the statements inside a fixed number of times. A For Loop in ActionScript looks like: for (initialization; condition; increment) {

mauli
Download Presentation

ICS 3U

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICS 3U The Counted Loop FOR LOOPS

  2. For Loops - Introduction Loops are used to repeat a block of statements. A For Loop is used to execute the statements inside a fixed number of times. A For Loop in ActionScript looks like: for (initialization; condition; increment) { Statement(s) to be repeated } Initialization performed only the first time the loop is executed, sets the starting value of the variable that is used to control the loop Condition a Boolean expression that is evaluated before each execution of the loop, tells when to stop the loop Increment performed after each execution of the loop, it advances the value of the loop control variable

  3. For Loops - Example A Simple Count to 10 vari: int; for (i =0; i <= 10; i++)  adds one { trace(i); }

  4. For Loops – Example Counting Backwards 10 to 1 vari: int; for (i = 10; i >=0; i--)  subtracts one { trace(i); }

  5. For Loops - Example Generating 25 random numbers between 1 and 100 varrandnum: int; vari: int; varhigh:int = 100; var low: int = 1; for (i = 0; i <= 25; i++) { randnum = Math.floor(Math.random ...); trace (randnum); }

  6. For Loops - Example Write a For Loop that will: Output all numbers between 1 and 100 that are divisible by 3.

  7. Additional Notes • Can I only increment my loop control variable by 1? Can I go up by 5’s? 2’s? • A single letter variable?! • we typically use these as loop control variables in the for loops (counted loops). • It is ok in these instances • Accumulators • Many times in loops we want to accumulate a value. Keep a running total, keeping a score, counting the number of even numbers, etc.

  8. Accumulators Write a for loop that will total 5 random numbers generated. Can also use subtract: newvalue= newvalue – randnum; OR newvalue -= randnum;

  9. Exercises See Website for the exercises ... NOW – Get to WORK!!

More Related