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.

01

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.
0 or 1 weak itemsContinue with this module.
2 weak itemsRevisit output in Module 3.
3 or more weak itemsRevisit factor of safety in Orientation, Module 6.
02

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.

if n >= 1.5: ... (runs when True)== tests equality, = assignsand, or, not combine conditions

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.

The skill works when: you write a clear comparison, indent the block consistently, and use == for tests.
The skill breaks down when: = is used where == is meant, or indentation is inconsistent.
The concept. A condition evaluates to True or False; the if statement runs one branch or another accordingly.
03

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.

ComparisonReads asExample result
==equal to3 == 3 is True
>=at least2.5 >= 1.5 is True
!=not equal3 != 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.

04

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)
  1. ProblemDecide safe or redesign from the factor of safety.
  2. Given / findstrength 250, stress 100, required n = 1.5. Find the verdict.
  3. Modeln = strength / stress; branch on n >= 1.5.
  4. CodeThe indented block under if runs only when the condition is true.
  5. Solven = 250 / 100 = 2.5, and 2.5 >= 1.5 is True, so it prints safe (n = 2.5).
  6. Check2.5 exceeds 1.5, so the safe branch is correct.
  7. ConclusionAn engineering adequacy check is an if statement on a computed margin.
Result. safe (n = 2.5).
05

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
  1. ProblemDecide whether 85 degrees is at or above boiling.
  2. Given / findT = 85, threshold 100. Find the classification.
  3. ModelEvaluate T >= 100, then branch.
  4. Solve85 >= 100 is False, so the else branch prints below boiling.
  5. Check85 is less than 100, so "below boiling" is right.
  6. ConclusionThe comparison is a Boolean you can print or branch on; the if picks the matching message.
Result. False, then "below boiling".
06

Misconceptions and diagnostics

MistakeSymptomDiagnostic questionCorrection
Using = for a testA syntax error in the if"Test or assign?"Use == to compare, = to assign.
Inconsistent indentationAn indentation error"Are the block lines aligned?"Indent every block line the same amount.
Wrong branch orderAn elif never reached"Is the broad condition first?"Order conditions from specific to general.
Comparing floats exactlyAn equality that never holds"Are these floats?"Compare floats with a tolerance, not ==.
07

Practice ladder

Level 1 · Direct skill

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.

Level 2 · Mixed concept

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").

Level 3 · Independent problem

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.

Transfer task | Real engineering

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.

08

Working with AI, and proving it yourself

Use AI as a tutor, not a black box

"Explain the difference between = and == in an if statement."
"Give me five conditions; I will predict True or False, then run them."
"Write my safety check." Draft the comparison yourself.
"Why won't my if run?" Check the condition and indentation yourself.

Portfolio task

Turn one engineering acceptance rule into an if/elif/else that prints a clear verdict for a given input.

Must include: a comparison, an elif or else, correct indentation, and a labeled result.
09

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.

TodayFinish this quiz and Levels 1 and 2 of the ladder.
+1 dayRe-write the factor-of-safety check from memory.
+3 daysTurn one rule into an if/elif/else.
+7 daysMove on to loops in Module 5.
+30 daysReach for a conditional whenever a decision appears.
10

Textbook mapping

This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.

Topic in this moduleWhere to read more
If-then-elseFangohr, Section 6.2, If-then-else
Relational operatorsFangohr, Section 6.5, Relational operators
Control flow basicsFangohr, 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.