Two ideas that do most of the work
Most programs, no matter how large, are built from a few basic ideas. Two of the most important are making decisions and repeating actions. A condition lets the program choose between paths. A loop lets it do something many times without you writing the same line over and over.
You already use both ideas in daily life. “If it is raining, take an umbrella” is a condition. “Stir until the sauce thickens” is a loop. The examples below are in Python, but other languages express the same ideas with slightly different punctuation.
Conditions: if, elif and else
A condition is a question with a yes-or-no answer, such as “is the temperature above 25?”. The answer is a Boolean value: True or False. An if statement runs a block of code only when the answer is True.
temperature = 28
if temperature > 25:
print("It is warm.")
else:
print("It is not warm.")
Here 28 is greater than 25, so the program prints It is warm. The else part runs only when the condition is false.
When there are more than two possibilities, elif (short for “else if”) adds extra checks, which are tried in order:
score = 72
if score >= 80:
grade = "A"
elif score >= 60:
grade = "B"
else:
grade = "C"
print(grade)
Because 72 is not at least 80 but is at least 60, this prints B. The order matters. Python stops at the first condition that is true, so the checks must be arranged with care. If the >= 60 check came first, a score of 90 would wrongly receive a B.
Comparison and combining
Conditions use comparison operators: == (equal to), != (not equal), <, >, <= and >=. Note that == tests equality, while a single = assigns a value to a variable. Mixing them up is one of the most common beginner mistakes.
You can combine conditions with and, or and not:
age = 16
has_ticket = True
if age >= 12 and has_ticket:
print("Entry allowed.")
Both parts must be true for the message to appear. The spreadsheet function IF follows the same logic, so anyone comfortable with spreadsheet basics already has a head start.
Loops: repeating without retyping
The for loop
A for loop repeats a block once for each item in a sequence. It is the usual choice when you know in advance what you are going through.
for number in range(1, 6):
print(number)
The range(1, 6) function produces the numbers 1, 2, 3, 4 and 5. It starts at the first value and stops just before the second, which surprises many beginners. The loop therefore prints five lines, from 1 to 5.
A loop can also build up a result. This adds up the numbers from 1 to 5:
total = 0
for number in range(1, 6):
total = total + number
print(total)
The variable total starts at 0 and is updated each time around: 1, then 3, then 6, then 10, then 15. The program prints 15, which matches 1 + 2 + 3 + 4 + 5.
You can also loop over a list directly:
colours = ["red", "green", "blue"]
for colour in colours:
print(colour)
This prints each of the three colors on its own line.
The while loop
A while loop keeps repeating as long as its condition stays true. It suits situations where you do not know in advance how many repeats are needed.
count = 3
while count > 0:
print(count)
count = count - 1
print("Done")
The loop prints 3, then 2, then 1. When count reaches 0 the condition count > 0 is false, so the loop ends and Done is printed.
Avoiding endless loops
A while loop must do something that eventually makes its condition false. If you forget the line count = count - 1 in the example above, count stays at 3 forever and the program never stops. This is called an infinite loop. Most environments let you interrupt a running program from the keyboard, and checking that something inside the loop changes is the first thing to look for. Finding such mistakes is part of a debugging mindset: run, observe, adjust.
Putting them together
Loops and conditions are most powerful in combination. This program counts how many numbers from 1 to 10 are even:
even_count = 0
for n in range(1, 11):
if n % 2 == 0:
even_count = even_count + 1
print(even_count)
The % operator gives the remainder after division, so n % 2 == 0 is true when a number divides evenly by 2. The even numbers from 1 to 10 are 2, 4, 6, 8 and 10, so the program prints 5.
How to think about them
When you meet a problem, two questions help:
- Does something repeat? If you notice the same action several times, a loop is probably appropriate. Spotting repetition is a key part of computational thinking.
- Does the action depend on something? If so, a condition decides when it happens.
Before running code, trace it by hand with small numbers, writing down the value of each variable at every step. It is slow, but it builds a reliable mental model of what the computer does. Remember that these structures are the building blocks of any algorithm basics you will meet: a precise list of steps that includes decisions and repetition.
Loops and conditions look simple, but nearly everything else, from games to web pages, rests on them. Practicing with small examples until they feel natural is time well spent.