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.

Python
ifStatement Syntax:
if condition:
statement
statement
Syntax Components:
ifclause: The first line starting with theifkeyword, 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
ifclause. If the condition is True, the block statements execute; if False, the entire block is skipped.
Single-Line
ifStatements:An
ifstatement can be written on a single line if it executes only one statement.Syntax:
if condition: statementExample:
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,
).
Relational Operators:
An operator that determines whether a specific relationship exists between two operand values.
Expression | Meaning |
|---|---|
Is | |
Is | |
Is | |
Is | |
Is | |
Is |
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.

Programmatic Applications:
Any relational operator can be used within an
ifclause (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
ifclause andelseclause must align vertically on the same column.Statements inside both the
ifblock and theelseblock must be consistently indented.

Control Flow Mechanics:
If the condition is True, the statements inside the
ifblock execute, and control jumps directly to the statement following the entireif-elseblock.If the condition is False, the statements inside the
elseblock execute, and control jumps directly to the statement following the entireif-elseblock.

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.

Detailed Character-by-Character Breakdown ("Mary" vs "Mark"):
Position 1:
'M'(ASCII ) vs'M'(ASCII ) -> Equal.Position 2:
'a'(ASCII ) vs'a'(ASCII ) -> Equal.Position 3:
'r'(ASCII ) vs'r'(ASCII ) -> Equal.Position 4:
'y'(ASCII ) vs'k'(ASCII ) -> Since , `