Skip to content

Exercise 6 of 10 · Loops and conditions

Even Numbers Only

What you will make

A program that walks through the numbers 1 to 10 and prints only the even ones, using % to test each number as the loop reaches it.

The one new idea: an if inside a for loop, decided again on every pass

Combining a loop with a condition is how a program filters or reacts to each item it sees, one at a time — this pattern shows up constantly, from checking every row in a report to validating every field in a form.

Go straight to the code ↓

A condition inside a loop

Putting an if inside a for loop means the condition gets checked again on every single pass, using whatever the loop variable currently holds. It's not decided once and reused — each pass asks the question fresh, which is what lets a loop treat different values differently instead of doing the same thing to all of them.

%, the modulo operator, gives the remainder left over after dividing one number by another. $n % 2 is 0 for every even number and 1 for every odd one, which makes it the standard way to test whether a number is even. Combining the two — a fresh if on every pass of a loop, testing a remainder — is how a program filters a whole range of numbers down to just the ones it cares about, without writing out a separate check for each one.

A worked example

Here's a different filter, using a different rule, so you can see the pattern with new numbers:

PHP
<?php
for ($i = 1; $i <= 6; $i++) {
    if ($i % 3 === 0) {
        echo "$i is a multiple of 3\n";
    }
}

Running it prints:

Output
3 is a multiple of 3
6 is a multiple of 3

The loop runs six times, but the if inside it only lets the echo run when $i % 3 comes out to 0 — that happens for 3 and 6, and nothing else, out of the numbers 1 through 6.

Your turn

The starter program is supposed to print only the even numbers from 1 to 10, but right now it prints the odd ones instead.

Look at the remainder the if is currently checking for. Numbers with a remainder of 1 after dividing by 2 are the odd ones; numbers with a remainder of 0 are the even ones. Fix the number being compared against, then press Run.

If something goes wrong

If the loop prints the wrong set of numbers entirely, double-check which remainder means "even" — it's 0, not 1, since an even number divides by 2 with nothing left over. If nothing prints at all, make sure the if is still sitting inside the loop's { } braces, and that % (not /) is the operator being used to find the remainder.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Mixing up which remainder means even and which means odd
A number is even when it divides by 2 with nothing left over — that's a remainder of 0, not 1. $i % 2 === 1 catches the odd numbers instead.
Using == instead of === out of habit
== converts the two values before comparing, which can accept types you didn't intend. === checks the value and type together, so it only matches when both are truly equal — that's the safer default in PHP.
Putting the if outside the for loop's braces
The if has to be *inside* the loop's { } to run again for every value of $i. Outside the braces, it would only run once, after the loop has already finished.

Want a blank editor instead? Open the PHP playground.