University of Toronto · FACULTY OF COMPUTER SCIENCE

CSC108H1 Chap.6 If Statements and Choosing Docstring Examples

- one subject, every graph, every model, every mark
6 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 6 of 10 · CSC108H1

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.

In this chapter

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

Q [3 marks]. AskSia-authored practice. A fee function returns 5.0 if the weight is at most 1.0, otherwise 9.5 if it is at most 5.0, and otherwise 15.0, with both limits tested as at most. Give the results for weights 3.2 and 5.0 and say which branch runs for each. The marks shown are an AskSia study allocation, not an official University marking scheme.
Figure 6.1.Figure 6.1. Conditions are tested from the top down. For a weight of 3.2 the first test is False, the second is True, so the call returns 9.5 and the else is never considered; dashed paths are the branches not taken.weight <= 1.0 ?weight <= 5.0 ?elsereturn 5.0return 9.5return 15.0True for 3.2
Figure 6.1. Conditions are tested from the top down. For a weight of 3.2 the first test is False, the second is True, so the call returns 9.5 and the else is never considered; dashed paths are the branches not taken.
  • 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.
Both calls return 9.5 from the middle branch. The second call sits exactly on the 5.0 edge, where an at-most test includes the limit; a body written with a strict less-than test would send it to the else and return 15.0, which is why boundary values make strong docstring examples.
Sia tip — For every limit in an if chain, write one example exactly on the limit; that is the input where choosing strictly less than instead of at most changes the result.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 21 of your University of Toronto subjects - and 1,000+ Bibles across every Australian university.
Sia - your CSC108H1 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works