ConceptStep · learning platform for computer science

Nested loops

C programming · Lesson

C · lesson 11 / 27

Nested loops

Observe an inner FOR completing all its iterations for every outer FOR iteration.

1 · Observe and predict

The concept

The inner counter restarts when the outer one changes. Two 2×2 loops produce four visits.

Mental model:

outer i → inner j → output cell → j repeats → i advances

2 · Flowchart First

Follow the same algorithm as a flowchart

Use Start and Step: when the active block changes, ConceptStep also highlights the C lines representing that same step.

Loading flowchart…
3 · Flowchart ↔ C · Run in Page

Lab 1 — Run the example program

Run C executes only the example shown on the left, using stdin on the right. The coding challenge below has a separate editor and test button.

Nested loops Start the flowchart to synchronize the code
                        1#include <stdio.h>
                        2 
                        3int main(void)
                        4{
                        5    int righe;
                        6    int colonne;
                        7    scanf("%d", &righe);
                        8    scanf("%d", &colonne);
                        9 
                        10    for (int i = 1; i <= righe; i = i + 1)
                        11    {
                        12        for (int j = 1; j <= colonne; j = j + 1)
                        13        {
                        14            printf("%d\n", i * 10 + j);
                        15        }
                        16    }
                        17 
                        18    return 0;
                        19}
Ready. Make a prediction before running.

                    
                

Lab 2 — Coding challenge: write your code — Rectangular grid

Read nonnegative rows and columns; use two nested loops and print the total cell count.

This is a different program from lab 1. Complete the starter code: Check all test cases compiles this editor and tests every input, including boundary cases.

4 · Guided practice

Check your prediction

With 2 rows and 2 columns, how many outputs are produced?

Challenge 1 / 5 · Understanding

Progress: 0 / 5 · correct: 0

Your competency map

What you need to know for Nested loops

ConceptStep separates what is required from what is recommended or useful as reinforcement. Only required prerequisites block your progress.

Current topic Strong skill Review due Needs reinforcement To be mastered Optional support

Relationships: solid line = required · dashed line = recommended · dotted line = reinforcement.

GUIDED LEARNING PATHS

Want to choose the next skill to develop?