PDE - Iterations Trip You Up? (Lesson) - is this one missing something
Types of Iterations / Loops
Another word for iteration is a loop. A loop or iteration makes whatever is in the block of code following the loop code statement happen over and over and over again.
When we walk, we step left foot, right foot or right foot, left foot over and over again until we reach our destination. The repetition stops with the arrival at the desired location.
On this lesson page we will learn of iterations/ loops that stop and when we need to control how to stop them or whether the loop structure handles this for us.
Some words used to create a loop are:
-
-
- count
- do
- repeat
- while
- for
-
What loops do what?
- The code words count (Alice code) and repeat (other language) tell the loop to do the block of code within the loop statement the number of times indicated. The iteration automatically stops after the indicated number of times. These hardcoded numbers could be a variable initialized to the starting value.
- count up to 4
executes the block of code that would be here 4 times, counting by up by 1
-
- repeat 5 times
- executes the block of code that would be here 5 times
- repeat 5 times
- the while code word indicates to keep going in the loop until a condition of truth associated with the while statement is not true
- while cnt > 3
executes a block of code inside this statement until variable cnt is 3 or or less
-
- The while loop requires you to create a control statement to get out. Otherwise if nothing is done to x and it started out < 3, the loop will continue on to infinity. Hmm... not good as the computer will run out of memory and the program lock up.
- Control the while loop by decrementing the cnt in the block of code within the while loop/iteration. Remember that the evaluation of the right side is stored in the left side in computer science programming. Now you have the loop working correctly.
wholeNumber cnt = 8
while cnt > 3
block of code to do
cnt = cnt - 1
- the do code word (not used in Alice) is like the while loop except the check for whether to continue is at the end block of code to execute. Thus a do loop always executes one (1) time until the cnt variable is checked, no matter the value of cnt.
- do
block of code to execute
until cnt < 8
- for iterations come in two different types. each -------- in together and for each ------ in ------
- each -------- -------- in ------- together
- each (object class) (name you create for the objects) in (an array you created) together
- Example:
- This gets all of the items you want in the array and does the block of code under the statement for all of the objects at the same time.
- Implied for, the SModel is grouping bipeds and quadrapeds together in an array called allAnimals, the variable allAnimalsEach is used to activate all of the animals in the array with the same name to do what you want (indices are handled automatically by the statement for each of the objects).
- The block of code is associated with each animal. You have to supply the grouping of animals (SModel), a name to call the animals collectively (in this case allAnimalsEach) and the array of SModel that you wish to use in the block of code (allAnimals).
- This does create a memory load for processing all of this and having it ready for all of the objects when the program is run
- Time is not of the essence here and depending on the size of the array and memory available, a large array may not run on your computer, especially if you are using up memory with lots of screens and / or other programs open.
- each -------- -------- in ------- together
-
- for each -------- -------- in -------
- Example:
- This gets all of the items you want in the array and does the block of code under the statement for all of the objects one after another starting with the first object in the array.
- the SModel is grouping bipeds and quadrapeds together in an array called allAnimals, the variable name allAnimalsEach (a name you create) to activate all of the animals in the array to do what you want (indices are handled automatically by the statement for each of the objects).
- This structure does not have the memory issue of the prior one as the object does the code in the block actively, not with the rest of the objects. Each object runs the loop block individually.
- Example:
- for each -------- -------- in -------