CodeAlchemy

Jotting one man's journey through software development, programming, and technology


Project maintained by pablogarciaprado Hosted on GitHub Pages — Theme by mattgraham

◀️ Home

Control Flow Statements

break - Exits the loop entirely

for i in range(5):
    if i == 3:
        break  # Stops the loop when i == 3
    print(i)

# Output: 0, 1, 2

continue - Skips the current iteration and moves to the next

for i in range(5):
    if i == 3:
        continue  # Skips printing 3
    print(i)

# Output: 0, 1, 2, 4

pass - Does nothing, used as a placeholder

def my_function():
    pass  # Placeholder for future code

for i in range(5):
    if i == 2:
        pass  # Doesn't affect the loop
    print(i)

# Output: 0, 1, 2, 3, 4

else in loops - Runs only if the loop does not exit via break

for i in range(3):
    print(i)
else:
    print("Loop finished without break")

# Output:
# 0
# 1
# 2
# Loop finished without break

try-except-finally-else - Handling exceptions

try:
    # Code that might raise an exception
except SomeError:
    # Handle the exception
else:
    # Runs only if no exception occurred
finally:
    # Always runs, whether an exception happened or not