Python Control Structures

Fundamentals of Program Statements and Execution Flow

  • Individual instructions given to a computer to perform a specific task are known as statements.
  • By default, statements in a program are executed sequentially, one after another, in the exact order in which they appear.
  • In various programming scenarios, it becomes necessary to alter or dictate the normal sequential order of execution based on specific test conditions.
  • Control statements are programming constructs employed specifically to control or change the flow of program execution.

Overview of Python Control Structures

  • A control structure is a programming construct that affects and regulates the flow of execution of a program.
  • Python provides three primary types of control structures:
    • Sequential Statements
    • Selection Statements
    • Iterative Statements

Control structures hierarchy and sequential execution diagram

Sequential Statements

  • Sequential statements refer to instructions that are executed strictly in a sequential order, meaning one statement follows directly after another without jumping, branching, or skipping.
  • A sequential structure is formally referred to as a straight-line path.
  • The operational flow follows an uninterrupted linear sequence:
    • Statement 1 executes first.
    • Upon completion of Statement 1, Statement 2 executes.
    • Upon completion of Statement 2, Statement 3 executes, continuing downward linearly.

Selection Statements (Decision-Making Constructs)

  • Certain programming problems cannot be resolved through an unvarying sequence of ordered steps.
  • When a program must choose whether to execute a particular set of statements depending on the evaluation of a specific test condition, a selection or decision-making statement is required.
  • Python supports three main selection statements:
    • if statement
    • if-else statement
    • if-elif-else statement

The if Statement

  • The if statement is a primary decision-making statement used to control execution flow based on a condition.
  • It evaluates a conditional expression that compares data values, making an execution decision based strictly on the boolean result of that comparison:
    • If the conditional expression evaluates to True, the entire indented block of statements placed within the if construct is executed.
    • If the conditional expression evaluates to False, the statements within the if block are completely bypassed and not executed.
  • There is no numerical restriction on how many statements can be placed within the if block; programmers may include as many statements as necessary.
  • General syntax of the if statement:
    • if (conditional expression):
    • statement(s)

Selection statements flowcharts and IDLE implementation

Program Implementation: Eligibility to Vote

  • Demonstration program: Program 1: To check your eligibility to vote.
  • Environment context: IDLE Shell 3.9.73.9.7 (Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32).
  • Code implementation:
    • >>> age = 30
    • >>> if (age > 18):
    • ... print("You are eligible to cast your vote")
  • Execution result:
    • You are eligible to cast your vote
  • Logic evaluation:
    • The conditional statement evaluates to True because the variable assigned value (3030) satisfies the test condition of being strictly greater than 1818.
    • As a consequence of the condition evaluating to True, the indented call to print executes and displays the message to the console.

Indentation and Execution Rules

  • Subject: If statement execution criteria (Info Mail criteria):
    • A statement or set of statements indented under an if statement executes strictly and solely when the underlying test condition evaluates to True.
    • When the condition evaluates to False, program control bypasses the indented if block entirely and immediately proceeds to subsequent statements situated outside the if block.

The if-else Statement

  • The if-else statement provides a two-way branching mechanism to evaluate whether a given test statement or expression is True or False.
  • Branch execution pathways:
    • When the specified condition evaluates to True, the indented if block executes.
    • When the specified condition evaluates to False, execution branches immediately to the else block, executing the statements indented underneath else.
  • General syntax of the if-else statement:
    • if (conditional expression):
    • statement(s)
    • else:
    • statement(s)
  • Flowchart logic for equality comparison:
    • Condition: if (a == b)
    • Branch taken on True: executes statements corresponding to "Equal".
    • Branch taken on False: executes statements corresponding to "Not Equal".