Chapter 22 - for Repetition Structure
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material

Repetition Structure
- Repetition Structure
- Definition
- A Repetition Structure (a.k.a. Loop) is a feature of Python Programming Language that is used to perform Repetitive Python Programming Tasks
- Purpose
- The main purpose of Repetition Structure it to perform Repetitive Programming Tasks
- Repetition Structures in Python
- Question
- How many Repetition Structures are there in Python Programming Language?
- Answer
- The Two Repetition Structures of Python Programming Language are
- for loop
- while loop
- The Two Repetition Structures of Python Programming Language are
- Four Main Components of a Loop (Repetition Structure)
- In Python Programming, the Four Main Components of a Loop are
- Initialization
- Condition (Test)
- Body of Loop
- Increment / Decrement
- Loop Counter Variable
- Question
- What is a Loop Counter Variable?
- Answer
- A Loop Counter Variable is an Integer Variable which is used to control the Number of Iterations of a Loop
- for Loop
- Definition
- The for loop in Python is used to iterate the statements or a part of the program several times
- Purpose
- The for loop is a generic sequence iterator in Python: it can step through the items in any ordered sequence objects
- Importance
- It is frequently used to traverse the data structures like the array and linked list
- With the for loop, we can execute a set of statements, once for each item in a list, tuple, set etc.
- Applications
- The for statement works on strings, lists, tuples, other built-in iterables, and new objects
- Advantages
- In a for loop, the index of iteration is always a clearly defined variable
- For loops can easily be used to iterate through elements of multidimensional arrays using nested for loops
- Disadvantages
- It cannot traverse through the elements in reverse fashion
- You cannot skip any element as the concept of index is not there
- You cannot choose to traverse to odd or even indexed elements too
- Situatable to use
- The for loop takes a collection of items and executes a block of code once for each item in the collection
- Syntax - for Loop
'''
if Condition is True the block will be executed otherwise the control will move to the endif statement
'''
for(initialization; condition; increment/decrement)
BODY OF LOOP
- Flowchart - for Loop

- Steps – Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop
- In Sha Allah, we will Solve Repetitive Programming Tasks with Fixed Number of Iterations using for Loop, in the following Steps:
- Step 01: Completely and Correctly Understand the Real-world Repetitive Task
- Step 1.1: Real-world Repetitive Task
- Step 1.2: Real-world Repetitive Task Description
- Step 1.3: Your Job – As a Software Developer
- Step 02: Can we treat the Real-world Repetitive Task (Step 1.2) as a Repetitive Programming Task?
- Step 2.1: Check Whether the Real-world Repetitive Task can be treated as a Programming Repetitive Task?
- Step 2.2: Convert the Real-world Repetitive Task Description (Step 1.2) into Repetitive Programming Task Description
- Step 03: Completely and Correctly Understand the Repetitive Programming Task Description
- Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description
- Step 3.2: Requirements Gathering
- Step 3.3: Requirements Analysis
- Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
- If (Number of Iterations of the Repetitive Programming Task are Known before Writing Program / Software == True)
- THEN
- Repetitive Programming Task will be Solved with Fixed Number of Iterations using for Loop
- Go to Step 05
- ELSE
- Repetitive Programming Task will be Solved with Variable Number of Iterations using for Loop
- Follow the Steps used to Solve a Repetitive Programming Task with Variable Number of Iterations using for Loop (See Section:………………..for details)
- THEN
- If (Number of Iterations of the Repetitive Programming Task are Known before Writing Program / Software == True)
- Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3)
- Step 5.1: Write Down the Range / Pattern
- Step 5.2: Extract the following Information from Range / Pattern (Step 5.1)
- START_VALUE
- END_VALUE
- DIFFERENCE (i.e., Difference Between Two Consecutive Values in the Range / Pattern)
- Step 06: Plan and Design Solution to the Repetitive Programming Task
- Step 6.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3, 5.1 and 5.2)
- Step 6.2: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task
- Step 6.3: Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed
- Step 6.4: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3)
- Step 6.5: Select Suitable Programming Environment to Write Software (Code)
- Step 07: Implementation
- Step 7.1: Write Software (based on Step 6.2, 6.3, 6.4 and 6.5)
- Step 01: Completely and Correctly Understand the Real-world Repetitive Task
- Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
- In Sha Allah, in the next Slides, we will step-by-step Solve a Real-world Programming Repetitive Task using the Steps mentioned in the previous Section
- Example 1 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
Step 01: Completely and Correctly Understand the Real-world Repetitive Task |
Step 1.1: Real-world Repetitive Task |
|
Step 1.2: Real-world Repetitive Task Description |
|
Step 1.3: Your Job – As a Software Developer |
|
Step 02: Can we treat the Real-world Repetitive Task (Step 1.2) as a Repetitive Programming Task? |
Step 2.1: Check Whether the Real-world Repetitive Task can be treated as a Programming Repetitive Task? |
|
Step 2.2: Convert the Real-world Repetitive Task Description (Step 1.2) into Repetitive Programming Task Description |
|
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description |
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description |
|
Step 3.2: Requirements Gathering |
|
Step 3.3: Requirements Analysis |
|
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations? |
|
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3) |
Step 5.1: Write Down the Range / Pattern |
|
Step 5.2: Extract the following Information from Range / Pattern (Step 5.1) |
|
Step 06: Plan and Design Solution to the Repetitive Programming Task |
Step 6.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3, 5.1 and 5.2) |
|
Step 6.2: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task |
SET start_value and end_value SET counter to start_value for counter < end_value Print – counter INCREMENT by 1 EndFor |
Step 6.3: Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed |
|
Step 6.4: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3) |
![]() |
Step 6.5: Select Suitable Programming Environment to Write Software (Code) |
''
Program Details
__author_ = Ms. Samavi Salman
__copyright__ = Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__program__name__ = Display First Five Positive Integers
__program__version__ = 1.1
__programing__language__ = python 3.8.3
__operating__system__ = Ubuntu 18.04.1 LTS 64 bit
Operating System
__IDE __ = jupyter__notebook 5.5.0
___Start__Date__ = 18-05-2021
____End__Date___ = 01-06-2021
'''
Step 07: Implementation |
Step 7.1: Write Software (based on Step 6.2, 6.3, 6.4 and 6.5) |
'''
Purpose of Program
------------------
The main purpose of this Program is to Display the First Five Positive Integer Numbers on the Output Screen'''
try:
# Input
# Set up start_value and end_value
start_value = 1
end_value = 6
# Initialize Loop Counter Variable
counter = start_value # Initialization
# Processing + Output
# for Loop – To perform Repetitive Programming Task
for counter in range(start_value,end_value): # Condition
print(counter, end= ' ') # Body of Loop
except:
print("Exception Occurred")
'''
Extracting Four Components of a Loop from For Loop (written using range Function) Components of Loop Extracted from Code
======================================
INITIALIZTION: counter = start_value
CONDITION: counter < end_value
BODY OF LOOP: Print - number
INCREMENT: counter = counter + 1
'''
counter = start_value
for counter < end_value
print - number
counter = counter + 1
Execution of Code (Dry Run) | |||
Python Statement | Processing | Memory | Output Screen |
start_value = 1 | Initialize Variable start_value with START_VALUE i.e., 1 | start_value = 1 | – |
end_value = 1 | Initialize Variable end_value with END_VALUE i.e., 6 | start_value = 1 end_value = 6 | – |
counter = start_value for counter < end_value print – number counter = counter + 1 Note: In Sha Allah, In the Remaining Table we will show Iterations of the above for Loop (Code) | |||
Iteration 01 | counter = start_value | start_value = 1 end_value = 6 counter = 1 | – |
Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 6 counter = 1 | – | |
Body of Loop print(counter) | start_value = 1 end_value = 6 counter = 1 | 1 – | |
counter = counter + 1 | start_value = 1 end_value = 6 counter = 2 | 1 – | |
Iteration 02 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 6 counter = 2 | 1 – |
Body of Loop Print(counter) | start_value = 1 end_value = 6 counter = 2 | 1 2 – | |
counter = counter + 1 | start_value = 1 end_value = 6 counter = 3 | 1 2 – | |
Iteration 03 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 6 counter = 3 | 1 2 – |
Body of Loop Print(counter) | start_value = 1 end_value = 6 counter = 3 | 1 2 3 – | |
counter = counter + 1 | start_value = 1 end_value = 6 counter = 4 | 1 2 3 – | |
Iteration 04 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 6 counter = 4 | 1 2 3 – |
Body of Loop Print(counter) | start_value = 1 end_value = 6 counter = 4 | 1 2 3 4 – | |
counter = counter + 1 | start_value = 1 end_value = 6 counter = 5 | 1 2 3 4 – | |
Iteration 05 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 6 counter = 5 | 1 2 3 4 – |
Body of Loop Print(counter) | start_value = 1 end_value = 6 counter = 5 | 1 2 3 4 5 | |
counter = counter + 1 | start_value = 1 end_value = 6 counter = 6 | 1 2 3 4 5 | |
Iteration 06 | Condition
Condition is False, So Loop will Terminate | start_value = 1 end_value = 6 counter = 6 | 1 2 3 4 5 |
Program Termination: All the Python Statements in the Program are executed. Therefore, Program will Terminate 😊 |
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Tasks and answer the questions given below
- Note
- Your answer should be
- Well Justified
- Your answer should be
- Questions
- Write Python Programs for all the Tasks given below by following the Steps given in this Lecture i.e.,
- Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
- Write Python Programs for all the Tasks given below by following the Steps given in this Lecture i.e.,
Task 1 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
10 20 30 40 |
Task 2 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
100 200 300 |
Task 3 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
1 3 5 7 9 |
Your Turn Task 1
- Task
- Write at least 6 different Tasks which are very similar to the ones given in the TODO Tasks
- For each Task write a Python Program using the following Steps mentioned in this Lecture i.e.,
- Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
- Example 2 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
Step 01: Completely and Correctly Understand the Real-world Repetitive Task |
Step 1.1: Real-world Repetitive Task |
|
Step 1.2: Real-world Repetitive Task Description |
|
Step 1.3: Your Job – As a Software Developer |
|
Step 02: Can we treat the Real-world Repetitive Task (Step 1.2) as a Repetitive Programming Task? |
Step 2.1: Check Whether the Real-world Repetitive Task can be treated as a Programming Repetitive Task? |
|
Step 2.2: Convert the Real-world Repetitive Task Description (Step 1.2) into Repetitive Programming Task Description |
|
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description |
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description |
|
Step 3.2: Requirements Gathering |
|
Step 3.3: Requirements Analysis |
|
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations? |
|
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3) |
Step 5.1: Write Down the Range / Pattern |
|
Step 5.2: Extract the following Information from Range / Pattern (Step 5.1) |
|
Step 06: Plan and Design Solution to the Repetitive Programming Task |
Step 6.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3, 5.1 and 5.2) |
|
Step 6.2: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task |
SET start_value and end_value SET counter to start_value for counter > end_value Print Value of counter DECREMENT by -2 EndFor |
Step 6.3: Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed |
|
Step 6.4: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3) |
![]() |
Step 6.5: Select Suitable Programming Environment to Write Software (Code) |
'''
Program Details
__author_ = Ms. Samavi Salman
__copyright__ = Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__program__name__ = First Five Even Numbers in Reverse Order
__program__version__ = 1.1
__programing__language__ = python 3.8.3
__operating__system__ = Ubuntu 18.04.1 LTS 64 bit
Operating System
__IDE __ = jupyter__notebook 5.5.0
___Start__Date__ = 18-05-2021
____End__Date___ = 01-06-2021
'''
Step 07: Implementation |
Step 7.1: Write Software (based on Step 6.2, 6.3, 6.4 and 6.5) |
'''
Purpose of Program
-----------------
The main purpose of this Program is to Display the First Five Even Numbers in Reverse Order on the Output Screen'''
try:
# Set up start_value and end_value
start_value = 1
end_value = 11
number = 2
# Initialize Loop Counter Variable
counter = start_value # Initialization
# Processing + Output
# for Loop – To perform Repetitive Programming Task
for counter in range(start_value, end_value): # Condition
print(number, "x", counter,"=",number * counter) # Body of Loop
except:
print("Exception Occurred")
'''
Extracting Four Components of a Loop from For Loop (written using range Function) Components of Loop Extracted from Code
======================================
INITIALIZTION: counter = start_value
CONDITION: counter > end_value
BODY OF LOOP: Print - number
INCREMENT: counter = counter - 2
'''
counter = start_value
for counter > end_value
print - counter
counter = counter – 2
Execution of Code (Dry Run) | |||
Python Statement | Processing | Memory | Output Screen |
start_value = 8 | Initialize Variable start_value with START_VALUE i.e., 8 | start_value = 8 | – |
end_value = -2 | Initialize Variable end_value with END_VALUE i.e., -2 | start_value = 8 end_value = -2 | – |
counter = start_value for counter > end_value print – counter counter = counter – 2 Note: In Sha Allah, In the Remaining Table we will show Iterations of the above for Loop (Code) | |||
counter = start_value | start_value = 8 end_value = -2 counter = 8 | – | |
Iteration 01 | Condition
Condition is True, So Execute Body of Loop | start_value = 8 end_value = -2 counter = 8 | – |
Body of Loop Print(counter) | start_value = 8 end_value = -2 counter = 8 | 8 – | |
counter = counter – 2 | start_value = 8 end_value = -2 counter = 6 | 8 – | |
Iteration 02 | Condition
Condition is True, So Execute Body of Loop | start_value = 8 end_value = -2 counter = 6 | 8 – |
Body of Loop Print(counter) | start_value = 8 end_value = -2 counter = 6 | 8 6 – | |
counter = counter – 2 | start_value = 8 end_value = -2 counter = 4 | 8 6 – | |
Iteration 03 | Condition
Condition is True, So Execute Body of Loop | start_value = 8 end_value = -2 counter = 4 | 8 6 – |
Body of Loop Print(counter) | start_value = 8 end_value = -2 counter = 4 | 8 6 4 – | |
counter = counter – 2 | start_value = 8 end_value = -2 counter = 2 | 8 6 4 – | |
Iteration 04 | Condition
Condition is True, So Execute Body of Loop | start_value = 8 end_value = -2 counter = 2 | 8 6 4 – |
Body of Loop Print(counter) | start_value = 8 end_value = -2 counter = 2 | 8 6 4 2 – | |
counter = counter – 2 | start_value = 8 end_value = -2 counter = 0 | 8 6 4 2 – | |
Iteration 05 | Condition
Condition is True, So Execute Body of Loop | start_value = 8 end_value = -2 counter = 0 | 8 6 4 2 – |
Body of Loop Print(counter) | start_value = 8 end_value = -2 counter = 0 | 8 6 4 2 0 | |
counter = counter – 2 | start_value = 8 end_value = -2 counter = -2 | 8 6 4 2 0 | |
Iteration 06 | Condition
Condition is False, So Loop will Terminate | start_value = 8 end_value = -2 counter = -2 | 8 6 4 2 0 |
Program Termination: All the Python Statements in the Program are executed. Therefore, Program will Terminate 😊 |
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Tasks and answer the questions given below
- Note
- Your answer should be
- Well Justified
- Your answer should be
- Questions
- Write Python Programs for all the Tasks given below by following the Steps given in this Lecture i.e.,
- Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
- Write Python Programs for all the Tasks given below by following the Steps given in this Lecture i.e.,
Task 1 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
5 4 3 2 1 |
Task 2 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
40 30 20 10 |
Task 3 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
9 7 5 3 1 |
Task 4 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
300 200 100 |
Your Turn Task 1
- Task
- Write at least 6 different Tasks which are very similar to the ones given in the TODO Tasks
- For each Task write a Python Program using the following Steps mentioned in this Chapter i.e.,
- Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
- Example 3 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
Step 01: Completely and Correctly Understand the Real-world Repetitive Task |
Step 1.1: Real-world Repetitive Task |
|
Step 1.2: Real-world Repetitive Task Description |
|
Step 1.3: Your Job – As a Software Developer |
|
Step 02: Can we treat the Real-world Repetitive Task (Step 1.2) as a Repetitive Programming Task? |
Step 2.1: Check Whether the Real-world Repetitive Task can be treated as a Programming Repetitive Task? |
|
Step 2.2: Convert the Real-world Repetitive Task Description (Step 1.2) into Repetitive Programming Task Description |
|
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description |
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description |
|
Step 3.2: Requirements Gathering |
|
Step 3.3: Requirements Analysis |
|
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations? |
|
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3) |
Step 5.1: Write Down the Range / Pattern |
|
Step 5.2: Extract the following Information from Range / Pattern (Step 5.1) |
|
Step 06: Plan and Design Solution to the Repetitive Programming Task |
Step 6.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3, 5.1 and 5.2) |
|
Step 6.2: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task |
SET START_VALUE, END_VALUE and NUMBER SET counter to START_VALUE for counter < end_value Print NUMBER, “x”, counter,”=”, NUMBER * counter INCREMENT by +1 EndFor |
Step 6.3: Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed |
|
Step 6.4: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3) |
![]() |
Step 6.5: Select Suitable Programming Environment to Write Software (Code) |
'''
Program Details
__author_ = Ms. Samavi Salman
__copyright__ = Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__program__name__ = Display Table of 2
__program__version__ = 1.1
__programing__language__ = python 3.8.3
__operating__system__ = Ubuntu 18.04.1 LTS 64 bit
Operating System
__IDE __ = jupyter__notebook 5.5.0
___Start__Date__ = 18-05-2021
____End__Date___ = 01-06-2021
'''
Step 07: Implementation |
Step 7.1: Write Software (based on Step 6.2, 6.3, 6.4 and 6.5) |
'''
Purpose of Program
------------------
The main purpose of this Program is to Display Table of 2 on the Output Screen'''
try:
# Set up start_value and end_value
start_value = 1
end_value = 11
number = 2
# Initialize Loop Counter Variable
counter = start_value # Initialization
# Processing + Output
# for Loop – To perform Repetitive Programming Task
for counter in range(start_value, end_value): # Condition
print(number, "x", counter,"=",number * counter) # Body of Loop
except:
print("Exception Occurred")
'''
Extracting Four Components of a Loop from For Loop (written using range Function) Components of Loop Extracted from Code
======================================
INITIALIZTION: counter = start_value
CONDITION: counter < end_value
BODY OF LOOP: Print - counter
INCREMENT: counter = counter + 1
'''
number = 2
counter = start_value
for counter < end_value
print - (number, "x", counter,"=",number * counter)
counter = counter + 1
Execution of Code (Dry Run) | |||
Python Statement | Processing | Memory | Output Screen |
start_value = 1 | Initialize Variable start_value with START_VALUE i.e., 1 | start_value = 1 | – |
end_value = 11 | Initialize Variable end_value with END_VALUE i.e., 10 | start_value = 1 end_value = 11 | – |
number = 2 | Initialize Loop counter Variable i.e., counter = start_value | start_value = 1 end_value = 11 number = 2 | – |
number = 2 counter = start_value for counter < end_value print – (number, “x”, counter,”=”,number * counter) counter = counter + 1 Note: In Sha Allah, In the Remaining Table we will show Iterations of the above for Loop (Code) | |||
Iteration 01 | counter = start_value | start_value = 1 end_value = 11 number = 2 counter = 1 | – |
Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 11 number = 2 counter = 1 | – | |
Body of Loop Print(counter) | start_value = 1 end_value = 11 number = 2 counter = 1 | 2 x 1 = 2 – | |
counter = counter + 1 | start_value = 1 end_value = 11 number = 2 counter = 2 | 2 x 1 = 2 – | |
Iteration 02 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 11 number = 2 counter = 2 | 2 x 1 = 2 – |
Body of Loop Print(counter) | start_value = 1 end_value = 11 number = 2 counter = 2 | 2 x 1 = 2 2 x 2 = 4 – | |
counter = counter + 1 | start_value = 1 end_value = 11 number = 2 counter = 3 | 2 x 1 = 2 2 x 2 = 4 – | |
Iteration 03 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 11 number = 2 counter = 3 | 2 x 1 = 2 2 x 2 = 4 – |
Body of Loop Print(counter) | start_value = 1 end_value = 11 number = 2 counter = 3 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 – | |
counter = counter + 1 | start_value = 1 end_value = 11 number = 2 counter = 4 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 – | |
Iteration 04 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 11 number = 2 counter = 4 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 – |
Body of Loop Print(counter) | start_value = 1 end_value = 11 number = 2 counter = 4 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 – | |
counter = counter + 1 | start_value = 1 end_value = 11 number = 2 counter = 5 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 – | |
Iteration 05 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 11 number = 2 counter = 5 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 – |
Body of Loop Print(counter) | start_value = 1 end_value = 11 number = 2 counter = 5 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 – | |
counter = counter + 1 | start_value = 1 end_value = 11 number = 2 counter = 6 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 – | |
Iteration 06 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 11 number = 2 counter = 6 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 – |
Body of Loop Print(counter) | start_value = 1 end_value = 11 number = 2 counter = 6 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 – | |
counter = counter + 1 | start_value = 1 end_value = 11 number = 2 counter = 7 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 – | |
Iteration 07 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 11 number = 2 counter = 7 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 – |
Body of Loop Print(counter) | start_value = 1 end_value = 11 number = 2 counter = 7 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 – | |
counter = counter + 1 | start_value = 1 end_value = 11 number = 2 counter = 8 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 – | |
Iteration 08 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 11 number = 2 counter = 8 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 – |
Body of Loop Print(counter) | start_value = 1 end_value = 11 number = 2 counter = 8 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 – | |
counter = counter + 1 | start_value = 1 end_value = 11 number = 2 counter = 9 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 – | |
Iteration 09 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 11 number = 2 counter = 9 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 – |
Body of Loop Print(counter) | start_value = 1 end_value = 11 number = 2 counter = 9 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 – | |
counter = counter + 1 | start_value = 1 end_value = 11 number = 2 counter = 10 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 – | |
Iteration 10 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 11 number = 2 counter = 10 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 – |
Body of Loop Print(counter) | start_value = 1 end_value = 11 number = 2 counter = 10 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 | |
counter = counter + 1 | start_value = 1 end_value = 11 number = 2 counter = 11 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 | |
Iteration 11 | Condition
Condition is False, So Loop will Terminate | start_value = 1 end_value = 11 number = 2 counter = 11 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 |
Program Termination: All the Python Statements in the Program are executed. Therefore, Program will Terminate 😊 |
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Tasks and answer the questions given below
- Note
- Your answer should be
- Well Justified
- Your answer should be
- Questions
- Write Python Programs for all the Tasks given below by following the Steps given in this Chapter i.e.,
- Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using while Loop)
- Write Python Programs for all the Tasks given below by following the Steps given in this Chapter i.e.,
Task 1 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Enter a Positive Integer Number: 3 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30 |
Task 2 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Enter a Positive Integer Number: 3 Enter Start Value: 8 Enter End Value: 15
3 x 8 = 24 3 x 9 = 27 3 x 10 = 30 3 x 11 = 33 3 x 12 = 36 3 x 13 = 39 3 x 14 = 42 3 x 15 = 45 |
Your Turn Task 1
- Task
- Write at least 6 different Tasks which are very similar to the ones given in the TODO Tasks
- For each Task write a Python Program using the following Steps mentioned in this Chapter i.e.,
- Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using while Loop)
Nested for Loop
- Example 1 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using Nested for Loop)
Step 01: Completely and Correctly Understand the Real-world Repetitive Task |
Step 1.1: Real-world Repetitive Task |
* ** *** **** ***** |
Step 1.2: Real-world Repetitive Task Description |
|
Step 1.3: Your Job – As a Software Developer |
* ** *** **** ***** |
Step 02: Can we treat the Real-world Repetitive Task (Step 1.2) as a Repetitive Programming Task? |
Step 2.1: Check Whether the Real-world Repetitive Task can be treated as a Programming Repetitive Task? |
|
Step 2.2: Convert the Real-world Repetitive Task Description (Step 1.2) into Repetitive Programming Task Description |
* ** *** **** ***** |
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description |
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description |
* ** *** **** ***** |
Step 3.2: Requirements Gathering |
* ** *** **** ***** |
Step 3.3: Requirements Analysis |
|
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations? |
|
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3) |
Step 5.1: Write Down the Range / Pattern |
* ** *** **** ***** |
Step 5.2: Extract the following Information from Range / Pattern (Step 5.1) |
|
Step 06: Plan and Design Solution to the Repetitive Programming Task |
Step 6.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3, 5.1 and 5.2) |
* ** *** **** *****
* ** *** **** *****
|
Step 6.2: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task |
SET START_VALUE and END_VALUE SET ol_counter to START_VALUE for ol_counter <= end_value SET il_counter for il_counter <= ol_counter: PRINT – * INCREMENT – il_counter by +1
PRINT – newline INCREMENT – ol_counter by +1 EndFor |
Step 6.3: Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed |
· Step 2.1: INIT – start_value = 1 · Step 2.2: INIT – end_value = 5 · Step 2.3: INIT – counter = start_value
· Step 3.1: INIT – il_counter · Step 3.2: FOR il_counter <= ol_counter · Step 3.2.1: DISPLAY – * · Step 3.2.2: INCREMENT – il_counter by +1 · Step 3.3: DISPLAY – newline · Step 3.4: INCREMENT – ol_counter by +1
|
Step 6.4: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3) |
Step 6.5: Select Suitable Programming Environment to Write Software (Code) |
'''
Program Details
__author_ = Ms. Samavi Salman
__copyright__ = Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__program__name__ = Display the Pattern of * to form a Triangle
__program__version__ = 1.1
__programing__language__ = python 3.8.3
__operating__system__ = Ubuntu 18.04.1 LTS 64 bit
Operating System
__IDE __ = jupyter__notebook 5.5.0
___Start__Date__ = 18-05-2021
____End__Date___ = 01-06-2021
'''
Step 07: Implementation |
Step 7.1: Write Software (based on Step 6.2, 6.3, 6.4 and 6.5) |
'''
Purpose of Program
------------------
The main purpose of this Program is to Display the Pattern on the Output Screen'''
try:
# Set up start_value and end_value
start_value = 0
end_value = 5
# Processing + Output
# Initialize Outer Loop Variable
ol_counter = start_value
for ol_counter in range(start_value, end_value):
# Initialize Inner Loop Variable
for il_counter in range(start_value, ol_counter + 1):
print("*", end='')
print("")
except:
print("Exception Occurred")
'''
Extracting Four Components of a Loop from For Loop (written using range Function)
Components of Loop Extracted from Code
======================================
INITIALIZTION: counter = start_value
CONDITION: counter < end_value
BODY OF LOOP: Print - number
INCREMENT: counter = counter + 1
'''
ol = start_value
for ol_counter <= end_value:
il_counter = 1
for il_counter <= ol_counter:
print("*", end = "")
il_counter += 1
print()
ol_counter += 1
Execution of Code (Dry Run) | ||||
Python Statement | Processing | Memory | Output Screen | |
Initialization | ||||
start_value = 0 | Initialize Variable start_value with START_VALUE i.e., 1 | start_value = 1 | – | |
end_value = 5 | Initialize Variable end_value with END_VALUE i.e., 5 | start_value = 1 end_value = 5 | – | |
ol_counter = start_value for ol_counter <= end_value: il_counter = 1 for il_counter <= ol_counter: print(“*”, end = “”) il_counter += 1 print() ol_counter += 1 Note: In Sha Allah, In the Remaining Table we will show Iterations of the above for Loop (Code) | ||||
Iteration 01 – Outer Loop | Condition – Outer Loop | |||
ol_counter = start_value | start_value = 1 end_value = 5 ol_counter = 1 | – | ||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 1 | – | ||
Body of Outer Loop | ||||
Iteration 01 – Inner Loop | Initialization – Inner Loop Counter Variable (il_counter) | |||
il_counter = 1 | start_value = 1 end_value = 5 ol_counter = 1 il_counter = 1 | – | ||
Condition – Inner Loop | ||||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 1 il_counter = 1 | |||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 1 il_counter = 1 | * | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 1 += 1 | start_value = 1 end_value = 5 ol_counter = 1 il_counter = 2 | * | ||
Condition – Inner Loop | ||||
Condition
Condition is False, Loop will Terminate | start_value = 1 end_value = 5 ol_counter = 1 il_counter = 2 | * | ||
Print Statement of Outer Loop | ||||
print() | start_value = 1 end_value = 5 ol_counter = 1 il_counter = 2 | * – | ||
Increment – Outer Loop Counter (ol_counter) | ||||
ol_counter += 1 1 += 1 | start_value = 1 end_value = 5 ol_counter = 2 il_counter = 2 | * – | ||
Iteration 02 – Outer Loop | Condition – Outer Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 2 il_counter = 2 | * – | ||
Body of Outer Loop | ||||
Iteration 01 – Inner Loop | Initialization – Inner Loop Counter Variable (il_counter) | |||
il_counter = 1 | start_value = 1 end_value = 5 ol_counter = 2 il_counter = 1 | * – | ||
Condition – Inner Loop | ||||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 2 il_counter = 1 | * – | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 2 il_counter = 1 | * * | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 1 += 1 | start_value = 1 end_value = 5 ol_counter = 2 il_counter = 2 | * * | ||
Iteration 02 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 2 il_counter = 2 | * * | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 2 il_counter = 2 | * ** | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 2 += 1 | start_value = 1 end_value = 5 ol_counter = 2 il_counter = 3 | * ** | ||
Iteration 03 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is False, Loop will Terminate | start_value = 1 end_value = 5 ol_counter = 2 il_counter = 3 | |||
Print Statement of Outer Loop | ||||
print() | start_value = 1 end_value = 5 ol_counter = 2 il_counter = 3 | * ** – | ||
Increment – Outer Loop Counter (ol_counter) | ||||
ol_counter += 1 2 += 1 | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 3 | * ** – | ||
Iteration 03 – Outer Loop | Condition – Outer Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 3 | * ** – | ||
Body of Outer Loop | ||||
Initialization – Inner Loop Counter Variable (il_counter) | ||||
Iteration 01 – Inner Loop | il_counter = 1 | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 1 | * ** – | |
Condition – Inner Loop | ||||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 1 | * ** – | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 1 | * ** *- | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 1 += 1 | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 2 | * ** *- | ||
Iteration 02 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 2 | * ** *- | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 2 | * ** **- | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 2 += 1 | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 3 | * ** **- | ||
Iteration 03 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 3 | * ** **- | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 3 | * ** ***- | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 3 += 1 | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 4 | * ** ***- | ||
Iteration 04 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is False, Loop will Terminate | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 4 | * ** ***- | ||
Print Statement of Outer Loop | ||||
print() | start_value = 1 end_value = 5 ol_counter = 3 il_counter = 4 | * ** *** – | ||
Increment – Outer Loop Counter (ol_counter) | ||||
ol_counter += 1 3 += 1 | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 4 | * ** *** – | ||
Iteration 04 – Outer Loop | Condition – Outer Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 4 | * ** *** – | ||
Body of Outer Loop | ||||
Iteration 01 – Inner Loop | Initialization – Inner Loop Counter Variable (il_counter) | |||
il_counter = 1 | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 1 | * ** *** – | ||
Condition – Inner Loop | ||||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 1 | * ** *** – | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 1 | * ** *** *- | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 1 += 1 | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 2 | * ** *** *- | ||
Iteration 02 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 2 | * ** *** *- | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 2 | * ** *** **- | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 2 += 1 | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 3 | * ** *** **- | ||
Iteration 03 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 3 | * ** *** **- | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 3 | * ** *** ***- | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 3 += 1 | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 4 | * ** *** ***- | ||
Iteration 04 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 4 | * ** *** ***- | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 4 | * ** *** ****- | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 4 += 1 | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 5 | * ** *** ****- | ||
Iteration 05 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is False, Loop will Terminate | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 5 | * ** *** ****- | ||
Print Statement of Outer Loop | ||||
print() | start_value = 1 end_value = 5 ol_counter = 4 il_counter = 5 | * ** *** **** – | ||
Increment – Outer Loop Counter (ol_counter) | ||||
ol_counter += 1 4 += 1 | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 5 | * ** *** **** – | ||
Iteration 05 – Outer Loop | Condition – Outer Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 5 | * ** *** **** – | ||
Body of Outer Loop | ||||
Iteration 01 – Inner Loop | Initialization – Inner Loop Counter Variable (il_counter) | |||
il_counter = 1 | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 1 | * ** *** **** – | ||
Condition – Inner Loop | ||||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 1 | * ** *** **** – | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 1 | * ** *** **** *- | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 1 += 1 | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 2 | * ** *** **** *- | ||
Iteration 02 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 2 | * ** *** **** *- | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 2 | * ** *** **** **- | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 2 += 1 | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 3 | * ** *** **** **- | ||
Iteration 03 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 3 | * ** *** **** **- | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 3 | * ** *** **** ***- | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 3 += 1 | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 4 | * ** *** **** ***- | ||
Iteration 04 – Inner fwhLoop | Condition – Inner Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 4 | * ** *** **** ***- | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 4 | * ** *** **** ****- | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 4 += 1 | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 5 | * ** *** **** ****- | ||
Iteration 05 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is True, Execute Body of Loop | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 5 | * ** *** **** ****- | ||
Body of Inner Loop | ||||
print – * | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 5 | * ** *** **** ***** | ||
Increment – Inner Loop Counter (il_counter) | ||||
il_counter += 1 5 += 1 | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 6 | * ** *** **** *****- | ||
Iteration 06 – Inner Loop | Condition – Inner Loop | |||
Condition
Condition is False, Loop will Terminate | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 6 | * ** *** **** *****- | ||
Print Statement of Outer Loop | ||||
print() | start_value = 1 end_value = 5 ol_counter = 5 il_counter = 6 | * ** *** **** ***** – | ||
Increment – Outer Loop Counter (ol_counter) | ||||
ol_counter += 1 5 += 1 | start_value = 1 end_value = 5 ol_counter = 6 il_counter = 6 | * ** *** **** ***** – | ||
Iteration 06 – Outer Loop | Condition – Outer Loop | |||
Condition
Condition is False, Loop will Terminate | start_value = 1 end_value = 5 ol_counter = 6 il_counter = 6 | * ** *** **** ***** – | ||
Program Termination: All the Python Statements in the Program are executed. Therefore, Program will Terminate 😊 |