EE1021 Programming for Engineers · Lecture 2 · 2026–2027
if / elif / else and correct indentationwhile loops that make progress toward a stopping conditionrange() and strings with for loopsfor or while loop, and use break to stop reading input when the user enters a special stop value (such as -1)Predict the output before running the code.
Check: True, 1, and "ens".
Bridge to today: a Boolean result such as x < y can decide which block runs.
Rule: 50% or more → OK. 20% to 49% → Low. Below 20% → Charge now.
battery = 35 # charge in percent (0 to 100)
if battery < 0 or battery > 100:
print("Invalid reading")
elif battery >= 50:
print("OK")
elif battery >= 20:
print("Low")
else:
print("Charge now")Python tests branches from top to bottom and stops after the first true condition. Here battery >= 50 is false, battery >= 20 is true, so the program prints Low.
Predict: what does the program print if battery = 10?
Check: Charge now. No condition above else is true.
Python uses indentation as part of its syntax. Use four spaces for each level.
temperature = int(input("Temperature: "))
if temperature < -50:
print("Invalid reading")
elif temperature > 75:
print("High temperature")
else:
print("Temperature is in range")
print("Check finished")Only the indented statement under the selected branch runs. The final print() is aligned with if, so it runs after whichever branch is selected.
Use the battery program to predict one output for each test case.
battery |
Why test it? |
|---|---|
| -1 | just below the valid range |
| 101 | just above the valid range |
| 50 | exact OK limit |
| 49 | just below the OK limit |
| 20 | exact Low limit |
| 19 | just below the Low limit |
Verify: trace the branches from top to bottom before running the program.
Check: Invalid reading, Invalid reading, OK, Low, Low, Charge now.
while: Repeat While a Condition Is TrueA reliable while loop has three parts:
while Loop| Before test | n < 8 |
Printed | Updated n |
|---|---|---|---|
| 0 | True |
0 | 2 |
| 2 | True |
2 | 4 |
| 4 | True |
4 | 6 |
| 6 | True |
6 | 8 |
| 8 | False |
— | — |
The loop stops when the condition becomes False.
for: Iterate Over a Sequencerange(start, stop, step)The stop value is excluded.
| Expression | Generated values |
|---|---|
range(5) |
0, 1, 2, 3, 4 |
range(2, 6) |
2, 3, 4, 5 |
range(2, 11, 2) |
2, 4, 6, 8, 10 |
range(5, 0, -1) |
5, 4, 3, 2, 1 |
Defaults: start = 0, step = 1.
break: Stop on a SentinelUse a sentinel value to signal that input is finished.
total = 0.0
while True:
reading = float(input("Reading (-999 to stop): "))
if reading == -999:
break
total += reading
print("Total:", total)break exits the innermost loop immediately. Statements after the loop still run.
for or whilePrefer for when:
range()Prefer while when:
Trace: list the generated values, identify which ones pass the if, and predict the output.
Check: range(1, 6) generates 1, 2, 3, 4, 5; only 2 and 4 are added; the output is 6.
Initialize the accumulator before the loop and update it inside the loop.
Trace the accumulator: 0 → 1 → 3 → 6 → 10 → 15.
text = "Programming for Engineers"
vowel_count = 0
for char in text:
if char in "aeiouAEIOU":
vowel_count += 1
print("Vowels:", vowel_count)The counter changes only when the current character satisfies the condition.
word = "Hello"
modified = ""
for char in word:
if char in "aeiouAEIOU":
modified += "*"
else:
modified += char
print(modified)The result is H*ll*. Each iteration contributes one character to the new string.
Read an unknown number of measurements and report how many exceed the limit.
limit = 75.0
high_count = 0
while True:
temperature = float(input("Temperature (-999 to stop): "))
if temperature == -999:
break
if temperature > limit:
high_count += 1
print("High readings:", high_count)This combines a sentinel-controlled while loop, branching, and a counter.
for Loop to whileThe while version makes initialization, condition, and update explicit.
Answer without running Python first.
x = 12 in if x < 0 ... elif x < 10 ... else ...?range(3, 11, 3) generate?Check: the else branch; 3, 6, 9; n += 1.
True or False and select branches.while loop has initialization, a condition, and an update.for loop iterates over a sequence such as range() or a string.range() excludes its stop value.break exits the innermost loop immediately.