Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.

Similar presentations


Presentation on theme: "Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming."— Presentation transcript:

1 Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming

2 Learning outcomes  At the end of this lecture, students should:  be familiar with boolean values True and False  be able to evaluate a boolean expression  be able to use relational operators (>, =, != and ==)  be able to use conditional statements (if, else) CompSci 1012

3 Boolean expressions - conditions  A condition is an expression which evaluates to either True or False  An expression which evaluates to either True or False is called a boolean expression. George Boole (1815-1964) invented Boolean algebra. CompSci 1013

4 Boolean Variables  Boolean variables are used to store a value which is either:  True  False  Example:  Naming a Boolean Variable:  Names of boolean variables express a fact that should be True or False.  Good boolean variable names: is_old_enough = True has_passed = True has_won_game = True has_passed = True has_won_game = True age = True a = True age = True a = True Bad boolean variable names CompSci 1014

5 Boolean variables  We can assign one boolean variable to another boolean variable, e.g.,  We can output the value stored in a boolean variable, e.g., print(has_passed) print(has_won_game) print(has_passed) print(has_won_game) has_passed = True has_won_game = has_passed has_passed = True has_won_game = has_passed CompSci 1015

6 Relational Operators  In boolean expressions, relational operators are used to compare values  The relational operators are: == > < >= <= != equal to greater than not equal to greater than or equal to less thanless than or equal to CompSci 1016

7 Boolean variables  Variables can be used to store the result of a comparison, i.e., to store a boolean expressions  For example: exam_mark = 76 age = 8 points_so_far = 56 passed_exam = exam_mark >= 50 has_won_game = points_so_far > 70 is_old_enough = age > 10 is_old_enough = passed_exam != has_won_game print(passed_exam, has_won_game, is_old_enough ) exam_mark = 76 age = 8 points_so_far = 56 passed_exam = exam_mark >= 50 has_won_game = points_so_far > 70 is_old_enough = age > 10 is_old_enough = passed_exam != has_won_game print(passed_exam, has_won_game, is_old_enough ) True False True DEMO Example01.py CompSci 1017

8 Exercise 1  What is the output after executing the following code? val1 = 50 val2 = 53 diff = abs(val1 - val2) print("1. ", val1 != val2) print("2. ", val1 >= val2 - 3) print("3. ", val2 % 2 == 0) print("4. ", diff < 3) val1 = 50 val2 = 53 diff = abs(val1 - val2) print("1. ", val1 != val2) print("2. ", val1 >= val2 - 3) print("3. ", val2 % 2 == 0) print("4. ", diff < 3) CompSci 1018

9 Boolean expressions – logical operators  As well as the relational operators, we can use the following logical operators in boolean expressions:  The three truth tables for these logical operators are shown below: andnot or CompSci 1019

10 Logical operators - examples  Assume that the variable, value, has been initialised. value > 10 and value < 100 Is value greater than 10 and less than 100 value >= 10 or value == 5 Is value greater than or equal to 10 or is value equal to 5 not value > 8 Is value not greater than 8 not (value > 8 or value == 1) value <= 8 and value != 1 Is value not greater than 8 and not equal to 1 or CompSci 10110

11 Chaining comparison operators  Comparison operators can be chained. Consider the following examples:  Note:  The comparison is performed between each pair of terms to be evaluated.  For instance, in the first example, 10 < value is evaluated to True AND value <100 is evaluated. Is value greater than 10 and less than 100 10 < value < 100 10 < value < 20 Is value greater than 10 and less than 20 CompSci 10111

12 Operator precedence  Below is the priority order of operations: Unary operators+, - Multiplicative arithmetic comparisons Additive arithmetic *, /, % +, -, =, !=, == Logical not Logical or Logical and not and or Assignment=, +=, -= HIGH LOW (Done first) (Done last) Exponentiation ** CompSci 10112

13 Use parentheses in boolean expressions  Use parentheses to group sections of your boolean expressions to make the program more readable, e.g.,  but do not overload your boolean expressions with unnecessary parentheses, e.g., a > b or (a > c and b < 45) ((a > b) or ((a > c) and (b < 45))) a > b or a > c and b < 45 “and” has a priority order Not clear! overuse of unnecessary parentheses CompSci 10113

14 Exercise 2  What is the output after executing the following code? def test_booleans(value): result = value > 10 or value <= 5 and value != 12 print("1.", result) result = (value > 10 or value <= 5) and value != 12 print("2.", result) def main(): test_booleans(12) test_booleans(13) main() def test_booleans(value): result = value > 10 or value <= 5 and value != 12 print("1.", result) result = (value > 10 or value <= 5) and value != 12 print("2.", result) def main(): test_booleans(12) test_booleans(13) main() CompSci 10114

15 Logical operators – exercises 3  Assume that the variable, value, has been initialised. Write the following four boolean expressions in Python: a) is value less than 100 or greater than 200 b) is value not equal to either 100 or 10 c) is value greater than 5 but not equal to 10 d) is value between 5 and 20 or equal to 50 CompSci 10115

16 Control Structures  In all the programs written so far the statements inside functions are executed in the order in which they are written, e.g., all the statements in the main() function are executed and they are executed in sequence.  We would like to be able to control the execution of our code so that blocks of code are only executed under certain conditions. Control structures allow us to change the flow of statement execution in our programs. CompSci 10116

17 A selection statement  A decision point in the program  a choice of doing something or not doing it, either do a block of code or not  alters the flow of control  For example: Waterfall Cave Village Birds Choose one option or the other, but not both. CompSci 10117

18 Python syntax for an if statement  In an if statement (selection statement) the code in the if block is executed if the condition is true  If the condition is false, then none of the statements in the block will be executed if boolean_expression: statement1 statement2 If the condition is true Conditional code If the condition is false Condition Indentation is important in Python (indicates the structure of code). Use one tab Be consistent with indentation Indentation is important in Python (indicates the structure of code). Use one tab Be consistent with indentation CompSci 10118

19 Example  What is the output after executing the following code?  Case 1:  Case 2 radius = -2 if radius >= 0: area = radius * radius * math.pi print("The area of the circle of radius", radius, "is", area) radius = -2 if radius >= 0: area = radius * radius * math.pi print("The area of the circle of radius", radius, "is", area) if radius >= 0: area = radius * radius * math.pi print("The area of the circle of radius", radius, "is", area) if radius >= 0: area = radius * radius * math.pi print("The area of the circle of radius", radius, "is", area) No output! NameError: name 'area' is not defined CompSci 10119

20 Example  What is the output after executing the following code? number = 25 if number > 30: print("A") if number >= 25: print("B") print("C") number = 32 if number % 6 < 2: print("D") if number // 3 != 10: print("E") number = 25 if number > 30: print("A") if number >= 25: print("B") print("C") number = 32 if number % 6 < 2: print("D") if number // 3 != 10: print("E") BCBC CompSci 10120

21 Exercises  What is the output after executing the following code?  If number is 5:  If number is 6: number = int(input("Enter an integer: ")) if number % 5 == 0: print("HiFive!") if number % 2 == 0: print("HiEven") number = int(input("Enter an integer: ")) if number % 5 == 0: print("HiFive!") if number % 2 == 0: print("HiEven") CompSci 10121

22 Exercise 4  What is the output after executing the following code? a = 42 b = 17 c = 94 if a > b and a > c: print("You") if not (a > b and a > c): print("cannot") if a > b or a > c: print("tuna") if not(a > b or a > c): print("fish") a = 42 b = 17 c = 94 if a > b and a > c: print("You") if not (a > b and a > c): print("cannot") if a > b or a > c: print("tuna") if not(a > b or a > c): print("fish") CompSci 10122

23 If statements – a common mistake  Remember that the equality operator is ==. What is the problem with the code below? val1 = 50 val2 = 53 if val1 = val2-3: print("Unbelievable") val1 = 50 val2 = 53 if val1 = val2-3: print("Unbelievable") Note: single = is the assignment operator. if val1 = val2-3: ^ SyntaxError: invalid syntax val1 = 50 val2 = 53 if val1 == val2-3: print("Unbelievable") val1 = 50 val2 = 53 if val1 == val2-3: print("Unbelievable") Unbelievable CompSci 10123

24 Comparing float numbers  Floating point numbers are always stored approximately. It is dangerous to test doubles for equality using ==.  Test equality of floats by accepting all values within an acceptable error limit: val1 = 0.3 val2 = 0.1 * 3 if val1 == val2: print("Sigh!") if val1 != val2: print("maybe yes, maybe no!") val1 = 0.3 val2 = 0.1 * 3 if val1 == val2: print("Sigh!") if val1 != val2: print("maybe yes, maybe no!") maybe yes, maybe no! val1 = 0.3 val2 = 0.1 * 3 error_limit = 0.00001 if abs(val1 - val2) < error_limit: print("Close enough!") val1 = 0.3 val2 = 0.1 * 3 error_limit = 0.00001 if abs(val1 - val2) < error_limit: print("Close enough!") Close enough! CompSci 10124

25 Exercise: Complete the function  Complete the print_message() function which has an equal chance of printing "now", "soon" and "never". Example output to the completed program is shown below: def print_message(): def main(): print("Life will improve") print_message() main() def print_message(): def main(): print("Life will improve") print_message() main() Life will improve now Life will improve soon CompSci 10125

26 Algorithm  Complete the function: Generate a random number between 0 to 2If the number is 0, print “now”If the number is 1, print “soon”If the number is 2, print “never” CompSci 10126

27 Summary  In a Python program:  boolean expressions evaluate to either True or False  be familiar with boolean values True and False  relational operators (>, =, != and ==) are used to compare values  Logical operators (not, and, or) can be used to build more complex boolean expressions  if statements are used if a block of code is to be executed only if a particular condition is True CompSci 10127


Download ppt "Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming."

Similar presentations


Ads by Google