CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Python · 7 min · free

Nested Loops and Pattern Programs

7 min4 KwickClipsFull text belowFree
Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Learn how a nested loop works, how to print star, number and letter patterns, how to trace nested output by hand, and how to find primes. The inner loop restarts and runs fully for every turn of the outer loop.

Follows the syllabus of: CBSE Class 9 Artificial Intelligence (417), CBSE Class 11 Computer Science (083), CBSE Class 11 Computer Science Essentials (083), ICSE Class 10 Computer Applications

On screen in this lesson

What is a nested loop?

A loop written inside another loop
The outer loop picks a row
The inner loop runs fully for that row
Total runs = outer count x inner count

The grid, cell by cell

rowcol valuesPrinted line
11, 2, 31 1 1 2 1 3
21, 2, 32 1 2 2 2 3
31, 2, 33 1 3 2 3 3

Two print tricks

end="" keeps the cursor on the same line
print() alone moves to the next line
Inner print makes columns
Outer print() makes rows

Trace table

rowcolPrints row * col
111
122
212
224

What is a prime number?

A prime has exactly two factors: 1 and itself
2, 3, 5 and 7 are prime
1 is not prime; 9 is not prime (3 x 3)
Test: does any number from 2 to num - 1 divide num?

Common mistakes

Putting print() inside the inner loop
Forgetting end="", so every star is on a new line
Using the outer variable name again inside
Forgetting that range stops before the end value

Quick answers

How many times does the inner body run for 3 rows and 4 columns?

12 times, because the inner loop restarts for every outer turn.

Why does every star land on a new line?

end="" is missing, so print moves to a new line each time.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. How does a program print a triangle of stars, one star at a time? It needs a loop inside a loop. Today we learn nested loops, star, number and letter patterns, tracing by hand, and a prime number program.

First, a new term. A nested loop is a loop written inside another loop. Think of a school timetable, where the outer loop picks the day. For each day, the inner loop goes through every period, from first to last. So if there are three rows and four columns, the inner body runs three times four, which is twelve times.

Here the outer loop variable is row, and the inner loop variable is col, short for column. Range of one comma four gives one, two and three. Look at the indentation carefully. The inner print is inside both loops. The empty print is inside only the outer loop, so it ends each row.

Now let us watch the grid fill up. When row is one, col runs one, two, three, and then the line ends. When row is two, col starts again from one and runs to three. When row is three, the same thing happens once more. The inner loop always restarts and runs fully for every outer turn.

Here is the outer loop drawn as a flowchart. First, row is set to one. The diamond asks, is row less than or equal to three? If yes, the whole inner loop runs, row goes up by one, and we ask again. If no, the flow goes down and the program stops.

Pattern programs need two small print tricks. Normally print moves to a new line after printing. Writing end equals empty quotes keeps the cursor on the same line. A print with empty brackets prints nothing and just moves to the next line. The inner print builds the columns of one row. The outer print, after the inner loop, starts the next row.

Our first pattern is a star triangle. The outer loop variable row goes from one to four. The inner loop runs row times, so row one gets one star. Row two gets two stars, and row four gets four stars. The rule is simple: the row number decides how many stars.

Next, a number triangle. This time the inner loop prints the variable num, not a star. Range of one comma row plus one gives the numbers from one up to row. So row three prints one, two, three. Pause and predict. What would print if we wrote print of row instead of print of num? Every line would repeat its own row number, like three, three, three.

Now a letter pattern. Every character has a number code, and capital A has the code sixty five. The function chr turns a code back into its character. So chr of sixty five is A, and chr of sixty six is B. As pos, the position, goes zero, one, two, we print A, B and C.

To turn the triangle upside down, only the outer loop changes. Range of four, zero, minus one counts down four, three, two, one. It stops before zero. The inner loop is exactly the same as before. So the first row has four stars, and the last row has one.

Exams often ask you to predict the output of a nested loop. The safest way is a trace table, which is a table where you write each variable at every step. Here row goes one and two, and col also goes one and two. The program prints row times col each time. Let us trace it on the next slide.

Write one line in the table for every run of the inner body. First, row is one and col is one, so it prints one. Then row stays one and col becomes two, so it prints two. Now the inner loop is over, so row becomes two and col restarts at one. It prints two, and finally two times two prints four.

Our last program finds prime numbers. A prime number has exactly two factors, one and the number itself. Two, three, five and seven are prime. One is not prime, because it has only one factor, and nine is not prime because three divides it. To test a number, we try every number from two up to one less than it.

The outer loop picks each number, num, from two to nineteen. We first assume num is prime, so prime equals True. The inner loop tries every divisor, div, from two to num minus one. The percent sign gives the remainder, and double equals compares it with zero. If the remainder is zero, div divides num, so prime becomes False. After the inner loop, if prime is still True, we print num.

Some exams ask for nested loops in pseudocode. The arrow means store a value. Each for loop closes with the word next, followed by its own variable. The inner loop closes first, then the outer loop.

Watch out for four common mistakes. If the empty print goes inside the inner loop, every row breaks too early. If you forget end equals empty quotes, each star lands on its own line. If the inner loop reuses the outer variable name, the rows get mixed up. And remember, range always stops just before its end value.

Let us revise what we learned today. The inner loop runs fully for every turn of the outer loop. The outer loop makes rows, and the inner loop makes columns. End equals empty quotes stays on the line, and an empty print starts a new line. To predict output, trace the loops in a table. And to find primes, an inner loop tests every divisor. Try making a pyramid pattern yourself.

Courses that teach this

CourseUnit
CBSE Class 9 Artificial Intelligence (417)Part C - Practical Work (Python)
CBSE Class 11 Computer Science (083)Computational Thinking and Programming - I
CBSE Class 11 Computer Science Essentials (083)Computational Thinking and Programming - I
ICSE Class 10 Computer ApplicationsRevision of Class IX Syllabus
Cambridge IGCSE Grade 9 Computer Science (0478)7. Algorithm Design and Problem-Solving

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →