35
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

Embed Size (px)

Citation preview

Page 1: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

1

Chapter 4The while loop and boolean

operatorsSamuel Marateck ©2010

Page 2: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

2

Boolean operators are used in boolean expressions and operate on other boolean expressions. They are the and, or and not.For instance:

X>3 and y<=4X>3 or y<=4not(X>3 and y<=4)

Page 3: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

3

If an and is used, both expressions that it

connects must be true in order for the entire

expression to be true.

Page 4: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

4

If an or is used, if either expression that it

connects is true, then the entire

expression is true. The following tables

show how the and and or work.

Page 5: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

5

and True False

True True False

False False False

Page 6: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

6

or True False

True True True

False True False

Page 7: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

7

So for instance in:

x>3 and y<=4

If x>3 is True and y<=4 is False, the entire

expression is False.

Page 8: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

8

However, in:

x>3 or y<=4

If x>3 is True and y<=4 is False, the entire

expression is True.

Page 9: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

9

The not changes the boolean expression it

precedes so that not True is False. The not

must precede a boolean expression and

operates on the expression it immediately

precedes.

Page 10: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

10

In

not x > 3

what is the expression the not immediately

precedes?

Page 11: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

11

In

not x > 3

the not immediately precedes the variable x

which is not a boolean expression. What

happens and how do you correct this?

Page 12: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

12

It causes a compilation error. You write it as:

not (x > 3)

If the value of x is 3, what is the value of the

entire expression?

Page 13: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

13

not (x > 3)If the value of x is 3, what is the value of the entire expression?

Since x > 3 is false, not (x > 3) is true.

Page 14: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

14

The while loop

The while loop differs from the for loop in

that the variables in the while statement

must be defined before the loop.

Page 15: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

15

So in:

while x < 3:

the value of x must be defined before the

loop –this is called priming the while --and

execution of the loop will continue

while the value of x < 3 is true. Here is an

example:

Page 16: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

16

x = 0while x < 3: print(x)Since the value of x < 3 is true initially, the loop begins execution. The indented statements that follow the while are said to be in the scope of the while.How many times will the loop be executed?

Page 17: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

17

x = 0

while x < 3:

print(x)

How many times will the loop be executed?

An infinite number of times.

Page 18: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

18

So unless a statement in the scope of the while statement changes the boolean expression in the while, execution of the while continues.x = 0while x < 3: print(x).

Page 19: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

19

So we can write, for instance,

x = 0

while x < 3:

print(x)

x = x + 1

.

Page 20: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

20

Now the scope of the while,x = 0while x < 3: print(x) x = x + 1is . print(x) x = x + 1

Page 21: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

21

Since x changes, the loop will only be

executed a finite number of times.

x = 0

while x < 3:

print(x)

x = x + 1

How many times will it be executed?

Page 22: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

22

The loop,

x = 0

while x < 3:

print(x)

x = x + 1

will be executed three times. What is its

output?

Page 23: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

23

The output of

x = 0

while x < 3:

print(x)

x = x + 1

Is 0, 1 and 2.

Page 24: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

24

One of the purposes of the while is to insure

integrity of the input. In the following, the

required input is between 0 and 9

Inclusively.

So the while continues until the input

is between 0 and 9.

Page 25: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

25

invalid = True #primes the while while invalid : #continues if invalid is True n = input('type your number please \n') if 0 <= int(n) <= 9: invalid = False else: #continues the while print(n + ' is invalid') print('done')

Page 26: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

26

In while invalid, the boolean expression is

simply the expression invalid which is

originally True. When the input is in the

required range, invalid changes to False and

execution quits the loop.

Page 27: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

27

The ASCII Table

All characters used by the computer are given a code. For instance, the digits ‘0’ through ‘9’ have the codes 48 through 57 in the ascii table. The letters ‘A’ through ‘Z’ have the codes65 through 92. The function ord() gives the ascii code for a given character and chr() gives the character corresponding to the ascii code. Soord(‘A’) returns 65 and chr(65) returns ‘A’.

Page 28: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

28

What’s the meaning of ‘0’<= c<=‘9’ where c

is a character?

How does the computer evaluate it?

Page 29: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

29

What’s the meaning of ‘0’<= c<=‘9’ where c is a character?How does the computer evaluate it?

It’s the equivalent of 48<=ord(c)<=57. So thecomputer tests if the ascii value of the character c is between the two limits 48 and 57.

Page 30: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

30

The following counts the number of characters after the period.

#counts # of characters after the period #Tests for no period also

s = input('Type your sentence please \n') n = -1 #compensates for counting the periodflag = False for c in s: if c == '.' : flag = True

Page 31: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

31

A flag is initialized and set to False

#counts # of characters after the period #Tests for no period also

s = input('Type your sentence please \n')

n = -1 #compensates for counting the period

flag = False

Page 32: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

32

Once the flag is True, the counting begins in n=n+1.

s = input('Type your sentence please \n') n = -1 #compensates for counting the periodflag = False for c in s: if c == '.' : flag = True if flag : n = n + 1 #counts the period also

Page 33: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

33

If no period is encountered, the flag remains

False. So an error message is printed after

the loop is executed:

if flag == False: print('No period found ')

else:

print('# of characters after the period is ', n)

Page 34: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

34

Else the answer will be printed.

The entire program is now:

Page 35: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010

35

#counts # of characters after the period #Tests for no period also s = input('Type your sentence please \n') n = -1 #compensates for counting the periodflag = False for c in s: if c == '.' : flag = True if flag : n = n + 1 #counts the period alsoif flag == False: print('No period found ') else: print('# of characters after the period is ', n)