Lesson 7 / 8
Module 4Lesson 1⏱ 55 min study

Lesson 7: Algorithmic Logic: Conditionals and Iteration Loops Across C++ and Python

Compare control flow mechanics across C++ and Python: master boolean algebra, conditional branching (if, elif, switch), and iteration constructs (for, while, range).

✦ Key Architectural Takeaways
  • •Control flow statements alter the linear top-to-bottom instruction pointer based on boolean condition evaluations.
  • •C++ enforces block scope using curly braces ({}), while Python uses semantic indentation (4 spaces) to define code blocks.
  • •The while loop evaluates a condition before each iteration, continuing until the condition evaluates to false.
  • •The for loop in C++ commonly uses a three-part counter (init; condition; increment), whereas Python iterates natively over iterables or range() sequences.
  • •Loop control statements break (terminate immediately) and continue (skip to next iteration) provide granular control over loops.

1. Conditional Branching: Side-by-Side Comparison

Conditional logic directs the computer to choose between different execution pathways based on the truth value of a condition:

CPPSYSTEM SCHEMATIC
// C++ Conditional Structure
int temperature = 28;

if (temperature > 30) {
    std::cout << "Warning: Thermal throttling active." << std::endl;
} else if (temperature >= 20) {
    std::cout << "Normal operating temperature." << std::endl;
} else {
    std::cout << "Sub-optimal cold temperature." << std::endl;
}
PYTHONSYSTEM SCHEMATIC
# Python Conditional Structure (Notice clean elif syntax and semantic indentation)
temperature = 28

if temperature > 30:
    print("Warning: Thermal throttling active.")
elif temperature >= 20:
    print("Normal operating temperature.")
else:
    print("Sub-optimal cold temperature.")
§

2. Iteration: while Loops and Sentinels

A while loop repeats a block of code as long as its controlling boolean expression remains true:

TEXTSYSTEM SCHEMATIC
                  [ Evaluate Condition ]
                            |
                   +--------+--------+
                   |                 |
                [ TRUE ]         [ FALSE ]
                   |                 |
                   v                 v
            [ Execute Body ]    [ Exit Loop ]
                   |
                   v
         [ Re-evaluate Condition ]
PYTHONSYSTEM SCHEMATIC
# Python while loop with dynamic user sentinel termination
total_sum = 0
count = 0

print("Enter numbers to calculate average (type -1 to finish):")
while True:
    entry = float(input("Value: "))
    if entry == -1:
        break  # Immediately exits the while loop
    total_sum += entry
    count += 1

if count > 0:
    print(f"Average of {count} entries: {total_sum / count:.2f}")
§

3. Iteration: for Loops and Sequences

CPPSYSTEM SCHEMATIC
// C++ Traditional Indexed Loop
for (int i = 1; i <= 5; ++i) {
    std::cout << "C++ Iteration: " << i << std::endl;
}

// C++ Modern Range-Based For Loop (C++11 and newer)
int primeNumbers[] = {2, 3, 5, 7, 11};
for (int p : primeNumbers) {
    std::cout << "Prime: " << p << std::endl;
}
PYTHONSYSTEM SCHEMATIC
# Python for loop over range(start, stop, step)
for i in range(1, 6):
    print(f"Python Iteration: {i}")

# Python for loop over native collections
servers = ["web-01", "db-primary", "cache-redis"]
for index, srv in enumerate(servers, start=1):
    print(f"Server #{index}: {srv}")