Concepts / Write Simple if Statements

Write Simple if Statements

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

  • Programming

Two Possible Paths

Many programs need to respond differently depending on a condition. Alternative execution handles this situation by providing two possible blocks of code: one block for when a condition is true and another block for when it is false. An if-else statement selects exactly one of those blocks.

TrueFalseafter branchafter branchconditionTrue or Falseif blockcondition is Truenext statementelse blockcondition is False
What happens next when a condition is true versus when it is false, and which of the two code blocks executes?

A First Execution Trace

number = 7 if number % 2 == 0: result = "even" else: result = "odd" print(result)

evaluateFalseFalse branchthennumber = 7input valuenumber % 2 == 0Falseif blockskippedprint(result)oddresult = oddelse block
Given the input 7 and the condition number % 2 == 0, which branch does program control follow and where does execution rejoin afterward?

How Python Chooses

When Python encounters an if-else statement, it evaluates the condition first. A condition is a boolean expression, so its result is either True or False. If the result is True, Python executes the indented code under if and skips the else block. If the result is False, Python skips the if block and executes the indented code under else. Exactly one block runs: never both and never neither.

TrueTrueFalseFalseafter blockafter blockboolean conditionTrue or Falseindented if coderuns when Trueindented else coderuns when Falsefollowing statementif codeskipped when Falseelse codeskipped when True
How does the condition's truth value determine whether an indented block runs or is skipped?
followed byfollowed byblock containsifkeywordprint("pass")indented code blockscore >= 60condition:starts block
How do the condition, colon, indentation, and code block fit together in a correctly written if statement?

Writing the Structure

python

The condition follows if, and a colon must follow the condition. The statements controlled by if must be indented. The else line also requires a colon, and the statements controlled by else must be indented. The two indented regions are the two alternative blocks.

Checking an Unexpected Result

Debugging an if statement begins at the condition. Compare the actual input with the condition and decide whether the result is True or False. Then check whether the program follows the corresponding branch. If the branch is correct but the result still seems wrong, inspect the statements inside that indented block.

  • Leaving out the colon after the condition.

    The if statement requires a colon after its condition.

    Fix: Write if score >= 60:

  • Leaving out the colon after else.

    The else branch also requires a colon before its indented block.

    Fix: Write else:

  • Failing to indent the code controlled by the condition.

    The code that belongs to the if block must be indented.

    Fix: Indent the statement under if.

  • Assuming both branches execute.

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

    Fix: Evaluate the condition and follow only the matching branch.

expected pathactual pathtemperature < 10expected Truecoldtemperature < 10actual Falsenot cold
At which condition or branch does actual execution diverge from the expected path, and what input caused the divergence?

Branch Prediction Practice

What do you think happens?

Which message is printed when points has the value 42?

  • high
  • low
Reveal answer

Answer: high

The condition points >= 40 is True for 42. Python executes the if block and skips the else block.

python
EASY

Trace the code before running it. Write down the condition, decide whether it is True or False, identify the branch that executes, and state what the program prints.

Hints
  • Start with the value of points.
  • Evaluate points >= 40.
  • Only one indented block executes.

Key Takeaways

  • An if-else statement provides two alternative execution paths.
  • Python evaluates the condition first and uses its True or False result to select a branch.
  • Exactly one block executes in an if-else statement; the other block is skipped.
  • A correct statement needs a colon after the if condition and after else, plus proper indentation for each block.
  • To debug unexpected behavior, evaluate the condition with the actual input and trace the selected branch.