0% found this document useful (0 votes)
4 views28 pages

week5

The document discusses the importance of loop statements in programming, specifically in Python, and explains the different types of loops: Counter Loops (For Loops) and Conditional Loops (While Loops). It provides examples of how to implement these loops to perform tasks such as summing numbers and taking user input until a condition is met. The conclusion emphasizes the distinction between the two types of loops and their applications in programming.

Uploaded by

Basic Tricks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views28 pages

week5

The document discusses the importance of loop statements in programming, specifically in Python, and explains the different types of loops: Counter Loops (For Loops) and Conditional Loops (While Loops). It provides examples of how to implement these loops to perform tasks such as summing numbers and taking user input until a condition is met. The conclusion emphasizes the distinction between the two types of loops and their applications in programming.

Uploaded by

Basic Tricks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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 num1 = 1
1
Statement
num2 = 3
2 sum = float(num1) + float(num2)
Statement
3 print('The sum is = '+ str(sum))
Statement
4 --------------------------------
The sum is = 4
Outp
ut
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 Conditio Stateme statemen
n nt t

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
Conditio
for Loop Statement
n True

Fals
e
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.
Keywo Loop
rd Condition

while
(condition)
{ Body of
Loop
//body
}
While Loop in Python
Then we will use Conditional Loops. I.e., we
will execute the loop until a certain condition
is met.
Keywo Loop
rd Condition

while
condition : Body of
Loop
//body
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
Conditio
while loop
n true

Fals
e
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):
print(x)
if x == 5 :
break

//Remaining code

1234 Outpu
t
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):
print(x)
if x == 5:
continue

//Remaining code

1 2 3 4 6 7 8… Output
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
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