Skip to content

Ilm o Irfan

Technologies

  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
Explore Courses

Introduction to Python

  • September 26, 2022
  • Home
  • Free Book

Table of Contents

Chapter 21 - while Repetition Structure

  • Authors
  • Ms. Samavi Salman
  • Dr. Rao Muhammad Adeel Nawab
  • Supporting Material
  • Download Supporting Material (Code): here

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
  • 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
  • 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 😊
  • 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 
      • Note 
        • Loop Counter Variable is Increments / Decrements based on the Difference Between Two Consecutive Values in the Range
    • 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
  • 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) 
    • 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)
  • 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
  • Printing the First Five Integer Numbers
Step 1.2: Real-world Repetitive Task Description
  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Five Positive Integer Numbers on a Screen and teach her students about Square of Numbers. 
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which prints the First Five Positive Integer Numbers on the Output Screen 😊
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?
  • Yes, The Real-world Repetitive Task can be treated as a Programming Repetitive Task
  • Reason
    • The Real-world Repetitive Task call the Function with little variations. Therefore, it 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
  • Write a Python Program which prints the First Five Positive Integer Numbers on the Output Screen.
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description
  • The main objective of this Software is to display First Five Positive Integer Numbers on the Output Screen. 
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 01
      • Display the First Five Positive Integer Numbers on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 01 – Display the First Five Integer Numbers on the Output Screen
    • No need to Print or Save Output in any Format
      • Just Display it on the Output Screen
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
  • 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) 
  • Analysis – Repetitive Programming Task
    • Printing the First Five Positive Integer Number i.e., 1 2 3 4 5
    • Total Number of Iterations = 5
    • Total Number of Iteration of the Repetitive Programming Task are Known before Writing Program / Software
  • Therefore, 
    • Repetitive Programming Task will be Solved with Fixed Number of Iterations using while Loop 😊
  •  
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3)
Step 5.1: Write Down the Range / Pattern
  • First Five Integer Number, i.e.,

·        1 2 3 4 5

Step 5.2: Extract the following Information from Range / Pattern (Step 5.1)
  • START_VALUE = 1
  • END_VAUE = 5
  • DIFFERENCE (i.e., Difference Between Two Consecutive Values in the Range / Pattern) = +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)
  • Input
    • Data
      • First Five Positive Integer Numbers, i.e.
        • 1 2 3 4 5
    • Instruction(s)
      • Display the First Five Positive Integer Numbers on the Output Screen
  • Output
    • 1 2 3 4 5
  • Processing
    • Use a Repetition Structure (In Sha Allah, we will use the while Loop)
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 01: START
  • Step 02: Initialize Variables
    • Step 2.1: INIT – start_value = 1
    • Step 2.2: INIT – end_value = 5
    • Step 2.3: INIT – counter = start_value
  • Step 03: WHILE counter <= end_value:
    • Step 3.1: DISPLAY – counter
    • Step 3.2: INCREMENT – counter by +1
  • ENDWHILE
  • Step 05: STOP
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

  • counter <= end_value
  • 1 <= 5
  • True

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 

  • counter = counter + 1

start_value = 1

end_value = 5

counter = 2

1 –

Iteration 02

Condition

  • counter <= end_value
  • 2 <= 5
  • True

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 

  • counter = counter + 1

start_value = 1

end_value = 5

counter = 3

1 2 –

Iteration 03

Condition

  • counter <= end_value
  • 3 <= 5
  • True

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 

  • counter = counter + 1

start_value = 1

end_value = 5

counter = 4

1 2 3 –

 

Iteration 04

Condition

  • counter <= end_value
  • 4 <= 5
  • True

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 

  • counter = counter + 1

start_value = 1

end_value = 5

counter = 5

1 2 3 4 –

Iteration 05

Condition

  • counter <= end_value
  • 5 <= 5
  • True

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 

  • counter = counter + 1

start_value = 1

end_value = 5

counter = 6

1 2 3 4 5

Iteration 06

Condition

  • 6 <= end_value
  • 6 <= 5
  • False 

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 Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • 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)

 

                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Four Multiples of 10 on a Screen and teach her students about Multiples of a Number. 

Required Output

10 20 30 40 

 

                        Task  2

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Three Multiples of 100 on a Screen and teach her students about Multiples of a Number. 

Required Output

100 200 300

 

                        Task  3

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Five Odd Numbers on a Screen and teach her students about Odd Numbers.

Required Output

1 3 5 7 9

 

Your Turn Tasks

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
  • Printing the First Five Even Numbers in Reverse Order
Step 1.2: Real-world Repetitive Task Description
  • Ms. Samavi is teaching Math to Grade 1 students. To teach Even Numbers to her students, she wants to develop a Program which displays First Five Even Numbers in Reverse Order on a Screen. 
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which prints the First Five Even Numbers in Reverse Order on the Output Screen 😊
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?
  • Yes, The Real-world Repetitive Task can be treated as a Programming Repetitive Task
    • Reason
      • The Real-world Repetitive Task repeats 5 times with little variations. Therefore, it 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
  • Write a Python Program which prints the First Five Even Numbers in Reverse Order on the Output Screen.
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description
  • The main objective of this Software is to display First Five Even Numbers in Reverse Order on the Output Screen. 
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 01
      • Only Consider First Five Even Numbers in Reverse Order
    • Requirement 02
      • Display the First Five Even Numbers in Reverse Order on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 01 – Only Consider First Five Even Numbers in Reverse Order
    • Only Take First Five Even Numbers in Reverse Order
      • First Five Even Numbers in reverse order will be starting from 8 and the Last will be 0 (as per Requirements of the Client i.e., Ms. Samavi)
  • Analysis of Requirement 02 – Display the First Five Even Numbers in Reverse Order on the Output Screen
    • No need to Print or Save Output in any Format
      • Just Display it on the Output Screen
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
  • 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) 
  • Analysis – Repetitive Programming Task
    • Printing the First Five Even Numbers in Reverse Order e., 8 6 4 2 0
    • Total Number of Iterations = 5
    • Total Number of Iteration of the Repetitive Programming Task are Known before Writing Program / Software
  • Therefore,
    • Repetitive Programming Task will be Solved with Fixed Number of Iterations using while Loop 😊
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3)
Step 5.1: Write Down the Range / Pattern
  • First Five Even Numbers in Reverse Order, i.e.
    • 8 6 4 2 0
Step 5.2: Extract the following Information from Range / Pattern (Step 5.1)
  • START_VALUE = 8
  • END_VAUE = 0
  • DIFFERENCE (i.e., Difference Between Two Consecutive Values in the Range / Pattern) = -2
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)
  • Input
    • Data
      • First Five Even Numbers in Reverse Order, i.e.
        • 8 6 4 2 0
    • Instruction(s)
      • Display the First Five Even Numbers in Reverse Order on the Output Screen
  • Output
    • 8 6 4 2 0
  • Processing
    • Use a Repetition Structure (In Sha Allah, we will use the while Loop)
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 01: START
  • Step 02: Initialize Variables
  • Step 2.1: INIT – start_value = 8
    • Step 2.2: INIT – end_value = 0
    • Step 2.3: INIT – counter = start_value
  • Step 03: WHILE counter >= end_value:
    • Step 3.1: DISPLAY – counter
    • Step 3.2: DECREMENT – counter by -2
  • ENDWHILE
  • Step 05: STOP
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

  • counter >= end_value
  • 8 >= 0
  • True

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 

  • counter = counter – 2

start_value = 8

end_value = 0

counter = 6

8 –







Iteration 02

Condition

  • counter >= end_value
  • 6 >= 0
  • True

 

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 

  • counter = counter – 2

start_value = 8

end_value = 0

counter = 4

8 6 –







Iteration 03

Condition

  • counter >= end_value
  • 4 >= 0
  • True

 

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 

  • counter = counter – 2

start_value = 8

end_value = 0

counter = 2

8 6 4 –






Iteration 04

Condition

  • counter >= end_value
  • 2 >= 0
  • True

 

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 

  • counter = counter – 2

    

start_value = 8

end_value = 0

counter = 0

8 6 4 2 –







Iteration 05

Condition

  • counter >= end_value
  • 0 >= 0
  • True

 

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 

  • counter = counter – 2

start_value = 8

end_value = 0

counter = -2

8 6 4 2 0

 

Iteration 06

Condition

  • counter >= end_value
  • -2 >= 0
  • False

 

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 Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • 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)

 

                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Five Positive Integer Numbers in Reverse Order on a Screen and teach her students about Positive Integer Numbers.

Required Output

5 4 3 2 1

 

                        Task  2

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Four Multiples of 10 in Reverse Order on a Screen and teach her students about Multiples of a Number.

Required Output

40 30 20 10

 

                        Task  3

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Five Odd Numbers in Reverse Order on a Screen and teach her students about Odd Numbers.

Required Output

9 7 5 3 1

 

                        Task  4

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Three Multiples of 100 in Reverse Order on a Screen and teach her students about Multiples of a Number.

Required Output

300 200 100

 

Your Turn Tasks

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
  • Printing a Text Message 
Step 1.2: Real-world Repetitive Task Description
  • Ms. Samavi is teaching Human Engineering Course to Grade 1 students. The first Topic is: Greatness of Allah. To teach her students about the Greatness of Allah Almighty, she wants to display the following Text Message 3 times on the Output Screen.
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which prints the Text Message 3 times on the Output Screen 😊
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?
  • Yes, The Real-world Repetitive Task can be treated as a Programming Repetitive Task
    • Reason
      • The Real-world Repetitive Task repeats 3 times with little variations. Therefore, it 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
  • Write a Python Program which prints the Text Message on the Output Screen.
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description
  • The main objective of this Software is to display Text Message on the Output Screen. 
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 01
      • Only Consider Text Message
    • Requirement 02
      • Display the Text Message 3 times on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 01 – Display the Text Message 3 times on the Output Screen
    • No need to Print or Save Output in any Format
      • Just Display it on the Output Screen
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
  • 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) 
  • Analysis – Repetitive Programming Task
    • Printing the Text Message e., (Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah.)
    • Total Number of Iterations = 3
    • Total Number of Iteration of the Repetitive Programming Task are Known before Writing Program / Software
  • Therefore,
    • Repetitive Programming Task will be Solved with Fixed Number of Iterations using while Loop 😊
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3)
Step 5.1: Write Down the Range / Pattern
  • Text Message, i.e.
    • Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah.
Step 5.2: Extract the following Information from Range / Pattern (Step 5.1)
  • START_VALUE = 1
  • END_VAUE = 3
  • DIFFERENCE (i.e., Difference Between Two Consecutive Values in the Range / Pattern) = +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)
  • Input
    • Data
      • Text Message, i.e.
        • Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah.
    • Instruction(s)
      • Display the Text Message on the Output Screen
  • Output
    • 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.
  • Processing
    • Use a Repetition Structure (In Sha Allah, we will use the while Loop)
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 01: START
  • Step 02: Initialize Variables
    • Step 2.1: INIT – start_value = 1
    • Step 2.2: INIT – end_value = 3
    • Step 2.3: INIT – counter = start_value
  • Step 03: WHILE counter <= end_value:
    • Step 3.1: DISPLAY – message (Allah is One and He has Control over Everything. Our Goal of Life is to Achieve Excellence in the Friendship of Allah.)
    • Step 3.2: INCREMENT – counter by +1
  • ENDWHILE
  • Step 05: STOP
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

  • counter <= end_value
  • 1 <= 3
  • True

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 

  • counter = counter + 1

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

  • counter <= end_value
  • 2 <= 3
  • True

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 

  • counter = counter + 1

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

  • counter <= end_value
  • 3 <= 3
  • True

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 

  • counter = counter + 1

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

  • counter <= end_value
  • 4 <= 3
  • False

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 Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • 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)

 

                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Human Engineering Course to Grade 1 students. The first Topic is: Greatness of Allah. She wants to display the following text 3 times on the Output Screen.

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

  • Ms. Samavi is teaching Human Engineering Course to Grade 1 students. The first Topic is: Greatness of Hazrat Muhammad S.A.W.W. She wants to display the following text 7 Times on the Output Screen.

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

  • Ms. Samavi is teaching Human Engineering Course to Grade 1 students. The first Topic is: Excellence in Physical Health. She wants to display the following text 10 Times on the Output Screen.

Required Output

  • To Achieve Excellence in Physical Health, Perform the following Real-world Tasks on Daily Basis:

·        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

  • Ms. Samavi is teaching Human Engineering Course to Grade 1 students. The first Topic is: Excellence in Mental Health. She wants to display the following text 4 Times on the Output Screen.

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

  • Ms. Samavi is teaching Human Engineering Course to Grade 1 students. The first Topic is: Excellence in Social Health. She wants to display the following text 20 Times on the Output Screen.

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

  • Ms. Samavi is teaching Human Engineering Course to Grade 1 students. The first Topic is: Excellence in Spiritual Health. She wants to display the following text 15 Times on the Output Screen.

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 Tasks

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
  • Printing Table of 2
Step 1.2: Real-world Repetitive Task Description
  • Ms. Samavi is teaching Math to Grade 1 students. To teach Tables to her students, she wants to develop a Program which displays the Table of 2 on the Output Screen. 
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which prints the Table of 2 on the Output Screen 😊
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?
  • Yes, The Real-world Repetitive Task can be treated as a Programming Repetitive Task
    • Reason
      • The Real-world Repetitive Task repeats 10 times with little variations. Therefore, it 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
  • Write a Python Program which prints the Table of 2 on the Output Screen.
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description
  • The main objective of this Software is to display Table of 2 on the Output Screen. 
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 01
      • Only Consider Table of 2
    • Requirement 02
      • Display the Table of 2 on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 01 – Only Consider Table of 2
    • Only Take a Number (i.e., 2)
      • Table of 2 will be starting from 1 and the Last will be 10 (as per Requirements of the Client i.e., Ms. Samavi)
  •  
    •  
  • Analysis of Requirement 02 – Display the Table of 2 on the Output Screen
    • No need to Print or Save Output in any Format
      • Just Display it on the Output Screen
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
  • 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) 
  • Analysis – Repetitive Programming Task
    • Printing the Table of 2
      • Total Number of Iterations = 10
      • Total Number of Iteration of the Repetitive Programming Task are Known before Writing Program / Software
  • Therefore, 
    • Repetitive Programming Task will be Solved with Fixed Number of Iterations using while Loop 😊
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3)
Step 5.1: Write Down the Range / Pattern
  • Table of 2, i.e.
    • 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
Step 5.2: Extract the following Information from Range / Pattern (Step 5.1)
  • START_VALUE = 1
  • END_VAUE = 10
  • DIFFERENCE (i.e., Difference Between Two Consecutive Values in the Range / Pattern) = +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)
  • Input
    • Data
      • Table of 2
    • Instruction(s)
      • Display the Table of 2 on the Output Screen
  • Output
    • 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
  • Processing
    • Use a Repetition Structure (In Sha Allah, we will use the while Loop)
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 01: START
  • Step 02: Initialize Variables
    • Step 2.1: INIT – start_value = 1
    • Step 2.2: INIT – end_value = 10
    • Step 2.3: INIT – number = 2
    • Step 2.4: INIT – counter = start_value
  • Step 03: WHILE counter <= end_value:
    • Step 3.1: DISPLAY – number,”x”, counter,”=”,number * counter
    • Step 3.2: INCREMENT – counter by +1
    • ENDWHILE
  • Step 05: STOP
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 = 1Initialize Variable start_value with START_VALUE i.e., 1start_value = 1–
end_value = 1Initialize Variable end_value with END_VALUE i.e., 10

start_value = 1

end_value = 10

–
counter = start_valueInitialize 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

  • counter <= end_value
  • 1 <= 10
  • True

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 

  • counter = counter + 1

start_value = 1

end_value = 10

counter = 2

2 x 1 = 2

–

 

Iteration 02

Condition

  • counter <= end_value
  • 2 <= 10
  • True

 

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 

  • counter = counter + 1

start_value = 1

end_value = 10

counter = 3

2 x 1 = 2

2 x 2 = 4

–

 

Iteration 03

Condition

  • counter <= end_value
  • 3 <= 10
  • True

 

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 

  • counter = counter + 1

start_value = 1

end_value = 10

counter = 4

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

–

 

Iteration 04

Condition

  • counter <= end_value
  • 4 <= 10
  • True

 

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 

  • counter = counter + 1

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

  • counter <= end_value
  • 5 <= 10
  • True

 

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 

  • counter = counter + 1

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

  • counter <= end_value
  • 6 <= 10
  • True

 

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 

  • counter = counter + 1

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

  • counter <= end_value
  • 7 <= 10
  • True

 

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 

  • counter = counter + 1

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

  • counter <= end_value
  • 8 <= 10
  • True

 

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 

  • counter = counter + 1

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

  • counter <= end_value
  • 9 <= 10
  • True

 

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 

  • counter = counter + 1

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

  • counter <= end_value
  • 10 <= 10
  • True

 

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 

  • counter = counter + 1

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

  • counter <= end_value
  • 11 <= 10
  • False

 

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 Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • 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)

 

 

                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. To teach Tables efficiently and quickly to her students, she wants to develop a Program, which takes a Positive Integer Number as Input and prints its Table on the Output Screen.

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

  • Ms. Samavi is teaching Math to Grade 1 students. To teach Multiples of a Number efficiently and quickly to her students, she wants to develop a Program, which takes a Positive Integer Number, Start Value and End Value as Input and prints its Multiples of the Positive Integer Number from Start value to the End value on the Output Screen.

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 Tasks

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 
        • 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
  • Printing Sum of Two Integer Number until user input “yes”
Step 1.2: Real-world Repetitive Task Description
  • Ms. Samavi is teaching Math to Grade 1 students. To teach Addition of Two Integer Numbers efficiently and quickly to her students, she wants to develop a Program, which takes Two Integer Numbers as Input from User (Student) and displays their Sum on the Output Screen. After that the Program asks the User (Student), whether (s)he wants to Practice More or Not? The Program should repeatedly perform Addition of Two Integer Numbers if the User’s (Student’s) Choice is yes, i.e., (s)he wants to Practice the Concept of Addition of Two Integer Numbers. Once the User (Student) enters no, the Program should Terminate. 
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write  a Python Program for Ms. Samavi, which prints the Sum of Two Integer Number until user input “yes” on the Output Screen 😊
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?
  • Yes, The Real-world Repetitive Task can be treated as a Programming Repetitive Task
    • Reason
      • The Real-world Repetitive Task repeats until user input “yes”. Therefore, it 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
  • Write a Python Program which prints the Sum of Two Integer Number until user input “yes” on the Output Screen.
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description
  • The main objective of this Software is to display Sum of Two Integer Number until user input “yes” on the Output Screen. 
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 01
      • Only Consider Two Integers
  • Requirement 02
    • Calculate the Sum of two Integers
  • Requirement 03
    • Display the Sum of two Integers until User input ‘yes’ on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 01 – Only Consider Two Integers
    • Take two Integer Numbers
  • Analysis of Requirement 02 – Calculate the Sum of two Integers
    • No need to Print or Save Output in any Format
    • Just Display it on the Output Screen
  • Analysis of Requirement 03 – Display the Sum of two Integers until User input ‘yes’ on the Output Screen
    • Ask the User to input Two Integer Numbers and Calculate Sum of Numbers until user input ‘yes’ on the Output Screen
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
  • 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) 
  • Analysis – Repetitive Programming Task
    • Take Two Integers as Input from User
    • Calculate the Sum of Integers
    • Total Number of Iterations = Until User input ‘yes’
    • Total Number of Iteration of the Repetitive Programming Task are Unknown before Writing Program / Software
  • Therefore, 
    • Repetitive Programming Task will be Solved with Variable Number of Iterations using while Loop 😊
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
  • Response of Loop Control Variable, i.e.,
    • choice = “yes”
Step 5.2: Set Condition of while Loop Equal to the Initial Value of choice Variable
  • Set Condition of while Loop Equal to the Initial Value of choice Variable, so that while Loop executes at least once i.e,
    • while choice == “yes”:
Step 5.3: Ask choice of User
  • 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 
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)
  • Input
    • Data
      • Integer Number01
      • Integer Number 02
      • Choice
    • Instruction(s)
      • Take two Integer Numbers from the User
      • Calulate the Sum of Integers
      • Display the Input Numbers and their Sum on the Output Screen
      • Repeat the Same process until User input Choice as ‘yes’
  • Output
    • 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)? no
  • Processing
    • Use a Repetition Structure (In Sha Allah, we will use the while Loop)
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 01: START
  • Step 02: Initialize Variables
    • Step 2.1: INIT – choice
  • Step 03: WHILE choice == “yes”:
    • Step 3.1: INPUT – number01 
    • Step 3.2: INPUT – number02
    • Step 3.3: INIT – sum
    • Step 3.4: CALCULATE – sum
    • Step 3.5: DISPLAY – number01, “+”, number02, “=”, sum
    • Step 3.6: DISPLAY – “Do You Want to Practice More (yes / no)?”
    • Step 3.7: INPUT – choice
  • ENDWHILE
  • Step 04: STOP
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

  • choice == “yes”
  • yes == yes
  • True

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

  • choice == “yes”
  • yes == yes
  • True

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

  • choice == “yes”
  • yes == yes
  • True

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

  • choice == “yes”
  • yes == yes
  • False

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

  • choice == “yes”
  • no == yes
  • False

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 Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • 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)
  •  

                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. To teach Multiplication of Two Integer Numbers efficiently and quickly to her students, she wants to develop a Program, which takes Two Integer Numbers as Input from User (Student) and displays their Product on the Output Screen. After that the Program asks the User (Student), whether (s)he wants to Practice More or Not? The Program should repeatedly perform Multiplication of Two Integer Numbers if the User’s (Student’s) Choice is yes, i.e., (s)he wants to Practice the Concept of Multiplication of Two Integer Numbers. Once the User (Student) enters no, the Program should Terminate.

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 Tasks

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)
        •  

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
  • Printing the following Pattern 

*

**

***

****

*****

Step 1.2: Real-world Repetitive Task Description
  • Ms. Samavi is teaching Drawing to Grade 1 students. She wants to display a Triangle on a Screen and teach her students about Sides of Triangle. 
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which prints the following Pattern on the Output Screen 😊

*

**

***

****

*****

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?
  • Yes, The Real-world Repetitive Task can be treated as a Programming Repetitive Task
    • Reason
      • The Real-world Repetitive Task repeats 5 times with little variations. Therefore, it 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
  • Write a Python Program for Ms. Samavi, which prints the following Pattern on the Output Screen 😊

*

**

***

****

*****

Step 03: Completely and Correctly Understand the Repetitive Programming Task Description
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description 
  • The main objective of this Software is to display the following Pattern on the Output Screen 😊

*

**

***

****

*****

Step 3.2: Requirements Gathering     
  • The main Requirements of this Software are as follows:
    • Requirement 01
      • Display the following Pattern on the Output Screen 😊

*

**

***

****

*****

Step 3.3: Requirements Analysis
  • Analysis of Requirement 01 – Only Consider Pattern
    • Use Nested Loop
      • Number of Lines = 5         ==> Output Loop
      • Number of * per Line       ==> Inner Loop
  • Analysis of Requirement 01 – Display the Pattern on the Output Screen
    • No need to Print or Save Output in any Format
      • Just Display it on the Output Screen
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
  • 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) 
  • Analysis – Repetitive Programming Task
    • Printing the Pattern
      • Total Number of Iterations = 5
      • Total Number of Iteration of the Repetitive Programming Task are Known before Writing Program / Software
  • Therefore, 
    • Repetitive Programming Task will be Solved with Fixed Number of Iterations using while Loop 😊
  •  
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3)
Step 5.1: Write Down the Range / Pattern
  • Display the following Pattern on the Output Screen 😊

*

**

***

****

*****

Step 5.2: Extract the following Information from Range / Pattern (Step 5.1)
  • START_VALUE = 1
  • END_VAUE = 5
  • OL_COUNTER = START_VALUE
  • IL_COUNTER = END_VAUE
  • DIFFERENCE (i.e., Difference Between Two Consecutive Values in the Range / Pattern) = +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)
  • Input
    • Data
      • First Five Positive Integer Numbers, i.e.

*

**

***

****

*****

    • Instruction(s)
      • Display the First Five Positive Integer Numbers on the Output Screen
  • Output

*

**

***

****

*****

  • Processing
    • Use a Repetition Structure (In Sha Allah, we will use the While Loop)
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 01: START
  • Step 02: Initialize Variables
    • Step 2.1: INIT – start_value = 1
    • Step 2.2: INIT – end_value = 5
    • Step 2.3: INIT – counter = start_value
  • Step 03: WHILE ol_counter <= end_value
    • Step 3.1: INIT – il_counter
    • Step 3.2: WHILE 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
  • EndWhile
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

  • while ol_counter <= end_value
  • 1 <= 5
  • True

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

  • while il_counter <= ol_counter
  • 1 <= 1
  • True

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

  • while il_counter <= ol_counter
  • 2 <= 1
  • False

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

  • while ol_counter <= end_value
  • 2 <= 5
  • True

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

  • while il_counter <= ol_counter
  • 1 <= 2
  • True

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

  • while il_counter <= ol_counter
  • 2 <= 2
  • True

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

  • while il_counter <= ol_counter
  • 3 <= 2
  • False

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

  • while ol_counter <= end_value
  • 3 <= 5
  • True

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

  • while il_counter <= ol_counter
  • 1 <= 3
  • True

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

  • while il_counter <= ol_counter
  • 2 <= 3
  • True

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

  • while il_counter <= ol_counter
  • 3 <= 3
  • True

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

  • while il_counter <= ol_counter
  • 4 <= 3
  • True

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

  • while ol_counter <= end_value
  • 4 <= 5
  • True

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

  • while il_counter <= ol_counter
  • 1 <= 4
  • True

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

  • while il_counter <= ol_counter
  • 2 <= 4
  • True

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

  • while il_counter <= ol_counter
  • 3 <= 4
  • True

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

  • while il_counter <= ol_counter
  • 4 <= 4
  • True

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

  • while il_counter <= ol_counter
  • 5 <= 4
  • False

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

  • while ol_counter <= end_value
  • 5 <= 5
  • True

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

  • while il_counter <= ol_counter
  • 1 <= 5
  • True

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

  • while il_counter <= ol_counter
  • 2 <= 5
  • True

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

  • while il_counter <= ol_counter
  • 3 <= 5
  • True

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

  • while il_counter <= ol_counter
  • 4 <= 5
  • True

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

  • while il_counter <= ol_counter
  • 5 <= 5
  • True

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

  • while il_counter <= ol_counter
  • 6 <= 5
  • False

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

  • while ol_counter <= end_value
  • 6 <= 5
  • False

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 😊

 

Chapter 20 - Repetition Structures in Python
  • Previous
Chapter 22 - for Repetition Structure
  • Next
Share this article!
Facebook
Twitter
LinkedIn
About Us

Ilm O Irfan Technologies believes that honest dedicated hard work with the ethical approach of commitment true to every word, and trust-building is the road to success in any field of business or life.

Quick Links
  • About Us
  • Contact
Useful Links
  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • Support
  • FAQ
Subscribe Our Newsletter
Facebook Twitter Linkedin Instagram Youtube

© 2022 Ilmo Irfan. All Rights Reserved.