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

Programing (Operations Using Nested Loop) ... Syed Fasih

This document is a lab report submitted by student Syed Fasih Abbas for their Introduction to Information Technology lab. The document outlines two tasks involving nested loop operations - printing a numeric pattern using nested for loops and printing an asterisk pattern using nested for loops. It provides code explanations for how to approach each task using nested loops to iterate through multiple conditions and print the desired patterns.

Uploaded by

Fasih Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Programing (Operations Using Nested Loop) ... Syed Fasih

This document is a lab report submitted by student Syed Fasih Abbas for their Introduction to Information Technology lab. The document outlines two tasks involving nested loop operations - printing a numeric pattern using nested for loops and printing an asterisk pattern using nested for loops. It provides code explanations for how to approach each task using nested loops to iterate through multiple conditions and print the desired patterns.

Uploaded by

Fasih Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

DEPARTMENT OF AVIONICS ENGINEERING

SUBJECT : Introduction to Information Technology


Lab
SUBJECT CODE : 108444
LAB NO : 08

TITLE : Operations using Nested Loop

SUBMITTED TO : Sir Laal Said


SEMESTER : 1st
SECTION : B

Marks Obtained

NAME Syed Fasih Abbas


REGISTRATION # 220701054
LAB REPORT 08
PERFORMANCE
TOTAL MARKS

DEADLINE: 6th December 2022


DATE OF SUBMISSION:

Introduction to IT Lab Experiment # 8


Objectives:
 The objective of this lab is to learn how and where nested loop can be used and why it is
needed
 To understand technical method for working with multidimensional data structures.
 After this lab, we will be able to understand how to print two-dimensional arrays,
iterating a list that contains an embedded list.

Introduction:
One loop inside another loop is called a Nested Loop. In Python, different nested loops can be
formed using for and while loops - a for-in loop inside another for-in loop, a while loop inside
another while loop, a for-in loop inside a while loop, a while inside the for-in loop.

Why do we need nested loops?


Similar to Loops in Python, nested loops execute a block of statements until the conditions
return to be False. These loops are used when we need to execute a block of statements under
multiple conditions. We can use these loops to traverse through multi-dimensional arrays,
matrix values, tabular data.

Flowchart to define the working


of nested loops:
Here is a flow diagram to demonstrate how nested
loops work in Python.
Python Nested for loop (for inside for)
“One for loop inside another for loop is called a nested for loop”

 Nested for loops are used when the number of iterations to execute a block of statements
are known beforehand.

Syntax for nested for loop


for variable1 in sequence:
for variable2 in sequence:
statement (s)

Example for nested for loop


Multiplication table for more than one number or range of numbers can be printed using a
nested for loop as shown below.

# program to print the first three multiplication tables for i in range(1, 4):
print("i =", i, "->", end = " ")
for j in range(1, 11):
print("{:2d}".format(i * j), end = " ")
print() # similar to new line printing after each multiplication tableOutput:
i = 1 -> 1 2 3 4 5 6 7 8 9 10
i = 2 -> 2 4 6 8 10 12 14 16 18 20
i = 3 -> 3 6 9 12 15 18 21 24 27 30

Introduction to IT Lab Experiment # 8


Python Nested while loop (while inside while)
“One while loop inside another while loop is called a nested while loop”

 Nested while loops are used when the number of iterations to execute a block of
statements are not known beforehand.

Syntax for nested while loop


while condition:
while condition:
statement (s)

Example for nested while loop


# program using nested while loop
a = 1while(a <= 3):
b = 1while(b <= 3):
print(b, end = ' ')
b = b + 1
print()
a = a + 1

Output:
1 2 31 2 31 2 3

Other Nested Loops (for inside while and while inside for)
Python allows us to write a for loop inside a while loop and a while inside a for loop as shown
below.

1) for inside while


# program which has a for loop inside a while loop
list1 = [40, "FACEPrep"]
a = len(list1)
i = 0while(i < a):
for j in list1:
print(j)
i = i + 1

Introduction to IT Lab Experiment # 8


Output:
40
FACEPrep
40
FACEPrep

 Here, the while loop executes until the length of the given sequence. For each iteration of
the while loop, for loop prints all the elements of the given sequence.

2) while inside for


# program which has a while loop inside a for loop
list1 = [40, "FACEPrep"]
a = len(list1)
for i in list1:
i = 0while(i < a):
print (list1[i]) # printing the element in the index i
i = i + 1

Output:
40
FACEPrep
40
FACEPrep

 Since the given list contains 2 elements, the for loop executed two times and we got the
output twice. while loop executes until the length of the given sequence.

Control statements in nested loops:


Python allows us to use control statements in nested loops to control the execution flow of the
program.

Break in nested loop

In Python, break is used to terminate the current iteration and proceed with the next iteration. If a
break statement is used inside an inner loop, then the current iteration of the inner loop gets
stopped. If it is used inside an outer loop, then the current iteration of the outer loop gets stopped.

Introduction to IT Lab Experiment # 8


# program which has a break statement inside an inner loop

list1 = [40, "FACEPrep", 0, 30.45]


a = len(list1)
i = 0while(i < a):
for j in list1:
if(j == 0):
break
print(j)
i = i + 1

Output:
40
FACEPrep
40
FACEPrep
40
FACEPrep
40
FACEPrep

 Here, the while loop executes four times. Hence we got the output four times. Also for
loop breaks when if condition becomes True.

Continue in nested loop:


In Python, continue is used to skip the execution of the rest of the statements in that loop and
proceed with the next iteration. If a continue statement is used inside an inner loop, then the rest
of the statements in the inner loop gets skipped. If it is used inside an outer loop, then the rest of
the statements in the outer loop get skipped.

# program which a continue statement inside an inner loop


list1 = [40, "FACEPrep", 0, 30.45]
a = len(list1)
i = 0while(i < a):
for j in list1:
if(j == 0):
continue
print(j)
i = i + 1

Introduction to IT Lab Experiment # 8


Output:
40
FACEPrep
30.4540
FACEPrep
30.4540
FACEPrep
30.4540
FACEPrep
30.45

 Here, the for loop skips the element zero and prints the remaining elements of the
sequence until the length of the sequence.

Nested Loop FAQs

1) What is a nested loop in python?

One loop inside another loop is called a nested loop. These nested loops are mainly used to
traverse through multi-dimensional arrays, matrix values, tabular data.

2) How to break nested loops in python?

We can break nested loops in Python using the control statement ‘break’. Whenever an
interpreter comes across the word ‘break’, it stops the current iteration and proceeds with the
next iteration.

3) Can we use while loop inside for loop in python?

Yes, we can use a while loop inside for loop and for loop inside a while loop.

Introduction to IT Lab Experiment # 8


Introduction to IT Lab Experiment # 8
LAB Tasks

TASK 1
Print the fallowing output using nested for
loop

22

333

4444

Code Explanation:

 Initializing a variable called “depth” and give it a value of 5 with the help of command:

depth = 5

 Going ahead, we have Nested for loop. We have the outer for loop to set the depth or
number of rows in this pattern. When we use the command:

for number in range(depth):

 This helps us to get a list of numbers 0-4. Because the depth value is 5, this means that 5
is exclusive and that is why the range goes from 0-4.
 Then, we set the inner for loop using this command:
for i in range(number):

 In the inner for loop, initially, the value of the number is 0, that’s why we skip the first
row. Then we go back to the outer for loop and the number value becomes 1. Once the
number value is 1, we head inside the inner loop and print 1.

Introduction to IT Lab Experiment # 8


 After that number value is incremented again & it becomes 2, then we head to the inner
loop and print 2 2.
 Similarly, we go back to the outer loop and this time number value becomes 3. After this
inside for loop, we print 3 3 3.
 Again we go back to the outer loop, then the value of “number” becomes 4 & we print 4
4 4 4 with the inner loop.

Output:
This program gives us specific pattern of required numbers

TASK 2
Print the * pattern using nested for loops

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

Code Explanation:

Introduction to IT Lab Experiment # 8


 The right Pascal triangle pattern is shown above. It can be clearly seen that it is made up
of an upper triangle and a lower triangle.

 So you can run 2 different loops one which creates the upper triangle and another which
creates the lower triangle.
Reference:
https://www.tutorialstonight.com/
Output:
python/pattern-program-in-
python
This program gives us right Pascal triangle (* pattern)

TASK 3
Print your name in 3 rows & columns

Code Explanation:

 Initializing a variable called “a” and assigned


a user provided value.
 Going ahead, we have Nested for loop. We
have the outer for loop to set the number of rows in this pattern. When we use the
command:

for x in range(3):
 This helps us to obtain 3 rows
 In the inner for loop, we have set the value of required columns we want.

for x in range(3):
 Using athematic operator (+) with “a” and stopping command by using end=”” &
breaking command and to exert in second line i.e. print ( ) we run our program.

Output:
This program provided my name and can provide any name in specific order as per user demand

Introduction to IT Lab Experiment # 8


TASK 4
Write a nested for loop program to print
multiplication table in Python

Code Explanation:
 In this program, the outer for loop is iterate
numbers from 1 to 10. The range() return 10
numbers. So total number of iteration of the outer
loop is 10.
 In the first iteration of the nested loop, the number
is 1. In the next, it 2 & so on till 10.
 Next, For each iteration of the outer loop, the
inner loop will execute ten times. The inner loop
will also execute ten times because we are printing multiplication table up to ten.
 In each iteration of an inner loop, we calculated the multiplication of two numbers.

Output:
Multiplication tables up to 10 are generated using nested loop.

Introduction to IT Lab Experiment # 8


TASK 5
Try to generate the following series program
using Nested Loop:

54321
4321
321
21
1

Code Explanation:

 Initializing a variable called “rows” and assigned a user provided value.


 Going ahead, we have Nested for loop. We have the outer for loop to set the number
of rows in this pattern. When we use the command:

for i in range(0,rows+1)):

 In the inner for loop, we have set the value

for j in range(rows-i,0,-1):

 Printing j and stopping command by using end=”” & breaking command and to exert
in second line i.e. print ( ) we run our program.

Output:
This program provide reverse number pattern of given task.

Introduction to IT Lab Experiment # 8


Conclusion

 A nested loop is a loop inside a loop. Therefore a nested for loop is a for loop inside
another for loop.
 The inner for loop is executed for each execution of the outer for loop. If the outer for
loop has range(a) and the inner for loop has range(b), then the inner for loop executes a*b
times.
 Nested for loops can be used to solve problems such as prime numbers in a given range,
pattern printing, multiplication table printing, etc.

Introduction to IT Lab Experiment # 8

You might also like