Skip to content
Computer Science · Grade 9

Active learning ideas

Nested Loops and Iteration Patterns

Active, hands-on work helps students grasp how nested loops interact because the visual outputs make abstract iteration concrete. When students trace patterns on paper or debug code side-by-side, they see how outer and inner loops multiply their runs, building accurate mental models faster than lectures alone.

Ontario Curriculum ExpectationsCS.HS.AP.7CS.HS.CT.8
20–45 minPairs → Whole Class4 activities

Activity 01

Stations Rotation30 min · Pairs

Pair Debug: Pattern Tracer

Pairs receive code with nested loops producing incorrect patterns, like a square instead of a triangle. They trace execution step-by-step on paper, identify errors in conditions, and fix the code. Test runs confirm corrections.

Analyze how nested loops can generate complex patterns or process multi-dimensional data.

Facilitation TipDuring Pair Debug: Pattern Tracer, ask each pair to mark up the code with colored arrows showing which line runs when, using one color per loop level.

What to look forPresent students with a code snippet containing nested loops and a specific output. Ask them to predict the final output or identify the number of times a specific line of code inside the inner loop will execute. For example: 'Given this code, how many times will 'print("*")' be executed?'

RememberUnderstandApplyAnalyzeSelf-ManagementRelationship Skills
Generate Complete Lesson

Activity 02

Stations Rotation45 min · Small Groups

Small Groups: Flag Designer

Groups use nested loops to code a pixel art Canadian flag pattern with colors. Start with a 20x13 grid outline, add loops for red bands and white square. Share and run codes on shared screens.

Design a program that uses nested loops to create a specific output pattern.

Facilitation TipFor Flag Designer in small groups, provide graph paper and colored pencils so students first sketch their flag before coding, linking visual design to loop structure.

What to look forProvide students with a simple pattern (e.g., a right-angled triangle of asterisks). Ask them to write pseudocode or actual code for a nested loop structure that would generate this pattern. Include a question: 'What is the relationship between the outer loop counter and the inner loop's range?'

RememberUnderstandApplyAnalyzeSelf-ManagementRelationship Skills
Generate Complete Lesson

Activity 03

Stations Rotation20 min · Whole Class

Whole Class: Iteration Predictor

Display nested loop code snippets with varying limits. Class predicts total inner loop runs via think-pair-share, then runs code to verify. Tally accuracy on board.

Predict the number of iterations a nested loop structure will execute given its conditions.

Facilitation TipIn Iteration Predictor, have students hold up whiteboards with their predicted totals before revealing the correct count, making misconceptions visible before discussion.

What to look forPose the question: 'Imagine you have a list of student grades for multiple subjects. How would you use nested loops to calculate the average grade for each student? Discuss the roles of the outer and inner loops in this process.'

RememberUnderstandApplyAnalyzeSelf-ManagementRelationship Skills
Generate Complete Lesson

Activity 04

Stations Rotation35 min · Individual

Individual: Shape Gallery

Students code three shapes: hollow square, diagonal line, pyramid using nested loops. Adjust sizes and add user input for dimensions. Submit runnable codes.

Analyze how nested loops can generate complex patterns or process multi-dimensional data.

Facilitation TipFor Shape Gallery, display student outputs on a wall with sticky notes for peer feedback, focusing comments on loop clarity and pattern accuracy.

What to look forPresent students with a code snippet containing nested loops and a specific output. Ask them to predict the final output or identify the number of times a specific line of code inside the inner loop will execute. For example: 'Given this code, how many times will 'print("*")' be executed?'

RememberUnderstandApplyAnalyzeSelf-ManagementRelationship Skills
Generate Complete Lesson

A few notes on teaching this unit

Teach this topic by having students alternate between tracing existing code and building their own, using a gradual release approach. Start with fully worked examples where students annotate loop behavior, then move to guided tasks with partial code to fill in, and finally independent design. Avoid overwhelming students by keeping patterns small at first and increasing complexity as confidence grows. Research shows that immediate, visual feedback—like seeing a star printed on the screen—reinforces correct loop relationships more effectively than abstract explanations alone.

By the end of these activities, students will confidently trace nested loops, predict iteration counts, and design programs that generate specified patterns or process 2D data. They will explain how loop conditions control output and justify their reasoning with evidence from their code or drawings.


Watch Out for These Misconceptions

  • During Pair Debug: Pattern Tracer, watch for students who assume the inner loop runs once per outer iteration.

    Have pairs count the total prints on their traced grid and compare it to their initial guess, showing how multiplication of runs occurs. Ask them to recount using the loop conditions to correct the underestimation.

  • During Flag Designer, watch for students who think changing the outer loop will uniformly slow down the inner loop.

    Ask groups to adjust only the outer loop’s range while keeping the inner loop identical, then observe that the inner pattern’s speed stays constant. Highlight the separation of loop scopes by pointing to the code segments.

  • During Iteration Predictor, watch for students who believe nested loops are only useful for graphics.

    Provide a 2D array of student grades and guide the class to write code that calculates each student’s average using nested loops. Show how the same structure processes data as it does patterns, reinforcing flexibility.


Methods used in this brief