Ics 3u
Download
1 / 9

ICS 3U - PowerPoint PPT Presentation


  • 127 Views
  • Uploaded on

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) {

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' ICS 3U' - mauli


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Ics 3u

ICS 3U

The Counted Loop

FOR LOOPS


For loops introduction
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


For loops example
For Loops - Example

A Simple Count to 10

vari: int;

for (i =0; i <= 10; i++)  adds one

{

trace(i);

}


For loops example1
For Loops – Example

Counting Backwards 10 to 1

vari: int;

for (i = 10; i >=0; i--)  subtracts one

{

trace(i);

}


For loops example2
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);

}


For loops example3
For Loops - Example

Write a For Loop that will:

Output all numbers between 1 and 100 that are divisible by 3.


Additional notes
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.


Accumulators
Accumulators

Write a for loop that will total 5 random numbers generated.

Can also use subtract:

newvalue= newvalue – randnum; OR newvalue -= randnum;


Exercises
Exercises

See Website for the exercises ...

NOW – Get to WORK!!


ad