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