Module 2 covers:
x = 5
if x > 0:
print("x is positive")
x = -2
if x > 0:
print("x is positive")
else:
print("x is not positive")
grade = 85
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
else:
print("D or F")
a, b = 5, 2
if a > 0:
if b > 0:
print("Both positive")
else:
print("a positive, b not positive")
else:
print("a not positive")
a > b, x == y, z != 0, True, Falseand, or, not<, >, <=, >=, ==, !=x = 5
if x > 0 and x < 10:
print("x is between 1 and 9") # Output: x is between 1 and 9
count = 0
while count < 5:
print(count)
count += 1
for letter in "Python":
print(letter)
for i in range(3): # 0, 1, 2
print(i)
for i in range(1, 6): # 1, 2, 3, 4, 5
print(i)
for i in range(2, 10, 2): # 2, 4, 6, 8
print(i)
for i in range(10):
if i == 5:
break
print(i) # Prints 0 to 4
for i in range(5):
if i == 2:
continue
print(i) # Prints 0, 1, 3, 4
for _ in range(3):
pass # Does nothing, useful as a placeholder
for i in range(3):
for j in range(2):
print(i, j)
for i in range(3):
print(i)
else:
print("Loop finished!") # Only runs if loop wasn’t broken
| Concept | Syntax Example | Notes |
|---|---|---|
| Simple if | if cond: ... |
One branch decision |
| if-else | if cond: ... else: ... |
Two branch decision |
| if-elif-else | if ... elif ... else ... |
Multi-way decision |
| while | while cond: ... |
Repeat while condition is True |
| for | for x in seq: ... |
Iterate over a sequence |
| range() | range(5), range(1,5), range(2,10,2) |
Generates number sequence |
| break | break |
Exit loop immediately |
| continue | continue |
Skip rest of this iteration |
| pass | pass |
No operation, placeholder |
| loop-else | for ... else: ... |
Runs if loop not broken |
| Nested loops | for ... for ... |
Outer/inner traversals |
| Relational operators | < > <= >= == != |
Compare values |
| Logical operators | and or not |
Combine boolean conditions |
Use this exhaustive file to master control flow in Python for the PCEP-30-02 exam. Work through the examples, exercises, and problems for best results!