Lesson 10: Loop Structures
Loops
Continuing our survey of utilitarian structures in JavaScript we turn to discuss how we create repeating blocks of code.
Read Duckett, Chapter 4, pp.170-182.
This is another short section of reading but it packs a big power punch for processing. However, jQuery also builds on this for some of our most common use cases. Very often we want to repeat an operation for each item we selected using jQuery. Thankfully we have another shortcut for such common operations.
Read Duckett, Chapter 7, pp.324-325.
To debrief, note the following:
- Loops must have an "exit clause" so that they don't run on forever and ever.
- The most basic of such clauses is a counter that changes on each iteration (like in a
for
loop). IN such a case, the loop is set up to repeat as long as that counter is within a certain range. - The most common of such clauses is to continue an operation for each item in a list. As long as we have items left in the list we'll continue the loop. When we reach the end, the loop stops repeating.
- More complex loops are useful but take extra thought. Just keep in mind:
- A loop must have a condition that will eventually change and cause the loop to stop.
- Therefore, the declaration block must include a logical possibility for this condition to change.
Study Questions
- What are loops in programming languages like JavaScript and what do they enable us to do?
- SKILL: Create vanilla JavaScript loops using
for
,while
, anddo... while
. - SKILL: Loop through a jQuery result set using
.each()
.
Presentation and Demo
Watch this presentation and play along in the demo provided in CULearn. Use this handout to take notes.