Concepts / Understand Comparison Operators

Understand Comparison Operators

Comparison operators are useful because they produce this kind of decision result from values being compared.

  • 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.

Key point

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.

if-else statementconditionTrue or Falseif blockcondition is Truenext statementelse blockcondition is FalseevaluateTrueFalsefinish branchfinish branch
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.

first valueTruecomparisonconditionFalsesecond valueinputinputcondition holdscondition does not hold
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.
Key point

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.

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

Tracing One Input

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

  1. 1
    Evaluate

    The condition produces the boolean result False.

  2. 2
    Select

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

  3. 3
    Execute

    Python executes the else block.

Result

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

Mistakes Beginners Make

  • Common mistake

    Expecting both blocks to execute

    What it looks like

    Reading the if block and the else block as two actions that will both happen.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Choosing a branch before evaluating the condition

    What it looks like

    Following the if block simply because it appears first.

    Why it fails

    Python evaluates the condition before choosing a path.

    Do this instead

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

  • Common mistake

    Leaving out a required colon

    What it looks like

    Writing the condition or else line without its colon.

    Why it fails

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

    Do this instead

    Check both required colon positions.

  • Common mistake

    Using incorrect indentation

    What it looks like

    Placing a statement outside the block it is meant to belong to.

    Why it fails

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

    Do this instead

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

At a glance

  • 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.

Related concepts

More concepts starting with U