Learning object 13 of 100
Nested-loop pattern builder
Choose a number of rows and columns and a pattern, and see the nested loop that draws it, its output, and how many times the inner body runs.
Loading…
What it shows
The inner loop runs completely for every pass of the outer loop. With a fixed inner limit the inner body runs rows × columns times. When the inner limit depends on the outer counter, as in a triangle, each row has a different length.
Guided experiment
- Choose Rectangle with 3 rows and 4 columns. Predict the inner count first.
- Switch to Right triangle. Why is the column setting ignored?
- Choose Checkerboard and explain the rule
(r + c) % 2.
Predict
An outer loop runs 4 times and its inner loop 6 times. How many times does the inner body run?
Related
Lessons: Lesson 19: Nested loops and patterns
Projects: Project 9: Shopping Cart Price Calculator
Source: web/assets/js/objects/object-013.js in the companion repository.