JK

Lecture on Decision Structures and Boolean Logic

The if Statement

  • Controls the order of execution of statements.

  • Control Structures:

    • Sequence structure: Executes statements in order.

    • Decision structure: Executes actions based on conditions (Also known as selection structure).

Boolean Expressions and Relational Operators

  • Boolean Expression: Tested to check if it is true or false (e.g., a > b).

  • Relational Operators:

    • > - greater than

    • < - less than

    • >= - greater than or equal to

    • <= - less than or equal to

    • == - equal

    • != - not equal

The if-else Statement

  • Structure:

    • Syntax: if condition: statements else: other statements

    • Executes one of two paths based on condition (true/false).

Comparing Strings

  • Strings compared using ==, !=, >, < based on ASCII values.

  • Case sensitive: e.g., 'abc' > 'Abc'.

Nested Decision Structures

  • A structure containing another decision structure.

  • Syntax: Nested if statements should maintain proper indentation.

The if-elif-else Statement

  • A simplified version of nested structures allowing multiple conditions.

  • Syntax:

if condition_1:  
  statements  
elif condition_2:  
  statements  
else:  
  statements

Logical Operators

  • Logical Operators: Create complex Boolean expressions.

    • and: True if both operands are true.

    • or: True if at least one operand is true.

    • not: Reverses Boolean value of the operand.

Checking Numeric Ranges

  • Within a Range: Use and (e.g., x \geq 10 \text{ and } x \leq 20).

  • Outside a Range: Use or (e.g., x < 10 \text{ or } x > 20).

Boolean Variables

  • Represents two values: True or False.

  • Often used as flags to signal conditions in programs.

    • Flag set to False: Condition does not exist.

    • Flag set to True: Condition exists.