Chapter 3: Decision Structures and Boolean Logic

COSC 1010: Intro to Software Development

Instructor: Dr. Md. Tahmidul Islam Molla
Department of Computer Science, Marquette University
Copyright © 2021, 2018, 2015 Pearson Education, Inc. All Rights Reserved


Class Activity

  • Navigate to Content > Chapter 2 > Chapter Two Class Activity_Sep 12th

  • Raise your hand if you need help finding the quiz.


Quiz Information

  • Quiz 2 Posted in D2L

  • Navigate to Content > Quizzes > Quiz 2

  • Raise your hand if you need help finding the quiz.


CHAPTER 3: Decision Structures and Boolean Logic

Topics Covered

  • The if Statement

  • The if-else Statement

  • Comparing Strings

  • Nested Decision Structures and the if-elif-else Statement

  • Logical Operators

  • Boolean Variables

  • Turtle Graphics: Determining the State of the Turtle


The if Statement

Basics

  • Sequence structure: A set of statements that execute in the order they appear.

  • Control structure: A logical design that controls the order in which a set of statements execute.

Example Program

  • Program requests user input:

  name = input('What is your name? ')
  age = int(input('What is your age? '))
  print('Here is the data you entered:')
  print('Name:', name)
  print('Age:', age)

Decision Structure

  • The decision structure allows specific actions to be performed contingent on a condition being true (also known as a selection structure).

Flowchart Representation

  • In a flowchart, a diamond shape represents a true/false condition.

  • Actions can be executed conditionally, only when the condition is true.

Single Alternative Decision Structure

  • Provides only one alternative path of execution.

  • If the condition is not true, it exits the structure.

Python Syntax for if Statement

  • Basic syntax:

  if condition:
      Statement
  • The first line is known as the if clause, containing the keyword 'if' followed by a condition.

  • The condition can be either true or false. If true, the block statements are executed; otherwise, they are skipped.

Boolean Expression & Relational Operators

  • A Boolean expression is tested by if statements to determine its truth value.

    • Example: a > b is true if a is greater than b; false otherwise.

  • Relational operator: Determines if a specific relationship exists between two values.

    • Examples: >, <, >=, <=, ==, !=.

Expression

Meaning

x > y

Is x greater than y?

x < y

Is x less than y?

x >= y

Is x greater than or equal to y?

x <= y

Is x less than or equal to y?

x == y

Is x equal to y?

x != y

Is x not equal to y?


Boolean Expressions in Python

  • Python evaluates Boolean expressions which can utilize comparison operators (3C,3E,3D,3D)+ more, producing True or False outputs.

  • Example evaluations:

  >>> 2 < 3               # True
  >>> 2 > 3               # False
  >>> 2 == 3              # False
  >>> 2 != 3              # True
  >>> 2 <= 3              # True
  >>> 2 >= 3              # False
  >>> 2+4 == 2*(9/3)      # True

Logical Operators

  • Logical operators create complex Boolean expressions:

    • and: True if both operands are true.

    • or: True if either operand is true.

    • not: Reverses the truth value of its operand.


The if-else Statement

Basics

  • The dual alternative decision structure provides two possible paths of execution.

  • Syntax:

  if condition:
      statements
  else:
      other statements
  • The if and else clauses must be aligned, and statements must be consistently indented.

Example Program

if temp > 86:
    print('It is hot!')
    print('Be sure to drink liquids.')
else:
    print('It is not hot.')
    print('Bring a jacket.')
print('Goodbye.')

Nested Decision Structures

  • A decision structure can be nested inside another decision structure.

  • Example: Determining loan qualification based on annual salary and employment duration.

    • Must earn at least $30,000/year

    • Must have been employed for at least two years

Syntax in Nested Structures

  • It is critical to use proper indentation for readability and to avoid errors in Python.

  • The else clause should align with its corresponding if clause.


The if-elif-else Statement

  • If-elif-else statements simplify the writing of nested decision structures and allow multiple conditions to be tested sequentially.

  • Syntax:

  if condition1:
      statement  
  elif condition2:
      statement  
  else:
      statement  
  • Cleanly aligns the if, elif, and else clauses, ensuring consistent indentation for conditioned blocks.


Logical Operators in Depth

Using Logical Operators

  • The and Operator: True if both expressions are true.

  • The or Operator: True if either expression is true.

  • The not Operator: Reverses the truth value of the expression.


Example Programs Using Logical Operators

if temperature < 20 and temperature > 12:
    print('The temperature is in the danger zone.')
if temperature < 20 or temperature > 100:
    print('The temperature is too extreme')
if not(temperature > 100):
    print('This is below the maximum temperature.')

Boolean Variables

  • A Boolean variable can hold one of two values: True or False.

  • Commonly used as flags to indicate whether certain conditions are satisfied in a program.

Example of Boolean variable usage

hungry = True
if hungry:
    print('Time to eat!')

Conditional Expressions

  • Syntax:

  value_1 if condition else value_2
  • If the condition is true, returns value1; otherwise, returns value2.

Example

  • grade = 'Pass' if score > 59 else 'Fail'

  • Equivalent to:

  if score > 59:
      grade = 'Pass'
  else:
      grade = 'Fail'

Summary of Chapter 3

  • Covered:

    • Decision structures: single alternative, dual alternative, nested structures.

    • Use of relational operators and logical operators in Boolean expressions.

    • String comparisons in Boolean expressions.

    • Boolean variables including examples and applications.