Iteration and recursion

Chapter 8 · Tamil Nadu State Board

  • 8 min read
  • 6-question quiz

Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content

In short

This lesson explains how iteration and recursion repeat actions or solve smaller versions of a problem. It shows how invariants help us track what stays unchanged and reason about loops and possible outcomes. It also teaches that recursive solutions need a base case and must reduce the input size at each recursive step. These ideas help us design algorithms and check that they produce the required result.

Written by AI from this lesson. How we check it

Key points

  • Iteration repeatedly runs a loop body while its condition remains true, updating variables as it goes.
    From your textbook
    “the loop body is repeatedly executed as long as the loop condition is true”
  • An invariant is an expression whose value stays the same after an assignment to its variables.
    From your textbook
    “if an expression of the variables has the same value before and after an assignment, it is an invariant”
  • A loop invariant remains true before and after every execution of the loop body.
    From your textbook
    “The loop invariant is true before the loop body and after the loop body, each time.”
  • A loop invariant and the condition that ends a loop can be used together to establish the required input-output relation.
    From your textbook
    “the termination condition and the loop invariant should establish the input-output relation”
  • Recursion solves a problem by reducing it to a smaller input and building the original solution from the smaller solution.
    From your textbook
    “by solving the instances of the problem with a part of the input”
  • A recursive algorithm needs at least one base case and a recursive step that makes the problem strictly smaller.
    From your textbook
    “there is at least one base case”
Test yourself: 6 questions

Formulas in this chapter

  • Length of a sequencelength(s) = 1 if s has one customer; otherwise, 1 + length(tail(s))Explained here
  • Recursive poweraⁿ = 1 if n = 0; otherwise, a × aⁿ⁻¹Explained here

Iteration and recursion

Iteration and recursion both repeat an action to solve a problem. The action is the same, but the state changes each time, helping the process move toward its goal. In iteration, a loop body repeats while its condition is true. In recursion, a problem is solved by solving the same problem with part of the input, then using that result to solve the original problem.

Definition

Loop invariant
A property of variables that remains unchanged each time the loop body executes.

Four points where a loop invariant is true

  • At the start of the loop, just before it.
  • At the start of each iteration, before the loop body.
  • At the end of each iteration, after the loop body.
  • At the end of the loop, just after it.

How to construct a loop

  • Establish the loop invariant at the start.
  • Update the variables so the process moves toward the end while keeping the invariant true.
  • When the loop ends, use the termination condition and the invariant to establish the required input-output relation.

Invariants of assignments

An expression is an invariant of an assignment if it has the same value before and after the assignment. To check one, replace the variables in the expression with their assigned values at the same time. If the expression stays unchanged, it is an invariant.

Worked example

For (u, v) = (20,15), check whether u + v is invariant under u, v := u + 5, v - 5.

Try it first, then show the solution
  1. Before the assignment, u + v = 20 + 15 = 35.
  2. After the assignment, (u, v) = (25, 10), so u + v = 25 + 10 = 35.

Answer: u + v = 35 is an invariant.

Worked example

For (p, c) = (10, 9), find an invariant under p, c := p + 1, c + 1.

Try it first, then show the solution
  1. Before the assignment, p - c = 10 - 9 = 1.
  2. After the assignment, (p, c) = (11, 10), so p - c = 11 - 10 = 1.

Answer: p - c = 1 is an invariant.

Worked example

Show that p - c is invariant under p, c := p + 1, c + 1.

Try it first, then show the solution
  1. Replace p with p + 1 and c with c + 1 in p - c.
  2. (p + 1) - (c + 1) = p - c.

Answer: The expression does not change, so p - c is an invariant.

Worked example

Check whether m + 3n is invariant under m, n := m + 3, n - 1.

Try it first, then show the solution
  1. Replace m with m + 3 and n with n - 1.
  2. (m + 3) + 3(n - 1) = m + 3 + 3n - 3 = m + 3n.

Answer: m + 3n is an invariant.

Using loop invariants in algorithms

A loop invariant is an invariant of the loop body. It helps us design a loop and reason about how its variables behave. The invariant must hold before the loop body and remain true after each execution.

Worked example

Design an iterative algorithm to compute aⁿ by multiplying a cumulatively n times.

Try it first, then show the solution
  1. Start with p, i := 1, 0. Use p = aⁱ as the loop invariant.
  2. While i ≠ n, update p, i := p × a, i + 1.
  3. Each update keeps p = aⁱ true. When the loop ends, i = n, so p = aⁿ.

Answer: The algorithm computes aⁿ. The examples are power(10, 4) = 10000, power(5, 3) = 125, and power(2, 5) = 32.

Worked example

How many cuts are needed to break a chocolate bar into n individual squares?

Try it first, then show the solution
  1. Start with p = 1 piece and c = 0 cuts.
  2. After each cut, update p, c := p + 1, c + 1 while p ≠ n.
  3. The invariant is p - c = 1. When the loop ends, p = n.
  4. Therefore, n - c = 1, so c = n - 1.

Answer: The number of cuts is one less than the number of squares.

Remember: At the end of a loop, combine the loop invariant with the condition that made the loop stop to reach the required result.

An invariant in the sparrow problem

There are 6 equally spaced trees, with 6 sparrows and one sparrow on each tree. If one sparrow flies from tree i to tree i + d, another flies from tree j to tree j - d. The sum S of the sparrows’ tree indices stays unchanged after each such step.

Definition

Invariant in the sparrow problem
The sum S of the indices of the sparrows’ trees remains unchanged after each pair of flights.

Recursive problem solving

Recursion solves a problem by reducing it to smaller sub-problems. A solver calls itself on a smaller input; this is a recursive call. It uses the sub-problem’s solution to build the solution to the original problem.

Two parts of a recursive solver

PartWhat it does
Base caseSolves the problem directly when its size is small enough. There must be at least one base case.
Recursion stepReduces the problem to a strictly smaller sub-problem, calls a sub-solver, and uses its solution to solve the original problem.

Checks for a correct recursive solution

  • Include at least one base case.
  • Make every recursive call use an input that is strictly smaller than the given input.

Remember: Recursion must eventually reach a base case, and each recursive call must reduce the input size.

Recursive algorithms for sequence length and powers

For a sequence with one customer, its length is 1. Otherwise, its length is 1 plus the length of the tail of the sequence. The tail is a smaller input, so the process can continue until the base case.

Length of a sequence

length(s) = 1 if s has one customer; otherwise, 1 + length(tail(s))

s is the sequence, and tail(s) is the sequence with its first customer removed.

A power can also be calculated recursively. When n = 0, the answer is 1. Otherwise, multiply a by the result of calculating power(a, n-1).

Recursive power

aⁿ = 1 if n = 0; otherwise, a × aⁿ⁻¹

a is the number being raised to a power, and n is an integer with n ≥ 0.

To calculate power(2, 5), the recursive calls reduce the exponent from 5 down to 0. The base case gives 1; the results are then multiplied as the calls return.

Worked example

Calculate power(2, 5) using the recursive definition.

Try it first, then show the solution
  1. power(2, 5) = 2 × power(2, 4)
  2. = 2 × 2 × power(2, 3)
  3. = 2 × 2 × 2 × power(2, 2)
  4. = 2 × 2 × 2 × 2 × power(2, 1)
  5. = 2 × 2 × 2 × 2 × 2 × power(2, 0)
  6. power(2, 0) = 1

Answer: power(2, 5) = 32

Covering a corner-covered board with triominoes

Definition

Triominoe
A triominoe is an L-shaped tile formed with three adjacent squares.

A corner-covered board has 2ⁿ × 2ⁿ squares, with one corner square already covered by a single square tile. The base case is n = 1: cover the other three squares of the 2 × 2 board with one triominoe.

For the recursion step, divide the board into four smaller subboards. Place a triominoe at the centre so that the subboard that already has the covered corner is left out. Each of the four subboards is now corner-covered, so solve each one recursively.

Board-covering algorithm

  • If n = 1, cover the three uncovered squares with one triominoe.
  • Otherwise, divide the board into four subboards of size n-1.
  • Place a triominoe at the centre, leaving out the corner-covered subboard.
  • Recursively tile each subboard of size n-1.

Remember: The central triominoe makes each smaller subboard a corner-covered board, so the same method can be used again.

Questions students ask

Short, direct answers to what students search for in this chapter.

What is a loop invariant?

A loop invariant is a property of the variables that remains unchanged each time the loop body executes. It is true before the loop body and after it, at every iteration. It also helps us construct an iterative algorithm and reason about what happens when the loop ends.

How do you check whether an expression is invariant under an assignment?

Replace each variable in the expression with its assigned value, doing the replacements at the same time. Simplify the result and compare it with the original expression. If both expressions are equal, the expression is an invariant of that assignment.

How does the power(a, n) algorithm compute aⁿ?

It starts with p, i := 1, 0 and keeps the invariant p = aⁱ true. While i ≠ n, it updates p, i := p × a, i + 1. When the loop ends, i = n, so p = aⁿ.

How many cuts are needed to separate a chocolate bar into n squares?

The process starts with p = 1 piece and c = 0 cuts. Each cut increases both by one, keeping p - c = 1. When the process ends, p = n. Therefore, n - c = 1, so the number of cuts is c = n - 1.

What stays invariant when the sparrows fly between trees?

The sum S of the indices of the trees where the sparrows are sitting stays unchanged. In each pair of flights, one sparrow moves from tree i to tree i + d, while another moves from tree j to tree j - d. Their changes balance, so the sum remains invariant.

Why can't all the chameleons become one color?

The starting differences are r - g = -2, g - b = -2, and b - r = 4. None is a multiple of 3. Since meetings change differences only by 3 or leave them unchanged, the counts cannot become equal.

Show 7 more questions

How does parity help solve the marble game?

The number of white marbles either stays the same or decreases by 2, so its odd-or-even status does not change. If it starts even, the final single marble must be black; if it starts odd, the final marble must be white.

What is recursion in an algorithm?

Recursion solves a problem by passing a smaller version of it to another instance of the same solver. The process continues until a small input can be solved directly. The solver then uses that result to answer the original problem.

How does recursion find the length of a sequence?

The solver counts the first item and adds the length of the remaining sequence. It repeats this with smaller sequences until only [E] remains, whose length is 1. For [A,B,C,D,E], the result is 5.

What is recursive problem solving?

Recursive problem solving reduces a problem to smaller sub-problems and calls a sub-solver to solve them. The solver uses the sub-problem solutions to construct the answer to the original problem. The process needs a base case that can be solved directly.

What is the difference between a base case and a recursion step?

A base case solves a problem directly when its size is small enough. A recursion step reduces the problem to a strictly smaller sub-problem, calls a sub-solver, and uses its solution to construct the solution to the original problem.

How do you calculate power(2, 5) recursively?

Use the rule power(a, n) = 1 when n = 0; otherwise, power(a, n) = a × power(a, n-1). This reduces power(2, 5) down to power(2, 0), which is 1. Multiplying the five factors of 2 gives 32.

How does recursion cover a corner-covered board with triominoes?

For n = 1, cover the three uncovered squares of the 2 × 2 board with one triominoe. For a larger board, divide it into four subboards and place a triominoe at the centre, leaving out the subboard with the original covered corner. Recursively tile all four subboards.

Book back answers

4 textbook exercises, solved and checked against the book. Questions we could not prove are left out.

SECTION-B

  1. What is an invariant?

    Try it, then show the answer

    An invariant is an expression involving variables that remains unchanged by an assignment to those variables.

    Its value is the same before and after the assignment.

    From the book: “if an expression of the variables has the same value before and after an assignment, it is an invariant”

  2. Define a loop invariant.

    Try it, then show the answer

    A loop invariant is an invariant of the loop body.

    It remains true before and after each execution of the loop body.

    From the book: “In a loop, if L is an invariant of the loop body B, then L is known as a loop invariant.”

  3. Does testing the loop condition affect the loop invariant? Why?

    Try it, then show the answer

    No. Testing the loop condition does not change the variables, so it does not affect the loop invariant.

    The chapter places the invariant at the start and end of the loop, while the condition determines whether the body executes or the loop ends.

    From the book: “Since L is true at the start of the first iteration, L is true at the start of the loop also”

  4. What is recursive problem solving?

    Try it, then show the answer

    Recursive problem solving reduces a problem to a strictly smaller sub-problem, calls a sub-solver to solve it, and constructs the original solution from that result. It also requires a base case that can be solved directly.

    The recursive call supplies the solution to the reduced input, which is then used to solve the given problem.

    From the book: “The input size to a sub-problem is smaller than the input size to the original problem.”

Practice

Question 1 of 6Score 0

What makes an expression an invariant of an assignment?

Exam questions

Practice questions with model answers. These are not from an official paper.

What is an invariant of an assignment?2 marks

It is an expression involving variables that has the same value before and after the assignment.

Name four points at which a loop invariant is true.2 marks

It is true just before the loop, before each iteration's loop body, after each iteration's loop body, and just after the loop ends.

Explain the two cases required in recursive problem solving.5 marks

The base case solves an input directly when it is small enough. The recursion step reduces the problem to a strictly smaller sub-problem, calls a sub-solver, and uses its solution to construct the solution to the original problem. A recursive solver must have at least one base case.

How does the loop invariant help in designing a loop?5 marks

It is established before the loop and maintained by the loop body as variables are updated. At the end, the invariant and the termination condition together should establish the required input-output relation.

Watch out

  • Do not confuse an invariant with a loop condition: the invariant stays true, while the loop condition determines whether another iteration runs.
  • A recursive step must make the input strictly smaller, and recursion must include at least one base case.

Still stuck on this lesson?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →