Logical Operators and Looping Structures

Review Question:
Write a program that reads an integer from the user, and prints a
message indicating whether or not the number is divisible by 4.

• Logical operators can be applied only to boolean values
• The result is also a boolean value (type bool) - either True or False

The logical operators:
and
or
not

• p and q is true only when both p and q are true. Otherwise p and q is false.

• p or q is true when at least one of p and q is true. If p and q are both false, then p or q is false.

• not(p) is false if p is true, and not(p) is true if p is false.

Examples:
i = 14
j = 18
c1 = 'elf'
c2 = 'help'

print (i<j)   # output: true
print i < j and i == i   # output: true
test = not(i < j)   # test is false
test2 = c1 == c2   # test2 is false
test3 = i<j or c1>c2   # test3 is true
test4 = i<j and c1 > c2   # test 4 is false

Important Note about Comparing Floating-Point Numbers

• Floating-point numbers have limited precision.
• Calculations lead to roundoff errors.

Consider the following code:

import math   # import math library, which includes sqrt() function

root = math.sqrt(2)
double diff = root*root - 2   # we expect diff to be 0

if diff == 0:
print "sqrt(2)^2 - 2 is 0"
else:
print "sqrt(2)^2 - 2 is ", diff

Output:
sqrt(2)^2 - 2 is 4.4408920985e-16

You should compare whether floating-point numbers are "close enough", rather than exactly equal. Roundoff errors are unavoidable.

Exercise: Write a program that reads two floating-point numbers from the keyboard, and prints a message indicating whether they are within epsilon of each other, where epsilon is defined to be 0.0001.

Exercise: Write a program that reads a string from the keyboard, and prints a message indicating whether or not each of the vowels a, e, i, o and u occur in the string. To do this, we will need to import the string library, and use the find() function from this library.

Example:
import string
s = "hello world hello"
print string.find(s, "hello")

Output: 0

Iteration Structures

The while statement

Example:
while I'm hungry:
eat another bite

Example:
while there are more lines of text in the input file:
read the next line from the file

Syntax:
while condition:
statement(s)

How it works:
A while statement executes a block of code repeatedly, as long as the associated condition is true. The condition is evaluated before each execution of the statements in the while body, and if the expression evalutes to false, execution of the while loop terminates.

Example:
count = 1
while count <= 5:
count = count + 1

print "count is ", count

Example:
# initialize variables to use in loop
num = 1
sum = 0

# as long as num is at most 5, add num to the sum
while num <= 5:
sum = sum + num  # add the current value of num to the sum
num = num + 1    # add 1 to num - what happens if I omit this statement?

print "The sum of the first 5 positive integers is ", sum

Exercise: Write a program that prints the integers from 1 to 15 to the console window, one number per line.

Exercise: Write a program that asks the user to enter 10 numbers, and prints the sum of those numbers.