Lecture 07
Lecture 07
▪ Two types
▪ while loop
▪ for loop
SENTINEL LOOPS
▪A sentinel loop continues to
process data until reaching a
special value that signals the end.
▪This special value is called the
sentinel.
4
from … import …
>>> from math import sqrt
>>> sqrt(25)
Difference? Problem?
n=0
>>> range(1)
>>> range(25)
>>> range(2,10,2)
THE for LOOP
print(x)
ACCUMULATOR LOOP:
SUMMATION
Compute and print the sum of the numbers between 1
and 5, inclusive
total = 0
for n in range(1, 6):
total = total + n total += n
print(total)
NESTED LOOPS
while (<Condition>):
while (<Condition>):
<statement-1>
<statement…>
BREAK & CONTINUE
def Greeting():
name = ‘Mary’
name = 'Mary Poppins‘
print(name) print(name)
print(name)
GLOBAL VARIABLES
name = 'Mary Poppins‘
def Greeting():
print(name)
Greeting()
GLOBAL VS LOCAL
num = 6
def multi(): 18
Local: ?
num = 6
num = num * 3 Global: ? 6
print(‘local variable: ’,
num)
multi()
print(‘Global: ’, num)
BEST PROGRAMMING
PRACTICES
▪ Preconditions
▪ Postconditions
COMMON PROBLEMS
▪ Indentation issues