Week 5 (Repetition Structures)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Repetition Structures

Learning Objectives
• Define why we need Loop Statements.

• Learn about the Loop structure

• Define and implement of Counting and Conditional Loops

• Define, understand, and Learn FOR and While Loop

Computer Science Department, UET Lahore.


Revision: Print sum of two numbers
Let’s say we want to print the sum of two numbers.

# This program adds two numbers


Statement 1 num1 = 1
Statement 2
num2 = 3
Statement 3
sum = float(num1) + float(num2)
print('The sum is = '+ str(sum))
Statement 4
--------------------------------
The sum is = 4
Output
Print sum
Let’s say we want to print sum of odd number from 1
to 10.
# This program adds five numbers
num1 = 1
num2 = 3
num3 = 5
num4 = 7
num5 = 9
sum = float(num1) + float(num2)+ float(num3) + float(num4) +
float(num5)+
print('The sum is = '+ str(sum))
--------------------------------
The sum is = 25
Print sum
Let’s say we want to print sum of odd number from 1
to 100 or may be Sum of odd number from 1 to 1000.
# This program adds five numbers
num1 = 1
num2 = 3
num3 = 5
num4 = 7
num5 = 9
sum = float(num1) + float(num2)+ float(num3) + float(num4) +
float(num5)+
print('The sum is = '+ str(sum))
--------------------------------
The sum is = 25
Code Repetition: Problem
Is there a way, we don’t have to repeat the same
instructions again and again?
Code Repetition: Solution
In High level languages, Loops are used to repeat the
same command without writing it multiple times.
Control structures: Iteration or looping
• The iteration statement allows
instructions to be executed until a Condition

certain condition is to be fulfilled. The


iteration statements are also called as
loops or Looping statements.
Code Block

OR
• Loops can execute a block of
code number of times until a
certain condition is met. Iteration
Loops
In Loops, the program knows beforehand about
how many times a specific instruction or set of
instructions will be executed.
Initial Loop Update
Keyword Statement Condition statement

for (int x = 1; x < 100; x = x + 2 )


{
//body Body of Loop

}
Loops in Python
In Loops, the program knows beforehand about how
many times a specific instruction or set of
instructions will be executed. This is called Counter
Loop Loop Initial Update
Keyword Condition Statement statement

for x in range (1,100,2) :

//body Body of Loop


Counter Loop: Working Example
Let’s say we want to print sum of odd number from 1
to 100 or may be Sum of odd number from 1 to 100.

# This program adds 100 odd numbers


sum = 0
for x in range (1,100,2):
sum = sum + x

print('The sum is = '+ str(sum))


--------------------------------
The sum is = 2500
Start

Initial
Statement

Loop Body of Update


Condition for Loop Statement
True

False

End
Other Types of Loops
Now, What if we don’t know beforehand how many
times a set of instructions will be executed?
Conditional Loops
In Such cases, we will use Conditional Loops. I.e., we
will execute the loop until a certain condition is met.

Keyword Loop Condition

while (condition):
{
//body Body of Loop

}
While Loop in Python
Then we will use Conditional Loops. I.e., we will
execute the loop until a certain condition is met.

Keyword Loop Condition

while condition :
//body Body of Loop
Conditional Loop: Working Example
Suppose, the requirement is to keep taking numbers
as input from the user until the user enters -1.
Conditional Loop: Working Example
Suppose, the requirement is to keep taking numbers as
input from the user until the user enters -1.

# This program adds 100 odd numbers


num = 0
while (num != -1):
print(‘Enter -1 to exit.’)
num = int(input(‘Enter a number: ’))

Enter -1 to exit.
Enter a number: -1
Conditional Loop: Working Example

Loop Body of
Condition while loop
true

False
Code Repetition: Problem
Is there any way, we can stop the loop before it has
looped through all the items?
Code Repetition: Solution (Break)
for x in range(1,100,1):
if x == 5 :
break
print(x)

//Remaining code
Output
1 2 3 4
Code Repetition: Problem
Is there any way, we can stop the current iteration
of the loop, and continue with the next?
Code Repetition: Solution (Continue)
for x in
for x in range(1,100,1):
if x == 5:
continue
print(x)

//Remaining code
Output
1 2 3 4 6 7 8…
Working Example

Write a Program that prints your Name


for a given number of times you want.
Let's See Practical
Solution
Learning Outcome

In this lecture, we learnt how to


write a Python Program that repeats
a Set of Instructions for a specific
number of times to solve the given
problem using Loops.
Conclusion
● Loops are of 2 types
1. Sequence-Controlled / Counter (For Loop)
Loops.
2. Sentinel controlled / Conditional (While) Loops
● In Counter Loop, the program knows beforehand
about how many times a specific instruction or
set of instructions will be executed.
● In Conditional Loop, the program doesn’t know
the number of instruction to be executed.
Self Assessment

Write a python Program that asks the user to enter 5


numbers, one at a time, and add them together. This
is called a Running Total. Once the user is done,
display the total sum on the Console. If its below 50
ask the user to enter again.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy