Concepts / Basic if Statements

Basic if Statements

An if-else statement creates two mutually exclusive branches: one executes if the condition is true, the other if it is false.

  • Programming

Choosing a Path

Programs often need to respond differently depending on a condition. An if-else statement provides exactly two possible paths: one path runs when the condition is true, and the other runs when the condition is false. These paths are called branches, and the structure is known as alternative execution.

truefalsethenthenconditiontrue branchfollowing codefalse branch
How does the condition determine which of the two execution paths runs, and why can only one branch execute?

Tracing the Decision

To trace an if-else statement, do not read both branches as though both will run. First evaluate the condition. If it is true, follow the if branch and skip the else branch. If it is false, skip the if branch and follow the else branch. After the selected branch finishes, execution continues with the code after the complete if-else statement.

nexttruefalseskip elseskip ifstartevaluate conditionif blockcode afterwardelse block
What happens next as the program evaluates the condition, enters one branch, skips the other, and reaches the code afterward?

What do you think happens?

Suppose the condition is true. Which messages are produced: the message in the if branch, the message in the else branch, or both?

  • The if-branch message only
  • The else-branch message only
  • Both messages
Reveal answer

Answer: The if-branch message only.

A true condition selects the if branch. The else branch is skipped, so exactly one branch executes.

Writing the Structure

An if-else statement has an if line, a condition, and a colon. The statements belonging to that branch are placed in an indented block. The else line also requires a colon, and its statements form a second consistently indented block. The two blocks represent the alternative branches.

python
ifstarts the true branchconditionvalue to evaluate:ends the if linetrue blockconsistently indentedelsestarts the false branch:ends the else linefalse blockconsistently indented
Where does each part of the intended control flow appear in the syntax?

Reading a Complete Example

Selecting One Message

Trace the two possible outcomes of an if-else statement when the condition is true and when it is false.

Evaluate: The program evaluates the condition before entering either branch.

True result: When the condition is true, the true branch runs and the false branch is skipped.

False result: When the condition is false, the true branch is skipped and the false branch runs.

Continue: After the selected branch, execution reaches the code that follows the if-else block.

Exactly one branch runs for each condition result.

if condition: print("accepted") else: print("rejected") print("finished")

selectsselectsthenthentruecondition resultacceptedbranch outputfinishedoutput after the blockfalsecondition resultrejectedbranch output
How does a true or false condition change which statement runs and what output the program produces?

Mistakes That Break Branching

  • Leaving out the colon after if or else.

    The syntax requires a colon after the if line and after the else line.

    Fix: Write if condition: and else:.

  • Using inconsistent indentation inside a branch.

    Statements in a block must be indented consistently.

    Fix: Give all statements belonging to the same branch the same indentation.

  • Treating if and else as two independent paths that both run.

    The branches are mutually exclusive. The condition selects exactly one of them.

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

  • Tracing the branch without checking the condition.

    The condition determines which branch executes.

    Fix: Write down the condition result before following the control flow.

opensfollowed byopensreplacesdisruptsif condition:required colonmissing colonsyntax breaktrue blockconsistent indentationinconsistentindentationblock breakelse:required colonfalse blockconsistent indentation
Where does the intended control flow break when a colon, indentation, or branch choice is incorrect?

Practice the Trace

EASY

Consider this if-else structure: if condition: print("first path") else: print("second path") print("afterward") Describe the output when the condition is true, then describe the output when the condition is false. Explain which branch is skipped in each case.

Hints
  • Evaluate the condition before choosing a branch.
  • Exactly one branch runs.
  • The afterward statement is outside the if-else block, so it follows the selected branch.

When debugging an if-else statement, trace it in three passes: identify the condition result, mark the branch that executes, and then continue to the code after the block. Also check both colons and the indentation of every statement in each branch.

Key Takeaways

  1. An if-else statement creates two mutually exclusive branches.
  2. A true condition selects the if branch; a false condition selects the else branch.
  3. Exactly one branch always executes because the condition is either true or false.
  4. Tracing means evaluating the condition first, following only the selected branch, and then continuing afterward.
  5. The syntax requires colons after if and else, with consistently indented branch blocks.

Key Takeaways

  • If-else statements provide alternative execution through two mutually exclusive branches.
  • The condition determines whether the if branch or the else branch runs.
  • Tracing requires evaluating the condition first and skipping the branch that does not match.
  • Both branch lines require colons, and their blocks must be indented consistently.
  • After one branch finishes, execution continues with the code after the if-else block.