Concepts / Boolean Values and Logic

Boolean Values and Logic

An if statement evaluates a condition and executes a block of code only if that condition is true.

  • Programming

A Decision in Program Execution

An if statement gives a program a conditional decision. Python evaluates a condition and executes the associated block of code only when that condition is true. When the condition is false, Python does not execute that if-block. An optional else-block can provide alternative code for the false case.

What do you think happens?

What happens when the condition checked by an if statement is false?

  • The if-block runs anyway

  • The if-block is skipped

  • The colon is ignored

  • Every statement in the file runs

Reveal answer
Answer:

The if-block is skipped

An if statement executes its block only when its condition is true. If an else-block is present, that alternative block can run when the condition is false.

Following the Two Paths

ConditionevaluatedIf-blockcondition trueFollowing codeElse-blockcondition falsetruefalsethenthen
What happens to program execution when the condition is true versus false?

The condition is the decision point. A true condition sends execution into the if-block. A false condition skips the if-block. If an else-block exists, the false path enters that alternative block instead. After the selected block finishes, execution continues with code that follows the conditional structure.

python

The Shape of an If Statement

An if statement has a condition followed by a colon. The colon signals to Python that a code block follows. Statements belonging to that block are indented. The else clause is optional and, when used, introduces an alternative block for the false condition.

ifstarts the conditionalif-blockindented statementsconditionvalue to evaluateelse-blockindented statements:block followselse:optional alternativeoptional
Where do the condition, colon, indented block, and optional else clause appear?
Code walkthrough
if is_ready:
    start_task()
else:
    wait_for_ready()

continue_work()
  1. 1
    Python evaluates is_ready.
  2. 2
    If the condition is true, Python executes start_task.
  3. 3
    If the condition is false, Python executes wait_for_ready instead.
  4. 4
    After the selected block, Python reaches continue_work.
  • The if keyword begins the conditional statement.
  • is_ready is the condition being evaluated.
  • The colon signals that the if-block follows.
  • The indented start_task statement belongs to the if-block.
  • The else clause introduces the alternative block.
  • The indented wait_for_ready statement belongs to the else-block.
  • continue_work is not indented under either block, so it follows the conditional structure.

Indentation Sets Block Membership

if condition:starts the blockinside_block()indentedalso_inside()same indentationoutside_block()not indentedblock beginssame blockindentation ends
Which statements belong to the if-block, and how does indentation determine when the block ends?

Python uses indentation to define which statements belong to a code block. Statements with the block's indentation are part of that block. When the indentation returns to the surrounding level, the block ends. Python uses indentation for this purpose instead of curly braces or keywords.

Before
if condition:
    first_action()
    second_action()
third_action()

first_action and second_action are indented under the if statement; third_action is outside the block.

After
if condition:
    first_action()
third_action()

Only first_action is indented under the if statement; third_action remains outside the block.

A Complete Conditional Example

Worked example

Choosing Between Two Actions

Trace which action is selected when a condition is true or false.

  1. 1
    Check the condition

    The if statement evaluates condition.

  2. 2
    Follow the true path

    When condition is true, Python executes choose_first_action because that statement is inside the indented if-block.

  3. 3
    Follow the false path

    When condition is false, Python skips the if-block and executes choose_second_action in the optional else-block.

  4. 4
    Continue

    The conditional has selected one block. Code after the structure can then be reached.

Result

The if-block handles the true condition, and the else-block handles the false condition.

python

Mistakes with Blocks

  • Common mistake

    Leaving out the colon after the condition

    What it looks like
    if condition
        action()
    Why it fails

    The colon signals to Python that a code block follows.

    Do this instead

    Write the condition followed by a colon: if condition:

  • Common mistake

    Failing to indent the if-block

    What it looks like
    if condition:
    action()
    Why it fails

    Indentation defines which statements belong to the if-block.

    Do this instead

    Indent the statement that belongs to the block.

  • Common mistake

    Indenting a statement that should follow the conditional structure

    What it looks like
    if condition:
        action()
        following_action()
    Why it fails

    The indentation makes following_action part of the if-block.

    Do this instead

    Place following_action at the surrounding indentation level when it should not belong to the if-block.

  • Common mistake

    Treating else as required

    Why it fails

    The else clause is optional.

    Do this instead

    Include else only when the false condition needs an alternative block.

Practice the Decision

EASY

Write a Python if statement that checks condition, runs first_action when the condition is true, and runs second_action when the condition is false. Include the colon and indent both blocks correctly.

Hints
  • Begin with if followed by the condition and a colon.
  • Indent the statement belonging to the if-block.
  • Put else: at the surrounding indentation level.
  • Indent the statement belonging to the else-block.
EASY

Look at the following structure and identify which statements belong to the if-block: if condition: followed by an indented first_action, an indented second_action, and a non-indented following_action.

Hints
  • Compare the indentation of each statement with the indentation of the other block statements.
  • The block ends when indentation returns to the surrounding level.

Key Takeaways

  1. 1
    An if statement evaluates a condition and runs its block only when the condition is true.
  2. 2
    The else clause is optional and supplies an alternative block for a false condition.
  3. 3
    A colon at the end of the if statement signals that a code block follows.
  4. 4
    Indentation determines which statements belong to the if-block or else-block.
  5. 5
    When indentation returns to the surrounding level, the conditional block ends.

At a glance

  • An if statement makes execution conditional on whether a condition is true.
  • The optional else-block runs as the alternative when the condition is false.
  • The colon marks the beginning of the following code block.
  • Indentation, rather than curly braces or keywords, identifies statements belonging to a Python block.

Related concepts

More concepts starting with B