Concepts / Control Flow: Making Decisions with if Statements

Control Flow: Making Decisions with if Statements

Programming languages, like natural languages, have vocabulary (words that must be spelled correctly) and grammar (rules for arranging those words into valid statements).

  • Programming

Why Exact Instructions Matter

Programming languages work like natural languages in two important ways: they have vocabulary and grammar. Vocabulary provides the words Python recognizes, while grammar determines how those words may be arranged into statements. An if statement is one way Python expresses a decision, but Python can understand it only when its words are spelled correctly and its structure follows the required rules.

Following the Decision Path

An if statement begins with the keyword if and a condition. Python evaluates that condition and uses the result to decide whether the statement or block associated with the if should be carried out. The condition is followed by a colon, which marks the structure that follows. Thinking of the statement as a decision point helps explain why its parts must appear in a particular order.

if statementcondition:conditionif blockcondition is truenext statementcondition is falsetruefalse
How does Python choose what code to execute when an if condition is true versus when it is false?
Worked example

Reading a Decision

Determine the structural parts of this if statement: if score > 0:

  1. 1
    Keyword

    if is the Python keyword that introduces the decision.

  2. 2
    Condition

    score > 0 is the condition placed after the keyword.

  3. 3
    Colon

    The colon at the end completes the required if-statement header.

Result

The statement has the required order: if keyword, condition, and colon.

The Grammar of an if Statement

A well-formed if statement follows a specific grammatical pattern. It uses the exact keyword if, places a condition after that keyword, and ends the if line with a colon. The instructions associated with the decision belong to the statement's block. In Python, the arrangement of these parts is not optional: changing the order or omitting a required part makes the statement invalid.

ifkeywordif blockassociated instructionsconditiondecision expression:required endingfollowed byfollowed byintroduces
How do the condition, colon, and following block fit together to form a valid if statement?
python

In this generated example, if is the keyword, score > 0 is the condition, and the colon ends the if line. The following instruction is shown as part of the associated block. The example illustrates the required structure; its purpose is to make the grammatical pattern visible.

Tracing Valid and Invalid Forms

Before
if score > 0
    print(score)

The if line is missing its required colon.

After
if score > 0:
    print(score)

The if line follows the required keyword, condition, and colon pattern.

if score > 0:colon includedif score > 0colon missingsyntax errorstatement refusedinvalid grammar
What structural difference causes one Python statement to be valid and another to produce a syntax error?

The difference between the two forms is only one character, but that character is required by Python's grammar. The form without the colon is invalid, so Python reports a syntax error and refuses to run it. This is an example of a grammatical error rather than a problem with the intended decision.

python

The source identifies a second spelling problem: My_name does not match my_name when the intended name is lowercase. Python is case-sensitive, so uppercase and lowercase characters are not interchangeable. Every character in a Python keyword or name must match exactly.

Mistakes Beginners Make

  • Common mistake

    Leaving out the colon at the end of an if line.

    What it looks like

    if score > 0

    Why it fails

    The source states that if statements must end with a colon. Omitting it violates Python's grammatical rules and causes a syntax error.

    Do this instead

    Write the condition followed by a colon: if score > 0:

  • Common mistake

    Changing the capitalization of a name.

    What it looks like
    Defining or using my_name as My_name
    Why it fails

    Python is case-sensitive. my_name, My_name, and other capitalization variants are not treated as the same spelling.

    Do this instead

    Use exactly the same characters and capitalization every time the name is used.

  • Common mistake

    Misspelling a Python keyword.

    What it looks like

    iff instead of if

    Why it fails

    Python has a fixed vocabulary of keywords, and a misspelled word is not recognized as the intended keyword.

    Do this instead

    Spell the keyword exactly as Python expects: if.

  • Common mistake

    Assuming Python will infer missing structure.

    What it looks like

    Putting the condition in the wrong place or omitting a required part.

    Why it fails

    Python follows explicit grammatical rules and cannot fill in ambiguous or malformed instructions.

    Do this instead

    Check the required order and punctuation of the statement before running it.

Best practice

When an if statement fails, inspect it in two passes. First check vocabulary: is if spelled correctly, and are names capitalized consistently? Then check grammar: is the condition in the expected place, and does the if line end with a colon? Separating spelling checks from structure checks makes the error easier to locate.

Check Your Understanding

EASY

Inspect each line and decide whether it follows the source's stated rules for an if statement. If it is invalid, identify the spelling or grammatical problem.

1. if score > 0:

  1. if score > 0
  2. if My_name:
  3. if my_name:
Hints
  • Check whether every if line ends with a colon.
  • Compare My_name and my_name character by character.
  • Remember that Python is case-sensitive.
  1. 1
    A valid if statement is not created by intention alone. It requires Python's exact vocabulary and grammar: the keyword must be spelled correctly, the condition must be placed after if, and the line must end with a colon. Python's case sensitivity also means that capitalization must remain consistent. When these rules are broken, Python reports a syntax error or treats differently spelled names as different names.

Key Takeaways

  • Python programming depends on both vocabulary and grammar.
  • Python is case-sensitive, so spelling and capitalization must match exactly.
  • An if statement uses the keyword if, a condition, and a colon in that order.
  • Missing required structure, such as the colon, causes a syntax error.
  • Careful checking of spelling and statement structure is a foundational programming skill.

Related concepts

More concepts starting with C