DEV Community

Nicholus Mush
Nicholus Mush

Posted on

Control Flow in Python

Control Flow in Python: Make Decisions and Repeat Actions

Control flow is how programs decide what to do next. With if, for, and while, Python can choose actions and repeat work.

What you'll learn

  • if / elif / else
  • for loops over collections
  • while loops based on conditions

Example

score = 82
if score >= 90:
    print("Excellent")
elif score >= 75:
    print("Good job")
else:
    print("Keep practicing")

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print("I like", fruit)

counter = 1
while counter <= 3:
    print("Loop pass", counter)
    counter += 1
Enter fullscreen mode Exit fullscreen mode

Real-world example

A retail checkout system applies a discount only when the cart total is above a threshold. This is decision-making applied to a practical task.

Why it's important

Control flow turns static code into behavior: the ability to respond to input, handle different cases, and automate repeated tasks.

Next

Combine control flow with functions to build reusable logic.

Top comments (0)