Programming and Computation · Module 4 of 10
Making Decisions: Booleans and Conditionals
Engineering code has to decide: is the design safe, is the value in range, did the test pass? This module covers Boolean values, comparisons, and the if, elif, and else statements that branch on them.
Readiness check
This module is about decisions in code. Tick only what you can do closed-notes.
- Recall true and false as ideas.
- Compare two numbers with greater-than or equal-to.
- Recall the factor of safety from orientation.
- Use variables and printing from earlier modules.
- Recall that a design must meet a threshold.
The core idea
A comparison such as n >= 1.5 evaluates to a Boolean, True or False. An if statement runs a block only when its condition is True, with optional elif and else branches. The block is defined by indentation, and conditions combine with and, or, and not.
Programs become useful when they decide. A comparison operator turns two values into a Boolean: n >= 1.5 is True when n is at least 1.5 and False otherwise, and the comparisons are == (equal), != (not equal), <, >, <=, and >=. An if statement uses such a Boolean to choose what runs: the indented block under if executes only when the condition is True. Add elif (else-if) branches to test further conditions in order, and a final else to catch everything left. Two features trip up beginners. First, the block is defined by indentation, not by braces: the lines indented under the if are the ones it controls, so consistent indentation is part of the syntax, not just style. Second, a single = assigns a value while a double == tests equality, and confusing them is a classic bug. Finally, conditions combine with and, or, and not, so a design check can require several things at once, like "margin is enough and mass is within budget." This branching is how a stress check, a range test, or a pass/fail becomes code.
== for tests.= is used where == is meant, or indentation is inconsistent.The skills, taught in order
Five skills let code make correct decisions.
4.1 Booleans and comparisons
A comparison returns True or False. The operators are ==, !=, <, >, <=, and >=. These are the raw material of every decision.
4.2 if, elif, else
if runs its block when the condition is true; elif tests another condition if the first was false; else catches the rest. Only the first true branch runs.
4.3 Indentation defines blocks
Python uses indentation, not braces, to show which lines belong to the if. Indent consistently, usually four spaces; inconsistent indentation is a syntax error, not just untidy.
| Comparison | Reads as | Example result |
|---|---|---|
== | equal to | 3 == 3 is True |
>= | at least | 2.5 >= 1.5 is True |
!= | not equal | 3 != 4 is True |
Comparisons produce Booleans; the if statement acts on them.
4.4 Combining conditions
Join conditions with and (both true), or (either true), and not (reverse). A safe design might need margin >= 1.5 and mass <= 250.
4.5 Equality versus assignment
A single = assigns a value; a double == tests equality. Using = inside an if is a common and confusing error, so read your conditions carefully.
Engineering connection: a factor-of-safety check, a temperature limit, or a pass/fail acceptance test is exactly an if on a comparison, the code form of an engineering judgement.
Worked example 1: a factor-of-safety check
A part has a strength of 250 MPa and a working stress of 100 MPa. Decide whether the factor of safety meets a required 1.5.
strength = 250
stress = 100
n = strength / stress
if n >= 1.5:
print(f"safe (n = {n})")
else:
print(f"redesign (n = {n})")
Output:
safe (n = 2.5)
- ProblemDecide safe or redesign from the factor of safety.
- Given / findstrength 250, stress 100, required n = 1.5. Find the verdict.
- Model
n = strength / stress; branch onn >= 1.5. - CodeThe indented block under
ifruns only when the condition is true. - Solven = 250 / 100 = 2.5, and 2.5 >= 1.5 is True, so it prints safe (n = 2.5).
- Check2.5 exceeds 1.5, so the safe branch is correct.
- ConclusionAn engineering adequacy check is an if statement on a computed margin.
Worked example 2: a temperature limit
Classify a measured temperature of 85 degrees Celsius against the boiling point of water.
T = 85
print(T >= 100) # the Boolean itself
if T >= 100:
print("at or above boiling")
else:
print("below boiling")
Output:
False
below boiling
- ProblemDecide whether 85 degrees is at or above boiling.
- Given / findT = 85, threshold 100. Find the classification.
- ModelEvaluate
T >= 100, then branch. - Solve85 >= 100 is False, so the else branch prints below boiling.
- Check85 is less than 100, so "below boiling" is right.
- ConclusionThe comparison is a Boolean you can print or branch on; the if picks the matching message.
Misconceptions and diagnostics
| Mistake | Symptom | Diagnostic question | Correction |
|---|---|---|---|
| Using = for a test | A syntax error in the if | "Test or assign?" | Use == to compare, = to assign. |
| Inconsistent indentation | An indentation error | "Are the block lines aligned?" | Indent every block line the same amount. |
| Wrong branch order | An elif never reached | "Is the broad condition first?" | Order conditions from specific to general. |
| Comparing floats exactly | An equality that never holds | "Are these floats?" | Compare floats with a tolerance, not ==. |
Practice ladder
Predict 7 % 2 == 0 and say what it tests.
Show answer
False; it tests whether 7 is even (a remainder of 0). 7 is odd.
Write an if/elif/else that prints "low", "ok", or "high" for a pressure below 90, between 90 and 110, or above 110.
Show answer
if p < 90: print("low"), elif p <= 110: print("ok"), else: print("high").
Write a check that a design is acceptable only if the margin is at least 1.5 and the mass is at most 250.
Show answer
if margin >= 1.5 and mass <= 250: print("accept") else print("reject"). Both conditions must hold.
Write a pass/fail acceptance test for a measured deflection against a limit, printing the verdict and the value.
What good work looks like
For example if delta <= limit: print(f"pass ({delta:.2f} mm)") else a fail message. A good answer uses a clear comparison, correct indentation, and a labeled, formatted value.
Working with AI, and proving it yourself
Use AI as a tutor, not a black box
Portfolio task
Turn one engineering acceptance rule into an if/elif/else that prints a clear verdict for a given input.
elif or else, correct indentation, and a labeled result.Retrieval and spaced review
Closed notes. Answer out loud, then reveal.
1. What does a comparison return?
A Boolean, True or False.
2. What does indentation define?
Which lines belong to the if block.
3. Difference between = and ==?
= assigns; == tests equality.
4. What do and, or, not do?
Combine conditions: both, either, and reverse.
5. When does an else run?
When no preceding if or elif condition was true.
Textbook mapping
This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.
| Topic in this module | Where to read more |
|---|---|
| If-then-else | Fangohr, Section 6.2, If-then-else |
| Relational operators | Fangohr, Section 6.5, Relational operators |
| Control flow basics | Fangohr, Section 6.1, Basics |
Chapter numbers refer to Fangohr's Introduction to Python for Computational Science and Engineering. Any recent version is equivalent for study.