Iteration in Practice
Lab: Building Blocks of Programming
Video Runtime: 12:51
Let’s put iteration into practice. In this episode, you will learn about the different iterator constructs in each language. Then you’ll see them in action.
Your key takeaways are:
- for, each, foreach instructions provide iterations through arrays, collections, or objects
- while, do/while, until instructions provide looping until the conditional expression is false
Study Notes
Types of Iterators
There are several types of iterators:
- for loop
- foreach
- do/while
- while
- each
Iterators for each language:
Language | for | foreach, each | do/while, while, until |
---|---|---|---|
PHP | Yes | Yes | Yes |
JavaScript | Yes | Yes | Yes |
Ruby | Yes | Yes | Yes |
Python | Yes | Yes | |
C | Yes | Yes | |
C# | Yes | Yes | Yes |
Visual Basic | Yes | Yes | Yes |
For Loop
In the following code:
for ( counter = 0; counter < 10; counter++ ) {}
This is how it breaks down:
- for: instruction keyword
- counter = 0: “counter” is the variable that holds the number of loops, statement initializes the variable
- counter < 10: test if counter is within the threshold, threshold (limit)
- counter++: increment the counter
Foreach Loop
In the following code:
foreach ( variable as key => value ) {}
This is how it breaks down:
- foreach: instruction keyword
- variable: represents the collection
- key: current key
- value: current value
JavaScript Foreach/Each Loop
In the following code:
for ( variable of object ) {}
This is how it breaks down:
- for: instruction keyword
- variable: current value
- object: represents the collection
In the following code:
variable.forEach(function ( value, key ) {});
This is how it breaks down:
- variable: represents the collection
- forEach: instruction keyword
- value: current value
- key: current key (index)
While Loop
In the following code:
while ( condition ) {}
This is how it breaks down:
- while: instruction keyword
- condition: conditional expression
The while loop will iterate as long as the condition is true.
Do/While Loop
In the following code:
do { | |
// do work | |
} while ( condition ) |
This is how it breaks down:
- do and while: instruction keyword
- condition: conditional expression
Unlike the while loop, the do/while will do the work within the code block at least once, regardless of the conditional in the while. Why? Because the conditional is checked after the code block.
Your best friend is code, Tonya is making the introductions.
Episodes
Total Lab Runtime: 02:17:55
- 1 Lab Introductionfree 06:21
- 2 User-Friendly Expression of Informationfree 22:42
- 3 Fundamentals of Syntaxfree 08:57
- 4 Decision Branching - if/then/elsefree 10:39
- 5 Decision Branching – switch/casefree 07:47
- 6 Basics of Iterationfree 14:14
- 7 Iteration in Practicefree 12:51
- 8 Order of Executionfree 10:55
- 9 Abstractionfree 11:45
- 10 Scoping – Who Can Communicate With Whomfree 31:44