Chapter 21 - while 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
- while Loop
- Definition
- In Python Programming, a while loop is used to execute a Set of Python Statements (i.e., Perform a Repetitive Programming Task) as long as a Condition is True
- Purpose
- The main purpose of while Loop is to
- Perform Repetitive Programming Tasks
- The main purpose of while Loop is to
- Importance
- while Loop can be used to perform any Repetitive Programming Tasks
- Applications
- while Loop is commonly used in a range of Programming Tasks including
- Searching an Element in a List (for e.g., Searching Student Name in a List of Student Names)
- Sorting Elements of a List (for e.g., Sorting a List of Student Names in Ascending Order)
- Reading Records from a File
- Writing Records to a File
- Deciding whether a Number is Prime or Not?
- Printing Table of a Number
- And Many More 😊
- while Loop is commonly used in a range of Programming Tasks including
- Suitable to Use
- while Loop is Suitable to Use for all Repetitive Programming Tasks 😊
- Role of Loop Counter Variable in while Loop
- In while Loop, Loop Counter Variable is used in
- Initialization (Mandatory)
- Condition (Mandatory)
- Increment / Decrement (Mandatory)
- Body of Loop (Optional)
- Syntax - while Loop
'''
if Condition is True the body of loop will be executed until the condition becomes False
'''
INITIALIZATION
while CONDITION:
BODY OF LOOP
INCREMNET / DECREMENT
- Flowchart – while Loop

- Note
- In Sha Allah in the next Slides, I will show how to use while Loop for
- Repetitive Tasks with Fixed Number of Iterations
- Repetitive Tasks with Variable Number of Iterations
while Loop – Repetitive Programming Tasks with Fixed Number of Iterations
- Main Characteristics of a Repetitive Programming Tasks with Fixed Number of Iterations
- When we have a Repetitive Programming Tasks with Fixed Number of Iterations, it has some Main Characteristics, which are as follows:
- Characteristics 01
- Number of Fixed Iterations are defined as a
- Range i.e., [START_VALUE – END_VALUE]
- START_VALUE
- First Value in the Range
- Note
- Loop Counter Variable is Initialized (i.e., INITIALIZATION) with the START_VALUE
- END_VALUE
- Loop Terminate when the Value of Loop Counter Variable exceeds END_VALUE
- Note
- Both Loop Counter Variable and END_VALUE are used in the CONDITION
- Difference Between Two Consecutive Values in the Range
- The Difference Between Two Consecutive Values in the Range is used determine the
- Value of Increment / Decrement
- The Difference Between Two Consecutive Values in the Range is used determine the
- Note
- Loop Counter Variable is Increments / Decrements based on the Difference Between Two Consecutive Values in the Range
- Number of Fixed Iterations are defined as a
- Characteristics 02
- Execution of the Loop will Start with the START_VALUE and each Iteration will take us closer to the END_VALUE
- Characteristics 03
- Loop will Terminate (i.e., we cannot have an Infinite Loop) when the Value of Loop Counter Variable exceeds END_VALUE
- Characteristics 01
- Steps – Solving a Repetitive Programming Tasks with Fixed Number of Iterations using while Loop
- In Sha Allah, we will Solve Repetitive Programming Tasks with Fixed Number of Iterations using while 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 while Loop
- Go to Step 05
- ELSE
- Repetitive Programming Task will be Solved with Variable Number of Iterations using while Loop
- Follow the Steps used to Solve a Repetitive Programming Task with Variable Number of Iterations using while Loop (See Section: while Loop – Repetitive Programming Tasks with Variable Number of Iterations 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 while 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 while 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 |
· 1 2 3 4 5 |
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 while counter is Less Than Or Equal to END_VALUE Print Value of counter Increase counter by +1 EndWhile |
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 = 5
# Initialize Loop Counter Variable
counter = start_value # Initialization
# Processing + Output
# while Loop – To perform Repetitive Programming Task
while counter <= end_value: # Condition
print(counter, end = ' ') # Body of Loop
counter = counter + 1 # Increment
except:
print("Exception Occurred")
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., 5 | start_value = 1 end_value = 5 | – |
counter = start_value | Initialize Loop counter Variable i.e., counter = start_value | start_value = 1 end_value = 5 counter = 1 | – |
while counter <= end_value: # Condition print(counter, end = ‘ ‘) # Body of Loop counter = counter + 1 # Increment Note: In Sha Allah, In the Remaining Table we will show Iterations of the above while Loop (Code) | |||
Iteration 01 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 5 counter = 1 | – |
Body of Loop Print(counter) | start_value = 1 end_value = 5 counter = 1 | 1 – | |
Increment
| start_value = 1 end_value = 5 counter = 2 | 1 – | |
Iteration 02 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 5 counter = 2 | 1 – |
Body of Loop Print(counter) | start_value = 1 end_value = 5 counter = 2 | 1 2 – | |
Increment
| start_value = 1 end_value = 5 counter = 3 | 1 2 – | |
Iteration 03 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 5 counter = 3 | 1 2 – |
Body of Loop Print(counter) | start_value = 1 end_value = 5 counter = 3 | 1 2 3 – | |
Increment
| start_value = 1 end_value = 5 counter = 4 | 1 2 3 – | |
Iteration 04 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 5 counter = 4 | 1 2 3 – |
Body of Loop Print(counter) | start_value = 1 end_value = 5 counter = 4 | 1 2 3 4 – | |
Increment
| start_value = 1 end_value = 5 counter = 5 | 1 2 3 4 – | |
Iteration 05 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 5 counter = 5 | 1 2 3 4 – |
Body of Loop Print(counter) | start_value = 1 end_value = 5 counter = 5 | 1 2 3 4 5 | |
Increment
| start_value = 1 end_value = 5 counter = 6 | 1 2 3 4 5 | |
Iteration 06 | Condition
Condition is False, So Loop will Terminate | start_value = 1 end_value = 5 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 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 |
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 Chapter i.e.,
- Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using while Loop)
- Example 2 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using while 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 while counter is Greater Than Or Equal to END_VALUE Print Value of counter Decrease counter by -2 EndWhile |
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 = 8
end_value = 0
# Initialize Loop Counter Variable
counter = start_value # Initialization
# Processing + Output
# while Loop – To perform Repetitive Programming Task
while counter >= end_value: # Condition
print(counter, end = ' ') # Body of Loop
counter = counter - 2 # Decrement
except:
print("Exception Occurred")
Execution of Code (Dry Run) | |||
Python Statement | Processing | Memory | Output Screen |
start_value = 1 | Initialize Variable start_value with START_VALUE i.e., 8 | start_value = 8 | – |
end_value = 1 | Initialize Variable end_value with END_VALUE i.e., 0 | start_value = 8 end_value = 0 | – |
counter = start_value | Initialize Loop counter Variable i.e., counter = start_value | start_value = 8 end_value = 0 counter = 8 | – |
while counter >= end_value: # Condition print(counter, end = ‘ ‘) # Body of Loop counter = counter – 2 # Decrement Note: In Sha Allah, In the Remaining Table we will show Iterations of the above while Loop (Code) | |||
Iteration 01 | Condition
Condition is True, So Execute Body of Loop | start_value = 8 end_value = 0 counter = 8 | – |
Body of Loop Print(counter) | start_value = 8 end_value = 0 counter = 8 | 8 – | |
Decrement
| start_value = 8 end_value = 0 counter = 6 | 8 – | |
Iteration 02 | Condition
Condition is True, So Execute Body of Loop | start_value = 8 end_value = 0 counter = 6 | 8 – |
Body of Loop Print(counter) | start_value = 8 end_value = 0 counter = 6 | 8 6 – | |
Decrement
| start_value = 8 end_value = 0 counter = 4 | 8 6 – | |
Iteration 03 | Condition
Condition is True, So Execute Body of Loop | start_value = 8 end_value = 0 counter = 4 | 8 6 – |
Body of Loop Print(counter) | start_value = 8 end_value = 0 counter = 4 | 8 6 4 – | |
Decrement
| start_value = 8 end_value = 0 counter = 2 | 8 6 4 – | |
Iteration 04 | Condition
Condition is True, So Execute Body of Loop | start_value = 8 end_value = 0 counter = 2 | 8 6 4 – |
Body of Loop Print(counter) | start_value = 8 end_value = 0 counter = 2 | 8 6 4 2 – | |
Decrement
| start_value = 8 end_value = 0 counter = 0 | 8 6 4 2 – | |
Iteration 05 | Condition
Condition is True, So Execute Body of Loop | start_value = 8 end_value = 0 counter = 0 | 8 6 4 2 – |
Body of Loop Print(counter) | start_value = 8 end_value = 0 counter = 0 | 8 6 4 2 0 | |
Decrement
| start_value = 8 end_value = 0 counter = -2 | 8 6 4 2 0 | |
Iteration 06 | Condition
Condition is False, So Loop will Terminate | start_value = 8 end_value = 0 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 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 |
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 while Loop)
- Example 3 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using while 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 while counter is Less Than Or Equal to END_VALUE Print message (Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah.) Increase counter by +1 EndWhile |
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 Text Message
__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 a Text Message 3 times on the Output Screen'''
try:
# Set up start_value and end_value
start_value = 1
end_value = 3
# Initialize Loop Counter Variable
counter = start_value # Initialization
# Processing + Output
# while Loop – To perform Repetitive Programming Task
while counter <= end_value: # Condition
print("Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah.") # Body of Loop
counter = counter + 1 # Increment
except:
print("Exception Occurred")
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., 3 | start_value = 1 end_value = 3 | – |
counter = start_value | Initialize Loop counter Variable i.e., counter = start_value | start_value = 1 end_value = 3 counter = 1 | – |
while counter <= end_value: # Condition print(“Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah.”) # Body of Loop counter = counter + 1 # Increment Note: In Sha Allah, In the Remaining Table we will show Iterations of the above while Loop (Code) | |||
Iteration 01 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 3 counter = 1 | – |
Body of Loop Print(counter) | start_value = 1 end_value = 3 counter = 1 | Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. | |
Increment
| start_value = 1 end_value = 3 counter = 2 | Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. | |
Iteration 02 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 3 counter = 2 | Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. |
Body of Loop Print(counter) | start_value = 1 end_value = 3 counter = 2 | Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. | |
Increment
| start_value = 1 end_value = 3 counter = 3 | Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. | |
Iteration 03 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 3 counter = 3 | Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. |
Body of Loop Print(counter) | start_value = 1 end_value = 3 counter = 3 | Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. | |
Increment
| start_value = 1 end_value = 3 counter = 4 | Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. | |
Iteration 04 | Condition
Condition is False, So Loop will Terminate | start_value = 1 end_value = 3 counter = 4 | Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. |
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 |
Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah. |
Task 2 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Hazrat Muhammad S.A.W.W. is the Last Messenger of Allah (and there is no Prophet after Him (PBUH)). He is a Perfect Role Model for us in all walks of Life. Our Goal of Life is to Achieve Excellence in Love (Ishq) of Hazrat Muhammad S.A.W.W. |
Task 3 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
· Go to Bed Immediately after Namaz.e.Isha · Take Three Healthy Meals on Daily Basis (for e.g., Dring 1 Liter Milk, 5 – 10 Spoons Honey, 10 – 20 Dates etc.) · At least 30 – 60 Minutes Game / Running / Brisk Walk on Daily Basis (with whole Family) |
Task 4 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
To Achieve Excellence in Mental Health, Perform the following Real-world Tasks on Daily Basis: 1. Be Happy, Keep Happy 😊 2. Allah ka Shukar ada karen (ادا کریں) 3. Read the following Dua after Every Namaz
|
Task 5 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Achieve Excellence in Social Health Health, Perform the following Real-world Tasks on Daily Basis: آپنے معاملات درست کریں اللہ پاک کی مخلوق سے پیار کریں حقوق العباد کا ہر صورت خیال کریں.حقوق العباد کے اوپر ہی سارے معاملات اور دنیا اور آخرت کی کامیابی کا راز ھے اس کو سمجھ لیا تو سب کچھ مل گیا گویا رب کی مخلوق راضی تو رب راضی Do At least One Good Deed on Daily Basis to Please Allah |
Task 6 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
To Achieve Excellence in Spiritual Health, Perform the following Real-world Tasks on Daily Basis: 1. Recite At least 1 Para of Quran.e.Pak on Daily Basis 2. At least Recite Durood Sharif for 30 Minutes on Daily Basis (Recite At least 1 Crore Durood Sharif (درود شریف) in Your Life) 3. Offer 5 Namaz in Masjid (مسجد) with Takbeer.e.Owla (تکبیر اولیٰ) on Daily Basis 4. Fast in the Holy Month of Ramazan 5. Perform Hajj and / or Give Zakat if it is Obligatory on You 6. Fulfill Both Huqooq Ullah (حقوق اللہ) and Huqooq Ul Ibad (حقوق العباد) Allah Pak ki 100% Frama bardari karen (فرمانبرداری کریں) aur Allah Pak ki Na-farmani (نافرمانی) sy 100% Bachen (بچیں) |
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)
- Example 4 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using while 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 while counter is Less Than Or Equal to END_VALUE Print number,”x”, counter,”=”,number * counter Increase counter by +1 EndWhile |
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 the 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 the Table of 2 on the Output Screen'''
try:
# Set up start_value and end_value
start_value = 1
end_value = 10
number = 2
# Initialize Loop Counter Variable
counter = start_value # Initialization
# Processing + Output
# while Loop – To perform Repetitive Programming Task
while counter <= end_value: # Condition
print(number, "x", counter,"=",number * counter) # Body of Loop
counter = counter + 1 # Increment
except:
print("Exception Occurred")
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., 10 | start_value = 1 end_value = 10 | – |
counter = start_value | Initialize Loop counter Variable i.e., counter = start_value | start_value = 1 end_value = 10 counter = 1 | – |
while counter <= end_value: # Condition print(number, “x”, counter,”=”,number * counter) # Body of Loop counter = counter + 1 # Increment
Note: In Sha Allah, In the Remaining Table we will show Iterations of the above while Loop (Code) | |||
Iteration 01 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 10 counter = 1 | – |
Body of Loop Print(counter) | start_value = 1 end_value = 10 counter = 1 | 2 x 1 = 2 – | |
Increment
| start_value = 1 end_value = 10 counter = 2 | 2 x 1 = 2 – | |
Iteration 02 | Condition
Condition is True, So Execute Body of Loop | start_value = 1 end_value = 10 counter = 2 | 2 x 1 = 2 – |
Body of Loop Print(counter) | start_value = 1 end_value = 10 counter = 2 | 2 x 1 = 2 2 x 2 = 4 – | |
Increment
| start_value = 1 end_value = 10 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 = 10 counter = 3 | 2 x 1 = 2 2 x 2 = 4 – |
Body of Loop Print(counter) | start_value = 1 end_value = 10 counter = 3 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 – | |
Increment
| start_value = 1 end_value = 10 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 = 10 counter = 4 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 – |
Body of Loop Print(counter) | start_value = 1 end_value = 10 counter = 4 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 – | |
Increment
| start_value = 1 end_value = 10 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 = 10 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 = 10 counter = 5 | 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 – | |
Increment
| start_value = 1 end_value = 10 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 = 10 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 = 10 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 – | |
Increment
| start_value = 1 end_value = 10 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 = 10 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 = 10 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 – | |
Increment
| start_value = 1 end_value = 10 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 = 10 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 = 10 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 – | |
Increment
| start_value = 1 end_value = 10 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 = 10 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 = 10 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 – | |
Increment
| start_value = 1 end_value = 10 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 = 10 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 = 10 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 | |
Increment
| start_value = 1 end_value = 10 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 = 10 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)
- Difference (between Two Consecutive Numbers) = +2
- Loop Counter Variable will be initialized with Start Value
- Condition will use End Value
- Difference (between Two Consecutive Numbers) is used decide about Increment / Decrement of Loop Counter Variable
- Body of Loop contains the Actual Code to be REPEATEDLY executed until the Condition is True
while Loop – Repetitive Programming Tasks with Variable Number of Iterations
- Steps – Solving a Repetitive Programming Tasks with Variable Number of Iterations using while Loop
- In Sha Allah, we will Solve Repetitive Programming Tasks with Variable Number of Iterations using while 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 while Loop
- See Section: while Loop – Repetitive Programming Tasks with Fixed Number of Iterations
- ELSE
- Repetitive Programming Task will be Solved with Variable Number of Iterations using while Loop
- Go to Step 05
- Step 05: Setup the Loop Control Variable (called choice) to Control the Number of Iterations of while Loop
- Step 5.1: Initialize the Loop Control Variable (called choice) with Positive Response i.e.,
- choice = “yes”
- Step 5.2: Set Condition of while Loop Equal to the Initial Value of choice Variable, so that while Loop executes at least once.
- while choice == “yes”:
- Step 5.3: After Executing the Code in Boody of Loop, ask the Choice of the User i.e., whether (s)he wants to have another Iteration of the while Loop or Not?
- print(“Do you want to continue (yes / no)? “)
- choice = input()
- If (User Choice == “yes”)
- THEN
- Loop will Execute One More Time (Iteration)
- ELSE
- Loop will Terminate
- Note
- Minimum Number of Iterations
- Step 5.1: Initialize the Loop Control Variable (called choice) with Positive Response i.e.,
- There will be at least 1 Iteration of the while Loop.
- Maximum Number of Iterations
- Total Number of Iterations are Not Known at Code Time because Number of Iterations are Dependent on the Choice of the User, which (s)he will provide at Run Time.
- 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, 5.1 and 5.3)
- 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)
- Difference Between Fixed Number of Iterations and Variable Number of Iterations
Serial No | Fixed Number of Iterations | Variable Number of Iterations |
1 | Number of Iterations are Known at Code Time | Number of Iterations are Not Known at Code Time |
2 | Total Number of Iterations will be decided based on Range / Pattern with following information: START_VALUE, END_VALUE and INCREMENT / DECREMNET | Total Number of Iterations will depend on Choice of User, which (s)he will provide at Run Time |
3 | Condition is based on Loop Conter Variable and END_VALUE | Condition is decided based on Loop Control Variable and User Choice |
- Example 1 - Repetitive Programming Tasks with Variable Number of Iterations
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: Setup the Loop Control Variable to Control the Number of Iterations |
Step 5.1: Initialize the Loop Control Variable (called choice) with Positive Response |
|
Step 5.2: Set Condition of while Loop Equal to the Initial Value of choice Variable |
|
Step 5.3: Ask choice of User |
|
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 CHOICE to “yes” while choice == “yes”: Input – number01 Input – number02 Calculate – sum Display – sum Print – “Do You Want to Practice More (yes / no)?” Input – choice EndWhile |
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 Sum of 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 Sum of Integers until User input choice as "yes" on the Output Screen'''
try:
# Initialize Loop Control Variable with Positive Response
choice = "yes"
while choice == "yes":
number01 = int(input("Enter First Integer Number: "))
number02 = int(input("Enter Second Integer Number: "))
sum = number01 + number02
print(number01, "+", number02, "=", sum)
choice = input("Do You Want to Practice More (yes / no)? ")
if not choice.isalpha():
raise Exception('')
except ValueError:
print("Error! Please Enter Integer Numbers")
except Exception as error:
print("Error! Please Enter Strings")
Enter First Integer Number: 7
Enter Second Integer Number: 10
7 + 10 = 17
Do You Want to Practice More (yes / no)? yes
Enter First Integer Number: 5
Enter Second Integer Number: 2
5 + 2 = 7
Do You Want to Practice More (yes / no)? yes
Enter First Integer Number: 5
Enter Second Integer Number: 9
5 + 9 = 14
Do You Want to Practice More (yes / no)? yes
Enter First Integer Number: 10
Enter Second Integer Number: 20
10 + 20 = 30
Do You Want to Practice More (yes / no)? no
Execution of Code (Dry Run) | |||
Python Statement | Processing | Memory | Output Screen |
choice = “yes” | Initialize Variable choice with value “yes” | choice = “yes” | – |
while choice == “yes”: number01 = int(input(“Enter First Integer Number: “)) number02 = int(input(“Enter Second Integer Number: “)) sum = number01 + number02 print(number01, “+”, number02, “=”, number01 + number02) print(“Do You Want to Practice More (yes / no)?”) choice = input()
Note: In Sha Allah, In the Remaining Table we will show Iterations of the above while Loop (Code) | |||
Iteration 01 | Condition
Condition is True, So Execute Body of Loop | choice = yes | – |
number01 = int(input(“Enter First Integer Number: “)) | choice = yes number01 = 7 | Enter First Integer Number: 7 | |
number02 = int(input(“Enter Second Integer Number: “)) | choice = yes number01 = 7 number02 = 10 | Enter First Integer Number: 7 Enter Second Integer Number: 10 | |
sum = number01 + number02 | choice = yes number01 = 7 number02 = 10 sum = 17 | Enter First Integer Number: 7 Enter Second Integer Number: 10 | |
print(number01, “+”, number02, “=”, number01 + number02) | choice = yes number01 = 7 number02 = 10 sum = 17 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 | |
choice = input(“Do You Want to Practice More (yes / no)?”) | choice = yes number01 = 7 number02 = 10 sum = 17 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes | |
Iteration 02 | Condition
Condition is True, So Execute Body of Loop | choice = yes number01 = 7 number02 = 10 sum = 17 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes |
number01 = int(input(“Enter First Integer Number: “)) | choice = yes number01 = 5 number02 = 10 sum = 17 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 | |
number02 = int(input(“Enter Second Integer Number: “)) | choice = yes number01 = 5 number02 = 2 sum = 17 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 | |
sum = number01 + number02 | choice = yes number01 = 5 number02 = 2 sum = 7 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 | |
print(number01, “+”, number02, “=”, number01 + number02) | choice = yes number01 = 5 number02 = 2 sum = 7 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 | |
choice = input(“Do You Want to Practice More (yes / no)?”) | choice = yes number01 = 5 number02 = 2 sum = 5 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes | |
Iteration 03 | Condition
Condition is True, So Execute Body of Loop | choice = yes number01 = 5 number02 = 2 sum = 7 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes |
number01 = int(input(“Enter First Integer Number: “)) | choice = yes number01 = 5 number02 = 2 sum = 7 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 | |
number02 = int(input(“Enter Second Integer Number: “)) | choice = yes number01 = 5 number02 = 9 sum = 7 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 9 | |
sum = number01 + number02 | choice = yes number01 = 5 number02 = 9 sum = 14 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 9 | |
print(number01, “+”, number02, “=”, number01 + number02) | choice = yes number01 = 5 number02 = 9 sum = 14 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 9 5 + 9 = 14 | |
choice = input(“Do You Want to Practice More (yes / no)?”) | choice = yes number01 = 5 number02 = 9 sum = 14 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 9 5 + 9 = 14 Do You Want to Practice More (yes / no)? yes | |
Iteration 04 | Condition
Condition is True | choice = yes | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 9 5 + 9 = 14 Do You Want to Practice More (yes / no)? yes |
number01 = int(input(“Enter First Integer Number: “)) | choice = yes number01 = 10 number02 = 2 sum = 14 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 9 5 + 9 = 14 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 10 | |
number02 = int(input(“Enter Second Integer Number: “)) | choice = yes number01 = 10 number02 = 20 sum = 14 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 9 5 + 9 = 14 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 10 Enter Second Integer Number: 20 | |
sum = number01 + number02 | choice = yes number01 = 10 number02 = 20 sum = 30 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 9 5 + 9 = 14 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 10 Enter Second Integer Number: 20 | |
print(number01, “+”, number02, “=”, number01 + number02) | choice = yes number01 = 10 number02 = 20 sum = 30 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 9 5 + 9 = 14 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 10 Enter Second Integer Number: 20 10 + 20 = 30 | |
choice = input(“Do You Want to Practice More (yes / no)?”) | choice = no number01 = 10 number02 = 20 sum = 30 | Enter First Integer Number: 7 Enter Second Integer Number: 10 7 + 10 = 17 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 9 5 + 9 = 14 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 10 Enter Second Integer Number: 20 10 + 20 = 30 Do You Want to Practice More (yes / no)? no | |
Iteration 05 | Condition
Condition is False, So Loop will Terminate | choice = no number01 = 10 number02 = 20 sum = 30 | Enter First Integer Number: 5 Enter Second Integer Number: 2 5 + 2 = 7 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 9 5 + 9 = 14 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 10 Enter Second Integer Number: 20 10 + 20 = 30 Do You Want to Practice More (yes / no)? no |
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 Variable 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 First Integer Number: 2 Enter Second Integer Number: 2 2 * 2 = 4 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 5 Enter Second Integer Number: 2 5 * 2 = 10 Do You Want to Practice More (yes / no)? yes Enter First Integer Number: 9 Enter Second Integer Number: 9 9 * 9 = 81 Do You Want to Practice More (yes / no)? no |
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 Variable Number of Iterations using while Loop)
- Steps (Solving a Repetitive Programming Tasks with Variable Number of Iterations using while Loop)
Nested While Loop
- Example 1 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using Nested While 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 while ol_counter <= end_value SET il_counter while il_counter <= ol_counter: PRINT – * INCREMENT – il_counter by +1
PRINT – newline INCREMENT – ol_counter by +1 EndWhile |
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 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 = 1
end_value = 5
# Initialize Outer Loop Variable
ol_counter = start_value
# Processing + Output
while ol_counter <= end_value:
il_counter = 1
while il_counter <= ol_counter:
print("*", end = "")
il_counter += 1
print()
ol_counter += 1
except:
print("Exception Occurred")
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 | Initialize Variable ol_counter with start_value | start_value = 1 end_value = 5 ol_counter = 1 | – | |
while ol_counter <= end_value: il_counter = 1 while 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 while Loop (Code) | ||||
Iteration 01 – Outer Loop | Condition – Outer Loop | |||
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 | 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 | 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 1 += 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 | 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 1 += 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 1 += 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 | 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 1 += 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 1 += 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 1 += 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 | 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 1 += 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 1 += 1 | start_value = 1 end_value = 5 ol_counter = 5 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 = 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 1 += 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 1 += 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 | 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 😊 |