Concepts / Loops and Conditional Logic

Loops and Conditional Logic

A common conditional pattern is to test whether a remainder equals 0.

  • Programming

The Leftover in Every Division

When objects are placed into equal-sized groups, division tells you how many complete groups can be made, but it may leave some objects ungrouped. The modulus operator, written as %, extracts that leftover value. In Python, an expression such as a % b finds the remainder when the integer a is divided by the integer b.

In the real world

Imagine having 7 apples and placing them into groups of 3. Two complete groups use 6 apples, leaving 1 apple ungrouped. The modulus operation 7 % 3 represents that leftover, so its result is 1.

7 apples2 groups of 36 apples1 appleremaindermake complete groupsleft over
How do complete groups and leftover items combine to produce a remainder?

Reading a Modulus Expression

The syntax is a % b. The first value, a, is the dividend: the number being divided. The second value, b, is the divisor: the number that defines the group size. The result is the remainder after making as many complete groups of size b as possible.

OperatorWhat it tells you
//How many complete groups fit
%What remains ungrouped
/The decimal result of division

These operators describe different results from the same division operation.

Dividendoriginal numberQuotientcomplete groupsDivisorgroup sizeRemainderleftovercomplete groups contributegroup size contributesleftover contributes
How do the quotient, divisor, and remainder connect to reconstruct the original dividend?

Tracing 17 % 5

What do you think happens?

What does 17 % 5 return?

  • 2

  • 3

  • 5

  • 0

Reveal answer
Answer:

2

Five fits into 17 three complete times, using 15. The 2 that remain is the modulus result.

Worked example

Finding the Remainder

Evaluate 17 % 5.

  1. 1
    Find complete groups

    Groups of 5 fit into 17 three complete times.

  2. 2
    Count what was used

    Three groups of 5 use 15 of the 17 items.

  3. 3
    Count what remains

    Two items remain after the complete groups are formed.

Result

17 % 5 is 2.

17dividend3 groupsgroups of 515used items2remainderdivide by 53 × 517 − 15
How can a dividend be mapped into groups of the divisor to identify the remainder?
python
Output
2
0
5

The second result is 0 because 12 divides evenly into groups of 4. The third result is 5 because the dividend is smaller than the divisor: no complete group of 8 can be made, so all 5 items remain.

Patterns from Remainders

With the same divisor, modulus produces a repeating set of possible remainders. For a divisor of 3, the remainders cycle through 0, 1, and 2. The result never exceeds 2, which is one less than the divisor. This predictable cycle makes modulus useful for detecting patterns, checking divisibility, and working with positions that repeat.

python
Output
0
1
2
0
1
2
0
00 % 311 % 322 % 303 % 3increase inputincrease inputincrease inputcycle continues
How does modulus convert increasing numbers into repeating positions?

A common conditional pattern is to test whether a remainder equals 0. If a number % 2 equals 0, the number divides evenly by 2; this identifies an even-number pattern. A nonzero remainder indicates that the division is not even, which identifies the other side of that two-position pattern.

Numbernumber % 2remainder0even patternNonzeroodd patternapply modulusequals 0does not equal 0
How does checking whether a number % 2 equals 0 distinguish the two number patterns?

Useful Cycles in Programs

Because the remainders repeat, modulus can turn an increasing number into a repeating position. For example, applying modulus with a fixed divisor can help distribute items into buckets, calculate positions in circular structures such as clocks or calendars, and detect repeating patterns. The important idea is that the input can keep increasing while the remainder stays within a fixed range.

Worked example

Mapping Positions into Three Buckets

Use the repeating results of division by 3 to assign successive positions to three repeating buckets.

  1. 1
    Start with position 0

    0 % 3 produces bucket position 0.

  2. 2
    Advance the position

    The next positions produce 1 and 2.

  3. 3
    Continue the cycle

    After position 2, the remainder returns to 0, then repeats 1 and 2.

Result

The repeating positions are 0, 1, 2, 0, 1, 2, and so on.

Mistakes with the Percent Sign

  • Common mistake

    Treating % as ordinary division

    What it looks like

    Expecting 17 % 5 to produce a decimal division result

    Why it fails

    The modulus operator returns the remainder, not the decimal result.

    Do this instead

    Use % when you want what remains after complete groups are formed.

  • Common mistake

    Confusing the remainder with the number of complete groups

    What it looks like

    Reading 7 % 3 as 2 because two complete groups can be made

    Why it fails

    Two is the number of complete groups; the remainder is the one apple left over.

    Do this instead

    Use // for the number of complete groups and % for the leftover.

  • Common mistake

    Assuming a smaller dividend produces zero

    What it looks like

    Expecting 5 % 8 to be 0

    Why it fails

    No complete group of 8 fits into 5, so all 5 items remain.

    Do this instead

    When the dividend is smaller than the divisor, the dividend remains as the remainder.

  • Common mistake

    Forgetting that an evenly divisible pair has remainder 0

    What it looks like

    Expecting 12 % 4 to be 4

    Why it fails

    Four divides into 12 evenly, so nothing is left over.

    Do this instead

    Check whether the dividend divides evenly by the divisor; if it does, the modulus result is 0.

Practice the Remainder

EASY

Predict the result of each expression before checking it: 14 % 4, 20 % 5, 3 % 7, and 10 % 3. For each one, identify the complete groups and the leftover.

Hints
  • Ask how many complete groups of the divisor fit into the dividend.
  • The modulus result is the amount left after those complete groups.
  • If the division is even, the result is 0.
  • If the dividend is smaller than the divisor, the dividend remains.
MEDIUM

A repeating position uses the expression n % 4. List the results for n values 0 through 7. Then describe the cycle you observe and explain how the result could represent one of four repeating positions.

Hints
  • The possible results are bounded by the divisor.
  • Start with 0 % 4 and increase the dividend one step at a time.
  • Look for the point where the result returns to 0.

Modulus Checklist

  1. 1
    The % operator returns the remainder after one integer is divided by another.
  2. 2
    In a % b, a is the dividend and b is the divisor.
  3. 3
    A result of 0 means the dividend divides evenly by the divisor.
  4. 4
    For a fixed divisor, the remainders form a predictable repeating cycle.
  5. 5
    Use modulus for leftovers, divisibility checks, repeating patterns, buckets, and circular positions.

Key Takeaways

  • Modulus, written as %, extracts the remainder from integer division.
  • The expression a % b uses a as the dividend and b as the divisor.
  • A remainder of 0 indicates even division.
  • Repeated modulus operations with the same divisor create cycles of predictable positions.
  • Modulus is useful for divisibility checks, even-or-odd patterns, buckets, and circular structures.

Related concepts

More concepts starting with L