Loop Structures

Exercise: Prompt the user to enter a sequence of positive integers, one at a time. The user enters zero or a negative number to quit. Print the sum of the positive integers.

This is an example of a loop called a sentinel loop. The loop gets and processes values until seeing a special value called the sentinel, that indicates that processing should stop. In the above exercise, the sentinel is either 0 or a negative number.

In general, the psuedo-code for a sentinel loop is like this:

read the first value
while value is not the sentinel:
     process the value
     read the next value

Example: Prompt the user to enter exam scores, and print the average score. The user enters a negative value to quit.

Set sum to 0
Set count to 0
Read the first score
while score is non-negative:
     add score to sum
     add 1 to count
     read next score
print average

Sample Output:
Enter a number (negative to quit): 67
Enter a number (negative to quit): 88
Enter a number (negative to quit): 99
Enter a number (negative to quit): -1

The average of the grades is 84.67.

Example (Using a loop to check for valid input)
Write a program that asks the user for a positive integer, and continues prompting the user until the value entered is positive. Then compute the sum of the integer's digits.
Note: Suppose the user enters 526. Then 526/10 = 52, and
526%10 = 6. So 52/10 = 5, and 52%10 = 2. And 5/10 = 0, and
5%10 = 5.

For Loops

for loops are sometimes called definite loops, since for loops execute the body of the loop a specified, or definite, number of times.

General form:
for variable in sequence:

The variable after the keyword "for" is called the loop index - it takes on the values in the sequence, one at a time, and the loop body is executed once for each value.

for i in [1, 2, 3, 4]:
   print i


The range() function


>>> range(5)
[0, 1, 2, 3, 4]
>>> range(3, 8)
[3, 4, 5, 6, 7]
>>> range(2, 10, 3)
[2, 5, 8]

Example: Write a for loop that prints the even numbers from 2 to 20. The output should appear on one line, with the numbers separated by a blank space.

Example: What is the output of the following loop? Can you re-write it so it looks nicer?
for i in range(6):
   print i, 2**i

Example: What is the output of this loop?
for num in [3, 2, 1, 5]:
   print num,

Simple Functions

Recall the form of a function definition:
def functionName(parameter1, parameter2, parameter3):

The statements which are indented under the function's header are called the function body, and these statements are executed when the function is called.

To call this function:
functionName(value1, value2, value3)

When the function is called, the statements in the body of the function are executed, with parameter1 set to value1, parameter2 set to value2, and parameter3 set to value3.

# This program contains a function that takes an integer and
#  prints 2 raised to the specified power.

def main():
    # for each i from 1 to 10, print 2**i
    for i in range(1, 11):
        printPower(i)  # call (execute) the printPower function

def printPower(theExponent):
    value = 2**theExponent
    print value

main()  # call the main function


Write a function called printLineTwice() that takes a string as its parameter, and prints that string twice on a single line.
Write a main function that  calls printLineTwice() 10 times.