JOVANA
Library Glossary Getting Started Three Levels Fields How it works Mission
Join the mission
All guides

Loops & Conditionals: Repeat and Decide

Two small ideas give your programs a brain and a heartbeat — deciding what to do, and doing it again and again — and almost everything else is built on top of them.

Making decisions: if this, then that

Up to now, a program might feel like a recipe that always does exactly the same steps in the same order. But real life isn't like that — you bring an umbrella if it's raining, and leave it home otherwise. A conditional is how you give a program that same common sense: it looks at whether something is true, and then picks which path to take.

In code, this almost always uses the words if and else. You write `if` followed by a question that can only be answered yes or no — like "is the score above 60?" — then the block of work to do when the answer is yes. The `else` part is optional: it's the fallback, what to do when the answer turns out to be no.

if (score >= 60) {
  print("You passed!")
} else {
  print("Try again.")
}
If the score is 60 or more, say you passed; otherwise, say try again. Exactly one branch runs.

Repeating: do it again, and again

Computers are wonderful at doing boring things without complaint. Suppose you need to send the same email to 500 people. You would never copy-paste it 500 times by hand — and you don't have to. A loop is the tool that says "do this action over and over," so you write the steps once and the computer repeats them for you.

Loops come in two everyday flavors. The first repeats once per item — "for each name on this list, send the email." The second repeats until a condition stops it — "keep asking for a password while the one I got is wrong." Both share the same heartbeat: do the work, check whether to go again, do the work, check again.

Looping over a list, item by item

Most of the time you loop because you have a collection of things to handle. In code, an ordered collection like that is called an array — think of it as a row of numbered boxes, each holding one value. The most common loop you'll ever write simply walks that array from the first box to the last, doing one small job at each stop.

fruits = ["apple", "pear", "plum"]

for fruit in fruits {
  print("I have a " + fruit)
}
For each fruit in the list, print one line. The loop runs three times — once per item — and then stops on its own.

Read that `for ... in ...` line out loud as "for each fruit in fruits." The word `fruit` is a fresh label that points at one item each time around — first "apple," then "pear," then "plum." You write the body once, and the loop kindly swaps in the next item for you on every pass. That's the whole trick, and you'll use it constantly.

The classic beginner traps

Two mistakes catch nearly everyone early on, so spotting them now will save you hours later. The first is the off-by-one error. It usually bites when you count positions yourself instead of using `for each`, because in most languages the first box in an array is numbered 0, not 1 — so a list of 3 items has positions 0, 1, and 2. Reaching for position 3 grabs nothing, or crashes.

The second trap is the infinite loop: a repeat that never stops, because the condition you told it to wait for never becomes true. Picture telling a friend "keep walking until you reach the red door," but there is no red door — they'd walk forever. In code, the loop just spins, the program freezes, and your fan starts roaring.

Recap

You've just met the two ideas that turn a flat list of instructions into something that thinks and acts. A conditional decides — it asks a yes/no question and picks a path. A loop repeats — once per item, or until a condition says stop.

The most common pattern you'll write is the two of them together: loop over an array, and inside the loop use an `if` to decide what to do with each item. Keep an eye out for the off-by-one and infinite-loop traps, always ask "what makes this stop?", and you'll have a remarkable amount of programming already in your hands.