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).
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.
Reading a Decision
Determine the structural parts of this if statement: if score > 0:
Keyword: if is the Python keyword that introduces the decision.
Condition: score > 0 is the condition placed after the keyword.
Colon: The colon at the end completes the required if-statement header.
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.
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
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.
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
Leaving out the colon at the end of an if line.
The source states that if statements must end with a colon. Omitting it violates Python's grammatical rules and causes a syntax error.
Fix:
Write the condition followed by a colon: if score > 0:Changing the capitalization of a name.
Python is case-sensitive. my_name, My_name, and other capitalization variants are not treated as the same spelling.
Fix:
Use exactly the same characters and capitalization every time the name is used.Misspelling a Python keyword.
Python has a fixed vocabulary of keywords, and a misspelled word is not recognized as the intended keyword.
Fix:
Spell the keyword exactly as Python expects: if.Assuming Python will infer missing structure.
Python follows explicit grammatical rules and cannot fill in ambiguous or malformed instructions.
Fix:
Check the required order and punctuation of the statement before running it.
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
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: 2. if score > 0 3. if My_name: 4. 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.
- 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.