Loading...

My Learning Pad Login

Welcome

PCEP-30-02 Module 2: Comprehensive Teaching Material — Control Flow


Table of Contents

  1. Module Overview & Syllabus
  2. Conditional Statements
    • if Statement
    • if-else Statement
    • if-elif-else Chains
    • Nested Conditionals
    • Boolean Expressions
    • Logical and Relational Operators
    • Exercises
  3. Loops
    • while Loop
    • for Loop
    • range() Function
    • Loop Control Statements (break, continue, pass)
    • Nested Loops
    • Loop + else Clause
    • Exercises
  4. Practice Problems
  5. Summary Table

1. Module Overview & Syllabus

Module 2 covers:


2. Conditional Statements

The if Statement

x = 5
        if x > 0:
            print("x is positive")
        

The if-else Statement

x = -2
        if x > 0:
            print("x is positive")
        else:
            print("x is not positive")
        

if-elif-else Chains

grade = 85
        if grade >= 90:
            print("A")
        elif grade >= 80:
            print("B")
        elif grade >= 70:
            print("C")
        else:
            print("D or F")
        

Nested Conditionals

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

Boolean Expressions

Logical and Relational Operators

Example

x = 5
        if x > 0 and x < 10:
            print("x is between 1 and 9")  # Output: x is between 1 and 9
        

Exercises

  1. Write a program that asks the user for a number and prints "Even" or "Odd".
  2. Write a program to check if a year is a leap year.
  3. Write a program that takes three numbers and prints the largest.

3. Loops

while Loop

count = 0
        while count < 5:
            print(count)
            count += 1
        

for Loop

for letter in "Python":
            print(letter)
        

range() Function

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)
        

Loop Control Statements

break

for i in range(10):
            if i == 5:
                break
            print(i)    # Prints 0 to 4
        

continue

for i in range(5):
            if i == 2:
                continue
            print(i)    # Prints 0, 1, 3, 4
        

pass

for _ in range(3):
            pass    # Does nothing, useful as a placeholder
        

Nested Loops

for i in range(3):
            for j in range(2):
                print(i, j)
        

Loop + else Clause

for i in range(3):
            print(i)
        else:
            print("Loop finished!")   # Only runs if loop wasn’t broken
        

Exercises

  1. Print all multiples of 5 up to 50 using a loop.
  2. Write a program that sums all numbers the user enters until they type zero.
  3. Use a nested loop to print a multiplication table (1 to 5).

4. Practice Problems

  1. Write code to calculate the factorial of a number using a loop.
  2. Count the number of vowels in a string with a for loop.
  3. Print all prime numbers less than 50 using control flow.
  4. Print a symmetrical triangle of stars using nested loops.
  5. Write code to find the largest number in a list using a for loop.

5. Summary Table

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!