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

Preview:

Citation preview

1

Chapter 4The while loop and boolean

operatorsSamuel 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)

3

If an and is used, both expressions that it

connects must be true in order for the entire

expression to be true.

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.

5

and True False

True True False

False False False

6

or True False

True True True

False True False

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.

8

However, in:

x>3 or y<=4

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

expression is True.

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.

10

In

not x > 3

what is the expression the not immediately

precedes?

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?

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?

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.

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.

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:

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?

17

x = 0

while x < 3:

print(x)

How many times will the loop be executed?

An infinite number of times.

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).

19

So we can write, for instance,

x = 0

while x < 3:

print(x)

x = x + 1

.

20

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

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?

22

The loop,

x = 0

while x < 3:

print(x)

x = x + 1

will be executed three times. What is its

output?

23

The output of

x = 0

while x < 3:

print(x)

x = x + 1

Is 0, 1 and 2.

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.

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')

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.

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’.

28

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

is a character?

How does the computer evaluate it?

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.

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

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

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

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)

34

Else the answer will be printed.

The entire program is now:

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)

Recommended