Making Decisions: Control Flow Basics

Programming is powerful because it allows us to make decisions and repeat tasks automatically. This is called control flow, and it’s how we get programs to respond intelligently to different situations.

In this post, we’ll explore two key concepts: conditional statements and loops.


1. Conditional Statements: The “What If” in Programming

Conditional statements let your program make decisions based on certain conditions. Think of it like asking, “What should I do if this happens?”

Example:

If you’re hungry, eat something. If not, do nothing.

In Python, it looks like this:

isHungry = True

if isHungry:
    print("Go get some food!")
else:
    print("You’re good for now.")

How It Works:

  • if: Checks if the condition is true.
  • else: Runs if the if condition is false.

You can add more conditions with elif (short for “else if”):

hoursOfSleep = 6

if hoursOfSleep < 5:
    print("You need more sleep!")
elif hoursOfSleep < 8:
    print("Not bad, but aim for 8 hours.")
else:
    print("Great job on the sleep!")

2. Loops: Doing Things Over and Over

Loops let your program repeat tasks without writing the same code over and over. There are two main types: for loops and while loops.

For Loops: “Do This for Every Item”

A for loop is great for working with lists or repeating a task a set number of times.

Example:

colors = ["red", "green", "blue"]

for color in colors:
    print(f"The color is {color}.")

Output:

The color is red.
The color is green.
The color is blue.

While Loops: “Do This Until…”

A while loop runs as long as a condition is true.

Example:

count = 1

while count <= 3:
    print(f"Count is {count}.")
    count += 1

Output:

Count is 1.
Count is 2.
Count is 3.

Key Rule:

Always make sure your while loop has a way to stop, or it will run forever!


3. Combining Conditions and Loops

Let’s put it all together! Here’s a program that keeps asking for input until the user says “quit”:

while True:
    userInput = input("Type something (or 'quit' to stop): ")

    if userInput == "quit":
        print("Goodbye!")
        break  # Exit the loop
    else:
        print(f"You said: {userInput}")

How It Works:

  • while True: Creates an infinite loop.
  • if userInput == "quit": Checks if the user wants to stop.
  • break: Exits the loop when the condition is met.

Quick Exercise

Write a program that:

  1. Asks the user for a number.
  2. Prints whether the number is even or odd.
  3. Repeats until the user types “stop.”

Example Solution:

while True:
    userInput = input("Enter a number (or 'stop' to quit): ")

    if userInput == "stop":
        print("Thanks for playing!")
        break

    number = int(userInput)
    if number % 2 == 0:
        print("That’s an even number.")
    else:
        print("That’s an odd number.")

Wrapping Up

Control flow gives your programs the power to make decisions and repeat tasks. It’s a fundamental part of programming, and mastering it will open the door to building more dynamic and useful applications.

Next, we’ll explore functions—the magic that lets you organize and reuse your code. Stay tuned!