Conditionals, loops, and functions

~10 min

So far, the code you have read has moved straight down the page, one instruction after another.

But real software also needs to make decisions, repeat work, and organize instructions into reusable pieces, abilities that allow a program to respond differently depending on what is happening.

Three of the most common structures used for this are conditionals, loops, and functions.

This is the deepest this curriculum will go into the mechanics of code, but the goal is still not to write programs from scratch. It is to recognize these structures and understand what a simple piece of business logic is doing.

Conditionals: choosing a path

A Concept · lights on your mapconditionalA structure that asks a true-or-false question and chooses which instructions to perform based on the answer. In Python it begins with if, optionally with elif and else paths. Every rule a product enforces eventually becomes a conditional somewhere. asks a true-or-false question and chooses which instructions to perform based on the answer.

In Python, a conditional commonly begins with the word if:

A conditionalPython
1if is_member:
2print("Free delivery")

You can read this as: if is_member is true, display “Free delivery.”

The indented line belongs to the conditional and runs only when the condition is true.

Indentation is especially important in Python because it shows which lines belong inside a particular structure.

A conditional can also provide another path using else:

Adding another pathPython
1if is_member:
2print("Free delivery")
3else:
4print("Delivery fee applies")

This reads as: if the customer is a member, display “Free delivery.” Otherwise, display “Delivery fee applies.”

Many product rules are implemented using conditionals, comparisons, and related logic.

A banking application might ask whether a transfer exceeds a limit. A shopping application might ask whether a coupon is valid. A streaming service might ask whether a user has an active subscription.

Conditionals allow the same software to behave differently in different situations.

Loops: repeating work

A Concept · lights on your maploopA structure that repeats a group of instructions, often once for every value in a list (“for each price, add it to the total”). Loops are how products handle “all of them” without writing code for each one. repeats a group of instructions.

Loops are especially useful when a program needs to perform the same action for every value in a list.

Imagine that a cart contains three drink prices: drink_prices = [5.50, 4.25, 6.00]

The program could use a loop to calculate the total:

Totaling a cart with a loopPython
1total = 0
2
3for price in drink_prices:
4total = total + price

You can read this as: begin with a total of zero. Then, for each price in the drink-price list, add that price to the total.

The first time through the loop, the program adds 5.50. The second time, it adds 4.25. The third time, it adds 6.00.

The same instructions work whether the list contains three prices or three hundred. The program does not need a separate line of code for every item.

Other loops might process every email in an inbox, display every product in a search result, or check every account in a report.

The important idea is: a loop repeats work without requiring the instruction to be written separately for every value.

Functions: naming reusable work

A Concept · lights on your mapfunctionA named, reusable group of instructions that performs a particular job. It can receive inputs, do processing, and return a result. Module 1’s input → processing → output, at the scale of a paragraph of code. Most code is functions calling other functions. is a named group of instructions designed to perform a particular job.

A function can receive inputs, perform processing, and return a result. That shape should sound familiar: input → processing → output

A function is a smaller version of that same pattern inside a program.

Consider a function that calculates a delivery fee:

The delivery-fee functionPython
1def delivery_fee(order_total, is_member):
def means “define a function.” The names in parentheses are the inputs it expects.
2if is_member:
Is the customer a member?
3return 0
return sends a result back and ends the function.
4elif order_total >= 25:
elif means “else if” and is checked only if the first condition was false.
5return 0
6else:
Neither condition was true.
7return 4.99

The first line creates the function: def delivery_fee(order_total, is_member):

In Python, def means “define a function.”

The function is named delivery_fee. The names inside the parentheses, order_total and is_member, name the inputs it expects to receive.

The indented lines underneath contain the function’s instructions.

Reading the function’s decisions

The first conditional is: if is_member:

This asks whether the customer is a member, and if the answer is true, the function runs return 0, a delivery fee of zero.

The word return sends a result back to the part of the program that used the function, and once a return statement runs, the function is finished.

The next path is: elif order_total >= 25:

elif means “else if,” so this condition is checked only if the customer is not a member.

The symbol >= means “greater than or equal to,” so if the order total is at least $25, the function returns zero.

The final path is: else:

If neither earlier condition is true, the function returns a delivery fee of $4.99.

In plain English, the complete rule is: members receive free delivery. Non-members receive free delivery on orders of at least $25. Everyone else pays $4.99.

This is business logic written as code.

Using a function

Defining a function creates and names the reusable instructions, but the program must still call the function when it wants that work performed.

Here is a function call: fee = delivery_fee(31.00, False)

This tells Python to run delivery_fee using an order total of 31.00 and a membership value of False.

The customer is not a member, so the first condition does not pass, but the order total is greater than $25, so the function returns 0.

The variable fee is therefore associated with the value 0.

Parameters and arguments

The function example introduces two similar but distinct terms.

A function’s Concept · lights on your mapparameterA function’s named input slot, as written in its definition (order_total, is_member). The labeled slot, not the value that goes into it; that’s an argument. are the named input slots listed when the function is defined: def delivery_fee(order_total, is_member):

Here, order_total and is_member are parameters.

The Concept · lights on your mapargumentThe actual value supplied when a function is called (31.00, False). The value placed into the slot, not the slot itself; that’s a parameter. are the actual values supplied when the function is called: delivery_fee(31.00, False)

Here, 31.00 and False are arguments.

A helpful way to remember the distinction is: parameters are the labeled slots. Arguments are the values placed into those slots.

In casual conversation, people sometimes use the words interchangeably, but recognizing the technical distinction will help you follow discussions about what information a function expects.

Why functions matter

Functions help keep code organized and reusable.

Without a function, the delivery-fee rules might be rewritten in several places throughout the application, and if the company later changed the free-delivery threshold from $25 to $30, engineers would need to find and update every copy.

By placing the rule in a function called delivery_fee, the team can create one named place responsible for that calculation, and other parts of the program can call the function whenever they need the answer.

This is what engineers may mean when they say that logic is centralized or should not be duplicated.

Functions also make code easier to discuss because a clear function name can communicate its purpose without requiring someone to read every instruction inside it immediately. This is another form of abstraction.

Not every function returns a value. Some functions mainly perform an action, such as sending a message or saving a file, but the input–processing–output model remains a useful starting point for understanding them.

Seeing the structures together

Conditionals, loops, and functions often appear together.

A function might loop through every item in a shopping cart. Inside the loop, a conditional might check whether each item qualifies for a discount. The function could then return the final price.

You do not need to understand every possible combination. The goal is to recognize the roles they play:

  • A conditional chooses a path.
  • A loop repeats work.
  • A function names and organizes a unit of work.

These structures help turn product requirements into software behavior.

The mental model to remember

A conditional asks a true-or-false question and chooses which instructions should run.

A loop repeats a group of instructions, often once for every value in a list.

A function is a named, reusable group of instructions that performs a particular job.

A parameter is a named input slot in a function’s definition.

An argument is an actual value supplied when the function is called.

A return value is the result a function sends back.

You should now be able to look at simple code and identify where it makes a decision, repeats work, or calls a reusable function.

Check — then the lesson continues

Using the delivery_fee function above, what comes back for delivery_fee(19.00, True)?

▼ answer the check to continue ▼