Concepts / Understand Comparison Operators

Understand Comparison Operators

Alternative execution uses if-else to run one of two code blocks based on whether a condition is true or false.

  • Programming

One Decision, Two Paths

An if-else statement gives a program two possible execution paths. Python evaluates a condition first. If the condition is True, Python runs the if block. If the condition is False, Python runs the else block. This is called alternative execution because the program chooses one alternative instead of running both blocks.

Exactly one block runs in an if-else statement: the if block when the condition is True, or the else block when the condition is False.

Tracing the Execution Path

To trace an if-else statement, do not begin by reading both blocks as if both will execute. Start with the condition. Determine whether it evaluates to True or False, then follow only the matching branch. A True result leads into the if block and skips the else block. A False result skips the if block and leads into the else block.

evaluateTrueFalsefinish branchfinish branchif-else statementconditionTrue or Falseif blockcondition is Truenext statementelse blockcondition is False
What happens next when the condition evaluates to True or False?

Condition Results

The condition in an if-else statement is a boolean expression. Its result is either True or False. Comparison operators are useful because they produce this kind of decision result from values being compared. For tracing purposes, the important question is not which branch appears first, but which boolean result the condition produces.

inputinputcondition holdscondition does not holdfirst valueTruecomparisonconditionFalsesecond value
How does a comparison condition transform values into the True or False result used by if-else?

Writing the Structure

An if-else statement has two code blocks. Begin with if, write the condition, and place a colon after the condition. The statements belonging to the if block must be properly indented. Then write else followed by a colon. The statements belonging to the else block must also be properly indented.

  • Write if followed by the condition.
  • Place a colon after the condition.
  • Indent the first block under if.
  • Write else followed by a colon.
  • Indent the second block under else.

The colon and indentation are part of the required if-else structure. They show Python where each block begins and which statements belong to it.

An Even-or-Odd Trace

Consider the even-or-odd example described in the source material. The first step is to evaluate the condition that asks whether the input is even. If that condition is True, execution follows the if block and identifies the input as even. If the condition is False, execution skips the if block and follows the else block, which identifies the input as odd.

evaluateTrueFalseinput numbereven conditionTrue or Falseeven branchcondition is Trueodd branchcondition is False
Given an input, how does the comparison result determine whether the if branch or the else branch executes?

Tracing One Input

Determine which branch executes when the even-or-odd condition evaluates to False.

Evaluate: The condition produces the boolean result False.

Select: Because the result is False, Python skips the if block.

Execute: Python executes the else block.

The else branch executes, and the if branch does not.

Mistakes Beginners Make

  • Expecting both blocks to execute

    An if-else statement selects exactly one block based on the condition result.

    Fix: Evaluate the condition first, then follow only the matching branch.

  • Choosing a branch before evaluating the condition

    Python evaluates the condition before choosing a path.

    Fix: Write down or identify the condition result as True or False before tracing execution.

  • Leaving out a required colon

    The if-else syntax requires a colon after the condition and after else.

    Fix: Check both required colon positions.

  • Using incorrect indentation

    The blocks must be properly indented so their statements are grouped correctly.

    Fix: Indent the statements under if and separately indent the statements under else.

Branch Prediction Practice

EASY

A condition in an if-else statement evaluates to True. Which block executes? Now repeat the trace with the condition evaluating to False. For each case, name the block that executes and the block that is skipped.

Hints
  • Start by recording the condition result.
  • True selects the if block.
  • False selects the else block.

What do you think happens?

An if-else condition evaluates to False. Which branch will execute?

  • The if block
  • The else block
  • Both blocks
  • Neither block
Reveal answer

Answer: The else block

Python skips the if block when its condition is False and executes the code under else. Exactly one block executes.

Key Takeaways

  1. An if-else statement provides two alternative execution paths.
  2. Python evaluates the condition before entering either block.
  3. A True condition selects the if block, while a False condition selects the else block.
  4. Exactly one block executes; the other block is skipped.
  5. Correct syntax requires a colon after the condition and after else, plus proper indentation for both blocks.

Key Takeaways

  • Comparison conditions produce a boolean result that guides control flow.
  • Trace an if-else statement by evaluating the condition first.
  • True selects the if block; False selects the else block.
  • An if-else statement executes exactly one of its two blocks.
  • Colons and indentation are required parts of the Python if-else structure.