Chapter 3: Decision Structures and Boolean Logic

Control Structures and Single Alternative Decision Structures

  • Control Structure Definition:

    • A control structure is a logical design that controls the order in which a set of statements executes within a program.

  • Sequence Structure:

    • A set of statements that execute strictly in the order they appear, sequentially from top to bottom.

  • Decision Structure (Selection Structure):

    • A control structure that executes specific action(s) only when a particular condition exists.

  • Flowchart Representation of Decision Structures:

    • A diamond symbol represents a True/False condition that must be evaluated.

    • Actions are conditionally executed, meaning they are performed only when the condition evaluates to True.

  • Single Alternative Decision Structure:

    • Provides only one alternative path of execution.

    • If the tested condition is True, the conditional path is executed.

    • If the condition evaluates to False, the decision structure is exited without performing any action.

A simple decision structure
  • Python if Statement Syntax:

if condition:
    statement
    statement
  • Syntax Components:

    • if clause: The first line starting with the if keyword, followed by the condition, and ending with a colon :.

    • Condition: A expression that evaluates to either True or False.

    • Statement Block: The indented statements underneath the if clause. If the condition is True, the block statements execute; if False, the entire block is skipped.

  • Single-Line if Statements:

    • An if statement can be written on a single line if it executes only one statement.

    • Syntax: if condition: statement

    • Example: if score > 59: print('You passed!')

Boolean Expressions and Relational Operators

  • Boolean Expression:

    • An expression evaluated by a decision structure to determine whether it is True or False (for example, a>ba > b).

  • Relational Operators:

    • An operator that determines whether a specific relationship exists between two operand values.

Expression

Meaning

x>yx > y

Is xx greater than yy?

x<yx < y

Is xx less than yy?

x≥yx \ge y

Is xx greater than or equal to yy?

x≤yx \le y

Is xx less than or equal to yy?

x==yx == y

Is xx equal to yy?

x!=yx != y

Is xx not equal to yy?

  • Relational Operator Mechanics:

    • Dual-relationship operators (>= and <=) test multiple conditions simultaneously; it is sufficient for either component relationship to hold for the overall expression to evaluate to True.

    • The == operator determines equality between operands. It must not be confused with the assignment operator =.

    • The != operator evaluates to True if the two operands are not equal to each other.

Example decision structure
  • Programmatic Applications:

    • Any relational operator can be used within an if clause (e.g., if balance == 0:, if payment != balance:).

    • Block nesting: A decision block can be placed inside another structure (such as a function). Statements inside the inner block must be indented relative to the outer block.

Dual Alternative Decision Structures: The if-else Statement

  • Dual Alternative Decision Structure:

    • A decision structure providing two distinct execution paths.

    • Executes one set of statements if the condition evaluates to True, and a separate set of statements if the condition evaluates to False.

  • Python Syntax:

if condition:
    statements
else:
    other_statements
  • Alignment and Indentation Rules:

    • The if clause and else clause must align vertically on the same column.

    • Statements inside both the if block and the else block must be consistently indented.

A dual alternative decision structure
  • Control Flow Mechanics:

    • If the condition is True, the statements inside the if block execute, and control jumps directly to the statement following the entire if-else block.

    • If the condition is False, the statements inside the else block execute, and control jumps directly to the statement following the entire if-else block.

Conditional execution in an if-else statement

String Comparison Mechanics

  • String Evaluation:

    • Strings can be compared for equality or inequality using == and !=.

    • String comparisons in Python are strictly case-sensitive (e.g., 'Python' is not equal to 'python').

  • Lexicographical Comparison (>, <, >=, <=):

    • Strings are compared character-by-character based on the numerical ASCII value of each character.

    • Comparison begins at the first character and proceeds sequentially to the right until a mismatch is encountered.

    • Substring rule: If a shorter word is an exact prefix substring of a longer word, the longer word is evaluated as greater than the shorter word.

Comparing each character in a string
  • Detailed Character-by-Character Breakdown ("Mary" vs "Mark"):

    • Position 1: 'M' (ASCII 7777) vs 'M' (ASCII 7777) -> Equal.

    • Position 2: 'a' (ASCII 9797) vs 'a' (ASCII 9797) -> Equal.

    • Position 3: 'r' (ASCII 114114) vs 'r' (ASCII 114114) -> Equal.

    • Position 4: 'y' (ASCII 121121) vs 'k' (ASCII 107107) -> Since 121>107121 > 107, `