0% found this document useful (0 votes)
9 views

Lecture Notes - Nested Loop (with Activiies)

Lectures notes in Introduction to Programming

Uploaded by

Jonathan Paga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture Notes - Nested Loop (with Activiies)

Lectures notes in Introduction to Programming

Uploaded by

Jonathan Paga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

NMF1- Introduction to Programming

Topic: Nested Loops

Lecture Notes: Nested Loops in Python

1. Introduction to Loops

A loop is a programming construct that repeats a block of code as long as a condition is met. In
Python, we have two types of loops:

 For loop: Iterates over a sequence (like a list, string, or range).


 While loop: Repeats as long as a condition is true.

2. What Are Nested Loops?

A nested loop is a loop inside another loop. The "outer" loop controls the overall repetition,
and the "inner" loop executes fully for every iteration of the outer loop. Nested loops can be
helpful when dealing with multidimensional data (like matrices or grids) or performing repeated
actions across multiple layers of iteration.

3. Syntax of Nested Loops

In Python, a nested loop follows the same syntax as a regular loop, but with an additional loop
inside the first one.

Example of a nested for loop:

for i in range(3): # Outer loop (iterates 3 times)


for j in range(2): # Inner loop (iterates 2 times for each outer loop iteration)
print(f"i: {i}, j: {j}")

Output:

i: 0, j: 0
i: 0, j: 1
i: 1, j: 0
i: 1, j: 1
i: 2, j: 0
i: 2, j: 1

Here, the outer loop runs 3 times, and for each iteration of the outer loop, the inner loop runs 2
times.

4. Common Uses of Nested Loops

Nested loops are commonly used for:


 Working with multi-dimensional arrays or matrices.
 Printing patterns or shapes.
 Processing grids, tables, or charts.

Example: Printing a Grid

for i in range(5): # Outer loop for rows


for j in range(5): # Inner loop for columns
print("*", end=" ") # Print "*" without moving to a new line
print() # Move to the next line after each row
NMF1- Introduction to Programming
Topic: Nested Loops

Output:

*****
*****
*****
*****
*****

5. Nested Loops with while Loops

Nested loops can also use while loops inside another while loop. The syntax is similar to for
loops.

Example:

i=0
while i < 3: # Outer loop
j=0
while j < 2: # Inner loop
print(f"i: {i}, j: {j}")
j += 1
i += 1

Output:

i: 0, j: 0
i: 0, j: 1
i: 1, j: 0
i: 1, j: 1
i: 2, j: 0
i: 2, j: 1

6. Time Complexity of Nested Loops

The time complexity of nested loops depends on the number of iterations in each loop. For
example, if the outer loop runs mmm times and the inner loop runs n times, the total time
complexity will be O(m×n).

 Example 1: Outer loop runs 3 times, inner loop runs 5 times:


o Time complexity: O(3×5)=O(15)

 Example 2: Outer loop runs nnn times, and inner loop also runs nnn times:
2
o Time complexity: O(n ) (quadratic time)

Learning Task: Students Activity

Activity 1: Multiplication Table (filename: act1_loop)

Write a program to print the multiplication table for a number n (up to 10). For example, if the
input is 3, it should print the following:

1*3=3
2*3=6
NMF1- Introduction to Programming
Topic: Nested Loops

3*3=9
...
10 * 3 = 30

Activity 2: Number Pattern (filename: act2_loop)

Write a program that prints a number pattern in the following format for n = 5:

1
12
123
1234
12345

Activity 3: Matrix Addition (filename: act3_loop)


Write a program that adds two 2x2 matrices.

Matrix A:

12
34

Matrix B:

56
78

Resulting Matrix:

68
10 12

Important:

Tips and Best Practices

 Indentation: Ensure that the indentation is consistent. In Python, indentation is crucial


for defining the blocks of code within loops.

 Break and Continue: You can use break to exit a loop early or continue to skip the
current iteration and move to the next.

 Avoid Deep Nesting: Too many nested loops can make the code harder to read and may
lead to performance issues. Try to refactor or optimize if possible.

Summary

 A nested loop is a loop inside another loop. The inner loop executes for each iteration of
the outer loop.
 Nested loops are useful for working with multi-dimensional data or performing repeated
actions.
 The time complexity of nested loops is the product of the number of iterations of the
outer and inner loops.
NMF1- Introduction to Programming
Topic: Nested Loops

 Practice with nested loops to get comfortable working with complex repetitive tasks.

Additional Resources

 Python Official Documentation


 GeeksforGeeks Nested Loop Tutorial
 LeetCode - Practice Nested Loops

You might also like