CSC108H1 Chap.6 If Statements and Choosing Docstring Examples
If Statements and Choosing Docstring Examples
One structure, one branch
An if statement starts with one if, may continue with any number of elif clauses and may end with one else.
Python tests the conditions from the top and runs only the first branch whose condition is True, then continues after the whole structure.
Examples that test behaviour
Week 3 also teaches how to choose docstring examples: one example for each behaviour of the function, with extra examples on the boundaries between branches.
Debugging tasks in Assignment 1 asked for one example a faulty body passes and one it fails, before the repair.
How the topic is assessed
Worksheet 9 asks for function bodies that choose among several results using thresholds and one special case, and Worksheet 11 asks which pair of example calls documents a function best and why.
Assignment 1 used both skills: one task classified an input into categories by limits, and another asked for docstring examples that make a function return False and True in different ways.
What this chapter covers
- 01
The shape of if, elif and else
- 02
How Python chooses exactly one branch
- 03
Separate if statements versus elif
- 04
Ordering conditions from narrow to broad
- 05
Choosing docstring examples by behaviour
- 06
Boundary examples and named constants
Worked example · free
Trace a fee chain at a boundary
- 1For 3.2 the first test is False and the second is True, so the call returns 9.5.
- 1For 5.0 the first test is False and 5.0 is at most 5.0, so it also returns 9.5.
- 1The else branch runs only for weights above 5.0.
Key terms
- Conditional Statement
- An if structure that runs a branch only when its condition is True.
- Elif Clause
- A further condition in an if structure, tested only if every condition above it was False.
- Boundary Example
- A docstring example placed exactly on the limit between two branches.
- Constant
- A name written in capitals that holds a value the program does not change.
If Statements and Choosing Docstring Examples FAQ
What is the difference between elif and a second if?
An elif belongs to the same structure and is skipped once an earlier branch has run. A separate if begins a new structure and is always tested, so it can run as well if an earlier branch changed the variable it checks.
Does an if statement always need an else?
No. An if on its own runs its statements when the condition is True and does nothing otherwise. An else is needed only when the False case has its own work to do, and a function can return inside an if and continue after it.
How many docstring examples should a function have?
Enough to show each behaviour once. The Week 3 worksheet's best pair shows two different behaviours, one True and one False, and functions with more branches need one example per branch plus examples on the boundaries.
Why should limits be stored as constants?
Naming a limit in capitals keeps it in one place, so a change happens once rather than in every condition. Assignment 2 required a provided constant to be used for exactly this reason: the function had to keep working if the constant's value changed.
Can a function return from inside an if without an else?
Yes. A return ends the function at once, so the code after the if runs only when the condition was False. Many course examples use this shape, returning in the if branch and leaving the final return on its own line after the structure.
Exam move
For each if-based worksheet function, list its behaviours and boundaries before writing examples, then write one example per behaviour and one per boundary. Rewrite one function with separate ifs instead of elif and find an input that gives a different answer. Finish by replacing literal limits with named constants and checking that every example still passes.
Include one function whose limits are named constants and change a constant to confirm the examples still describe it.
Working through If Statements and Choosing Docstring Examples in CSC108H1? Sia is AskSia’s AI Computer Science tutor — ask any CSC108H1 If Statements and Choosing Docstring Examples question and get a clear, step-by-step explanation grounded in how CSC108H1 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.