Download as pdf or txt
Download as pdf or txt
You are on page 1of 80

SENG 113: Computer Programming I

Doç.Dr. Hilal ARSLAN

Week 7: Loops

Computer Programming
Repetitive Structures
Structures

Computer Programming
Python Loops
• A loop is a programming structure that repeats a sequence of
instructions until a specific condition is met. Loops allow
you to repeat a process over and over without having to
write the same (potentially long) instructions each time
you want your program to perform a task.

• Two major types of loops are FOR LOOPS and WHILE


LOOPS. A For loop will run a preset number of times
whereas a While loop will run a variable number of times.

Computer Programming
for loop
for loop

Computer Programming
for loop
Python Loops

Computer Programming
Python Loops
Python Loops
Python Loops
Example
Solution
Example
Solution
Example
Solution
for loop
Range
range() function returns a sequence of numbers,
• Starting from 0 by default,
• Increments by 1 (by default)
• Stops before a specified number

Computer Programming
for loop
Range

Computer Programming
for loop
Example

Computer Programming
for loop
Solution

Computer Programming
Range

n = 4 n = 4
for k in range(n): for k in range(1,n):
print k print k

0 1
Output: 1 2
Output:
2 3
3
“Counting from Here to
(Almost) There”
Here = 20
There = 24
for k in range(Here,There):
print k

20
21
Output: 22
23
“Counting Down”
Here = 20
There = 24
for k in range(There,Here,-1):
print k

24
23
Output: 22
21
for loop

Computer Programming
for loop

Computer Programming
for loop

Computer Programming
for loop

Week 5: Introduction
Computer Programming
to Python 27 / 28
for loop

Computer Programming
for loop
Example

Computer Programming
for loop
Solution

Computer Programming
for loop
Example

Computer Programming
for loop
Solution

Computer Programming
for loop
Example

Computer Programming
Iterating Through a String
Output:
s = ‘abcd’
a
for c in s: b
print c c
d

In this example, the “for-loop” variable is c.


One at a time, it takes on the value of each
character in s.
We learned about this in the previous lecture.
Iterating Through a Range

Output:
n = 4
0
for k in range(n):
1
print k 2
3

How does this work? What does range(n) mean?


Note the Similarities

n = 4 s = ‘abcd’
for k in range(n): for c in s:
print k print c

0 a
Output: 1 Output: b
2 c
3 d
Example

n = 4
s = 0
for k in range(n):
x = 2**k
s = s + x Output:
print s

We are repeating the purple box 4 times


for-loop Mechanics with range
for k in range(4):

Loop Body

Let k = 0 and then execute the loop body.


Let k = 1 and then execute the loop body.
Let k = 2 and then execute the loop body.
Let k = 3 and then execute the loop body.

k is called the loop variable a.k.a. the count variable


Summation
s = 0
x = 2**0
s = s+x s = 0
x = 2**1 for k in range(4):
s = s+x
x = 2**k
x = 2**2
s = s+x s = s+x
x = 2**3 print s
s = s+x

Let’s step through the mechanics of this for-loop


s = 0
for k in range(4): s -> 0
x = 2**k
s = s + x
print s

Initialize the running sum s.


1+2+4+8
s = 0
for k in range(4): s -> 0
x = 2**k
k -> 0
s = s + x
print s

We enter the loop.

The loop variable k is set to zero


1+2+4+8
s = 0
for k in range(4): s -> 0
x = 2**k
k -> 0
s = s + x
print s

k<4 is true so we execute the loop body


with that value of k.
1+2+4+8
s = 0
for k in range(4): s -> 1
x = 2**k
k -> 0
s = s + x
print s x -> 1
1+2+4+8
s = 0
for k in range(4): s -> 1
x = 2**k
k -> 0
s = s + x
print s x -> 1

k is increased by 1
1+2+4+8
s = 0
for k in range(4): s -> 1
x = 2**k
k -> 1
s = s + x
print s x -> 1
1+2+4+8
s = 0
for k in range(4): s -> 1
x = 2**k
k -> 1
s = s + x
print s x -> 1

k<4 is true so we execute the loop body


with that value of k.
1+2+4+8
s = 0
for k in range(4): s -> 3
x = 2**k
k -> 1
s = s + x
print s x -> 2
1+2+4+8
s = 0
for k in range(4): s -> 3
x = 2**k
k -> 1
s = s + x
print s x -> 2

k is increased by 1
1+2+4+8
s = 0
for k in range(4): s -> 3
x = 2**k
k -> 2
s = s + x
print s x -> 2
1+2+4+8
s = 0
for k in range(4): s -> 3
x = 2**k
k -> 2
s = s + x
print s x -> 2

k<4 is true so we execute the loop body


with that value of k.
1+2+4+8
s = 0
for k in range(4): s -> 7
x = 2**k
k -> 2
s = s + x
print s x -> 4
1+2+4+8
s = 0
for k in range(4): s -> 7
x = 2**k
k -> 2
s = s + x
print s x -> 4

k is increased by 1
1+2+4+8
s = 0
for k in range(4): s -> 7
x = 2**k
k -> 3
s = s + x
print s x -> 4
1+2+4+8
s = 0
for k in range(4): s -> 7
x = 2**k
k -> 3
s = s + x
print s x -> 4

k<4 is true so we execute the loop body


with that value of k.
1+2+4+8
s = 0
for k in range(4): s -> 15
x = 2**k
k -> 3
s = s + x
print s x -> 8
1+2+4+8
s = 0
for k in range(4): s -> 15
x = 2**k
k -> 3
s = s + x
print s x -> 8

k is increased by 1
1+2+4+8
s = 0
for k in range(4): s -> 15
x = 2**k
k -> 4
s = s + x
print s x -> 8
1+2+4+8
s = 0
for k in range(4): s -> 15
x = 2**k
k -> 4
s = s + x
print s x -> 8

k<4 is False so we exit the loop body


and proceed with the next
statement after the loop.
1+2+4+8
s = 0
for k in range(4): s -> 15
x = 2**k
k -> 4
s = s + x
print s x -> 8

Output
15
More General:
1 + 2 + 4 + … + 2**(n-1)

n = any positive integer


s = 0
for k in range(n):
x = 2**k
s = s+x
print s
for-loop Mechanics with range
for k in range(n):

Loop Body

Let k = 0 and then execute the loop body.


Let k = 1 and then execute the loop body.
Let k = 2 and then execute the loop body.
:
Let k = n-1 and then execute the loop body.
How Many Positive Integers are
there less than 10**6 and that
are divisible by
2, 3, and 5?
How Many Integers < 10**6 are
there that are divisible by
2, 3, and 5?
Example
“Left-Shifting” a String
Output:
abcd
s = ‘abcd’
bcda
n = len(s) cdab
for k in range(n): dabc
t = s[k:]+s[:k]
print t
If k==2, then s[2:]+s[:2]
looks like this: ‘cd’ + ‘ab’

Iteration with strings doesn’t always have the form “for c in s”


Break
Break
• This keyword terminates the loop when a condition
is met

Computer Programming
Continue
• a continue keyword is an expression used in the
loops that allows us to skip this iteration and return
to the beginning of the loop

Week 5: Introduction
Computer Programming
to Python 67 / 28
Example
• Enter 10 integer numbers and find the
largest of them and print the screen

Computer Programming
Solution
• Enter 10 integer numbers and find the
largest of them and print the screen

Computer Programming
Example

Computer Programming
Solution

Computer Programming
Example

Computer Programming
Solution

Computer Programming
Example

Computer Programming
Solution

Computer Programming
Example

Computer Programming
Solution

Computer Programming
Example
Write a code for giving following output

Computer Programming
Solution

Computer Programming

You might also like