Home Coding & Scripting Control Flow in Python (if/else, for, while)

Control Flow in Python (if/else, for, while)

313
0

In programming, control flow determines the order in which your code runs. Instead of executing line by line from top to bottom, you can make decisions and repeat tasks.

In Python, the three most common control flow tools are:

  • if/else statements
  • for loops
  • while loops

Let’s go through each with examples.


📝 If You Want to Practice the Code

If you plan to practice the Python code mentioned within this blog post, you’ll need a working Python environment. You can execute the commands in one of the following ways (choose only one option):

👉 Choose whichever environment you’re most comfortable with. If you haven’t set up Python yet, start with one of the installation posts before trying the examples here.


🔹 if/else Statements

An if statement checks a condition. If the condition is True, Python runs the indented code. If not, it skips it.

age = 18

if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

👉 Output: You are an adult.

You can also chain multiple conditions using elif (else if):

marks = 72

if marks >= 90:
    print("Grade A")
elif marks >= 75:
    print("Grade B")
elif marks >= 50:
    print("Grade C")
else:
    print("Fail")

🔹 for Loops

A for loop lets you repeat something for each item in a sequence (like a list or string), or a range of numbers.

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)

👉 Output:

apple
banana
cherry

You can also loop over numbers using range():

for i in range(5):
    print(i)

👉 Output: 0 1 2 3 4


🔹 while Loops

A while loop keeps running as long as a condition is True.

count = 1

while count <= 5:
    print("Count is:", count)
    count += 1

👉 Output:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5

⚠️ Be careful: if the condition never becomes False, the loop will run forever.


📝 Mini Task

Try this simple program:

# Ask the user for a number and check if it is even or odd
number = int(input("Enter a number: "))

if number % 2 == 0:
    print("Even number")
else:
    print("Odd number")

# Print numbers from 1 to that number using a for loop
for i in range(1, number + 1):
    print(i)

# Keep asking until the user enters 0
while number != 0:
    number = int(input("Enter another number (0 to stop): "))

✅ Summary

  • if/else → decision making
  • for loop → repeat for items or a fixed range
  • while loop → repeat while a condition is true

Control flow makes your programs flexible and interactive, letting you handle different situations and repeat tasks efficiently.

Previous articlePython Variables, Data Types, and Operators
Next articleFunctions in Python
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here