Concepts / Understand Boolean Expressions

Understand Boolean Expressions

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

Many programs need to choose between two alternatives. An if-else statement makes this choice by using a condition. If the condition is true, one block of code runs. If the condition is false, the other block runs. This is called alternative execution because the program follows one of two possible execution paths.

evaluateTrueFalsethen continuethen continueif-else statementconditionTrue or Falseif blockcondition is Truenext statementelse blockcondition is False
What happens next when the condition evaluates to true or false?

Evaluate Before Executing

Python does not choose a branch at random and does not execute both blocks. It first evaluates the condition. A condition is a Boolean expression, so its result is either True or False. A True result sends execution into the block under if. A False result sends execution into the block under else. After the selected block finishes, execution continues with the statement that follows the if-else statement.

evaluateTrueFalseinput or comparisonBoolean resultTrue or Falseif blockTrueelse blockFalse
How does a Boolean expression change an input or comparison into a true or false value?

The most useful tracing question is: What is the condition's Boolean result? Once that result is known, the executing branch is determined.

Writing the Two Blocks

An if-else statement has two code blocks. The first follows if and runs when the condition is true. The second follows else and runs when the condition is false.

The structure is ordered: write if, then the condition, then a colon. Put the first block at the required indentation. Next write else followed by a colon, and put the second block at its indentation. The colons and indentation show Python where the statement and each block begin.

followed byfollowed byopensfollowed byfollowed byopensifstarts the conditionif blockindented codeelsealternative branchelse blockindented codeconditionproduces True or False:ends the else line:ends the condition line
Which condition, colon, indentation level, and code block belong to each part of an if-else statement?

Tracing an Even-or-Odd Decision

Choosing a Branch for an Input

Trace an if-else statement that classifies an input as even or odd.

Start with the input: Use the given number as the input for the even-or-odd example.

Evaluate the condition: Determine whether the condition about the input is True or False before considering either block.

Choose one branch: If the condition is True, follow the if block. If the condition is False, skip the if block and follow the else block.

Continue afterward: Exactly one block has executed, so tracing continues with the statement after the if-else statement.

The condition determines the result: a True condition selects the if block, and a False condition selects the else block.

condition evaluatescondition evaluatesselectselectinput valueTruecondition resultif blockexecutesFalsecondition resultelse blockexecutes
How does a specific input map to either the if branch or the else branch?

The important tracing habit is to separate the stages. First identify the input. Next evaluate the condition. Finally follow only the branch that matches the result. Do not trace statements from the skipped branch as if they had executed.

Common Branching Mistakes

  • Leaving out the colon after the condition.

    The required if-else structure includes a colon after the condition.

    Fix: End the condition line with a colon.

  • Leaving out the colon after else.

    The else part also requires a colon before its block.

    Fix: Write else followed by a colon.

  • Using incorrect indentation for a block.

    Proper indentation identifies the code belonging to each block.

    Fix: Keep the statements in each block properly indented.

  • Assuming both blocks execute.

    An if-else statement executes exactly one of its two blocks.

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

  • Choosing a branch before evaluating the condition.

    Python evaluates the condition before selecting a branch.

    Fix: Record whether the condition is True or False before tracing execution.

Branch Selection Practice

What do you think happens?

A condition in an if-else statement evaluates to False. Which block executes?

  • Only the if block
  • Only the else block
  • Both blocks
  • Neither block
Reveal answer

Answer: Only the else block

Python evaluates the condition first. When the result is False, it skips the if block and executes the else block.

EASY

For an if-else statement whose condition evaluates to True, describe the execution path in order. Include the condition evaluation, the selected block, the skipped block, and what happens after the selected block finishes.

Hints
  • Begin by naming the Boolean result.
  • Remember that the block under else is skipped when the condition is True.
  • Finish by stating that execution continues after the if-else statement.
MEDIUM

Inspect the structure of an if-else statement and list the four syntax features you should check: the condition, the colon after the condition, the colon after else, and the indentation of both blocks.

Hints
  • Check the line beginning with if.
  • Check the line beginning with else.
  • Check the indentation beneath each branch.

Key Takeaways

  1. Alternative execution uses if-else to choose between two code blocks.
  2. Python evaluates the condition before selecting a branch.
  3. A True condition executes the if block, while a False condition executes the else block.
  4. Exactly one block executes for each if-else statement.
  5. Correct syntax requires a colon after the condition and after else, along with proper indentation for each block.

Key Takeaways

  • An if-else statement provides two alternative execution paths.
  • The condition is a Boolean expression that evaluates to True or False.
  • Trace the statement by evaluating the condition first and then following the matching branch.
  • The if block runs for True; the else block runs for False.
  • Colons and proper indentation are required parts of the structure.