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

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advanced Piloting Cruise Plot.
Kapitel S3 Astronomie Autor: Bennett et al. Raumzeit und Gravitation Kapitel S3 Raumzeit und Gravitation © Pearson Studium 2010 Folie: 1.
Kapitel 21 Astronomie Autor: Bennett et al. Galaxienentwicklung Kapitel 21 Galaxienentwicklung © Pearson Studium 2010 Folie: 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Chapter 1 Image Slides Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
Multiplying binomials You will have 20 seconds to answer each of the following multiplication problems. If you get hung up, go to the next problem when.
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Year 6 mental test 5 second questions
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ZMQS ZMQS
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
ABC Technology Project
3 Logic The Study of What’s True or False or Somewhere in Between.
© S Haughton more than 3?
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
VOORBLAD.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
“Start-to-End” Simulations Imaging of Single Molecules at the European XFEL Igor Zagorodnov S2E Meeting DESY 10. February 2014.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Squares and Square Root WALK. Solve each problem REVIEW:
Do you have the Maths Factor?. Maths Can you beat this term’s Maths Challenge?
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Chapter 5 Test Review Sections 5-1 through 5-4.
SIMOCODE-DP Software.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Slippery Slope
Januar MDMDFSSMDMDFSSS
Week 1.
Analyzing Genes and Genomes
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Figure Essential Cell Biology (© Garland Science 2010)
Intracellular Compartments and Transport
A SMALL TRUTH TO MAKE LIFE 100%
1 Unit 1 Kinematics Chapter 1 Day
PSSA Preparation.
Essential Cell Biology
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
1 PART 1 ILLUSTRATION OF DOCUMENTS  Brief introduction to the documents contained in the envelope  Detailed clarification of the documents content.
How Cells Obtain Energy from Food
Immunobiology: The Immune System in Health & Disease Sixth Edition
Chapter 30 Induction and Inductance In this chapter we will study the following topics: -Faraday’s law of induction -Lenz’s rule -Electric field induced.
CpSc 3220 Designing a Database
Traktor- og motorlære Kapitel 1 1 Kopiering forbudt.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Presentation transcript:

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<=4 X>3 or y<=4 not(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 andTrueFalse True False

6 orTrueFalse True FalseTrueFalse

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 = 0 while 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 = 0 while 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 = 0 while x < 3: print(x) x = x + 1 is. 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 codes 65 through 92. The function ord() gives the ascii code for a given character and chr() gives the character corresponding to the ascii code. So ord(‘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 the computer 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 period flag = 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 period flag = 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 period flag = False for c in s: if c == '.' : flag = True if flag : n = n + 1 #counts the period also if flag == False: print('No period found ') else: print('# of characters after the period is ', n)