Composition and Decomposition
Chapter 7 · Tamil Nadu State Board
- 10 min read
- 4-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 algorithms can be represented and built from smaller statements. It teaches sequential, alternative, conditional, case analysis and iterative control flow, along with decomposition, refinement and functions. These ideas help organise solutions into manageable steps and make algorithms easier to understand and use. It also compares programming languages, pseudo code and flowcharts as ways to express algorithms.
Written by AI from this lesson. How we check it
Key points
- Algorithms can be written using programming languages, pseudo code or flowcharts.
From your textbook
“There are mainly three different notations for representing algorithms.”
- A programming language is formal, and its grammar must be followed exactly for a computer to execute a program.
From your textbook
“Programs must obey the grammar of the programming language exactly.”
- Pseudo code uses programming-like elements and plain English to communicate algorithms to people.
From your textbook
“Pseudo code is a mix of programminglanguage-like constructs and plain English.”
- Compound statements combine other statements into a hierarchical algorithm structure.
From your textbook
“Statements composed of other statements are known as compound statements.”
- Sequential statements run in written order; alternative statements choose one branch based on a condition.
From your textbook
“The statements in the sequence are executed one after another, in the same order as they are written in the algorithm”
- An iterative statement repeats its body while its condition is true, and stops when the condition becomes false.
From your textbook
“These two steps, testing C and executing S, are repeated until C becomes false.”
- Decomposition breaks a problem into smaller problems, while refinement expands a step into more detail and functions abstract subproblems.
From your textbook
“It involves breaking down a problem into smaller and more manageable problems, and combining the solutions of the smaller problems to solve the original problem.”
Algorithm Notations
Algorithms can be shown in three main ways: programming language, pseudo code, and flowcharts. Each notation helps express an algorithm for a different purpose.
Three ways to represent algorithms
| Notation | Purpose and features |
|---|---|
| Programming language | A formal notation that lets a computer execute an algorithm. Its grammar must be followed exactly. |
| Pseudo code | A mix of programming-like structures and plain English. It is mainly for people to understand algorithms. |
| Flowchart | A diagram that shows the algorithm’s control flow using boxes and arrows. |
Programming Languages, Pseudo Code, and Flowcharts
Definition
- Program
- An algorithm written in a programming language is called a program.
C, C++ and Python are programming languages. A translator changes a program into instructions the computer can execute. If the program has grammar errors, the translator cannot translate it.
Pseudo code uses building blocks such as variables and control flow, but it can also use natural English. It does not need programming-language grammar, but it still must be rigorous and correct. It is the most widely used notation for representing algorithms.
Flowchart symbols
- A rectangle contains a statement and has one outgoing arrow to the next box.
- A diamond contains a condition and has two outgoing arrows labeled true and false.
- A parallelogram represents inputs or outputs.
- Special boxes marked Start and End show where execution begins and finishes.
Flowcharts show the order in which statements and conditions are followed. They are less compact than programming language or pseudo code, can hide an algorithm’s hierarchy, and do not restrict control flow to disciplined alternatives and loops.
Composition and Decomposition
Definition
- Statement
- A statement is a phrase that commands the computer to perform an action.
Definition
- Compound statement
- A compound statement is made from other statements, creating a hierarchical structure in an algorithm.
Decomposition breaks a problem or algorithm into parts. Each part can be refined into smaller parts with more detail, or treated as a function. Composition combines statements into larger statements.
Three control flow statements
- Sequential: statements run one after another in the written order.
- Alternative: the process state is tested, and a statement is selected based on the result.
- Iterative: a control flow statement used to alter the process flow.
A sequential statement made from S1 and S2 is written with S1 first and S2 next. Execute S1, then execute S2. When a problem is split into two statements, the input-output relation of S1 can serve as the input property of S2.
Remember: For sequential control flow, keep the order clear: execute S1 first, then S2.
Assignment Sequences and Safe States
An algorithm can be built from a sequence of assignments. In the farmer, goat, grass and wolf problem, every intermediate state must avoid leaving the goat with the wolf or the grass while the farmer is on the opposite side. The shown sequence reaches the goal state with all four on the same side and obeys this rule.
Remember: Check every intermediate state, not just the starting and ending states.
Alternative Statements and Conditional Statements
Definition
- Condition
- A condition is a phrase that describes a test of the state.
Definition
- Alternative statement
- An alternative statement tests a condition and executes S1 if it is true, or S2 if it is false.
In an alternative statement, the condition is tested first. Only one of its two statements is carried out. The two paths then continue to the statement after the alternative.
Definition
- Conditional statement
- A conditional statement executes S when condition C is true and does nothing when C is false.
A conditional statement is like an alternative statement with an empty else part. It is useful when an action is needed only if a condition is true.
Worked example
Find the minimum of two numbers a and b and store it in result.
Try it first, then show the solutionSolution
- Test whether a < b.
- If it is true, assign a to result.
- Otherwise, assign b to result.
Answer: result = a ↓ b
Case Analysis
Definition
- Case analysis statement
- A case analysis statement checks several conditions in order and executes the statement for the first condition that is true, or the default statement if none is true.
The cases should be exhaustive, so at least one case applies. They should also be disjoint, so only one case applies. If more than one condition is true, the statement still executes only the first matching case.
Comparing two numbers
- If a < b, assign -1 to result.
- If a = b, assign 0 to result.
- Otherwise, a > b, so assign 1 to result.
Remember: Case analysis runs the first matching case, not every matching case.
Iterative Statements and Loops
Definition
- Iterative statement
- An iterative statement repeatedly executes its loop body while its loop condition is true.
A loop tests its condition before each iteration. If the condition is true, it executes the loop body and tests again. When the condition is false, the loop ends and control moves to the next statement.
Parts of a loop
| Term | Meaning |
|---|---|
| Loop condition | The condition tested by the loop. |
| Loop body | The statement repeated by the loop. |
| Iteration | One test of the loop condition and one execution of the loop body. |
| Termination condition | not C, the condition that indicates the loop should end. |
Worked example
For (A, B) = (22, 5), use repeated subtraction to find q and r such that A = q × B + r and 0 ≤ r < B.
Try it first, then show the solutionSolution
- Start with q = 0 and r = 22.
- While r ≥ 5, increase q by 1 and subtract 5 from r.
- The successive values of (q, r) are (1, 17), (2, 12), (3, 7), and (4, 2).
- At (q, r) = (4, 2), the condition r ≥ B is false.
Answer: q = 4 and r = 2
Worked example
If two types of chameleons are equal in number, arrange meetings so both types become zero and all chameleons become the third type.
Try it first, then show the solutionSolution
- While a > 0, let one chameleon from each of the two equal types meet.
- After each meeting, decrease a and b by 1 and increase c by 2.
- For A, B, C = 4, 4, 6, the values become (3, 3, 8), (2, 2, 10), (1, 1, 12), and (0, 0, 14).
Answer: a = b = 0 and c = A+B+C
Problem Decomposition
Definition
- Problem decomposition
- It breaks a problem into smaller, manageable subproblems and combines their solutions to solve the original problem.
A problem may have a structure that helps us divide it into parts. Each part can be broken down further until it is small enough to solve simply. The smaller solutions are then brought together.
Remember: Decomposition means divide the problem, solve the parts, and combine their solutions.
Refinement and Abstraction
Definition
- Refinement
- Refinement repeatedly expands a statement into more detailed steps.
A high-level action such as getting ready for school can be refined into eating breakfast, putting on clothes, and leaving home. Each of those actions can then be expanded into smaller steps. Refinement can continue through several levels of detail.
Ways actions can be refined
- A sequence gives the steps in order, such as taking out a bicycle and riding away.
- A conditional step depends on a condition, such as choosing a dress based on the day.
- An iterative step repeats while a condition remains true, such as eating bites while idlis are on the plate.
Definition
- Abstraction
- Abstraction lets us use a subproblem's specification without needing to know how the subproblem is solved.
A subproblem is specified by its input property and its input-output relation. When solving the main problem, knowing this specification is enough. A flowchart can show the steps of an action, but it does not show the hierarchical structure of refinement.
Functions as Sub-algorithms
Definition
- Function
- A function is a sub-algorithm specified by its input property and the desired input-output relation.
To use a function, the main algorithm needs its name, input property, and input-output relation. The user provides inputs that satisfy the property and can rely on the specified outputs. The function works like a black box: its implementation is hidden from its users.
Why functions help
- The main algorithm can use a subproblem without knowing how it is solved.
- A function may keep its own local variables, which are not visible to the user.
- Keeping those variables inside the function reduces clutter in the main algorithm.
Worked example
Test whether a triangle with sides a, b, c is right-angled, where c is the longest side.
Try it first, then show the solutionSolution
- Use the function square(y), which takes y as input and returns y2.
- Compare square(c) with square(a) + square(b).
- Set result to true if they are equal; otherwise, set result to false.
Answer: The triangle is right-angled when c2 = a2 + b2.
Remember: A function user needs to know what the function does, not how it is implemented.
Circulating the Contents of Three Variables
Circulating means moving each variable’s value to the next variable in a cycle. B gets the original value of A, C gets the original value of B, and A gets the original value of C. The required assignments must preserve the original values until each is used.
Decanting Oil Between Bottles
Represent the amount of oil in the 5L, 8L and 3L bottles with variables. At the start, the 8L bottle is full and the other two are empty. The goal is to divide the oil into two equal quantities.
States and assignment model
- Initial state: 5L bottle = 0L, 8L bottle = 8L, 3L bottle = 0L.
- Final state: 5L bottle = 4L, 8L bottle = 4L, 3L bottle = 0L.
- Model each decanting step by assigning the resulting bottle quantities to the variables.
Tracing the Factorial Algorithm
Definition
- Factorial
- For a non-negative integer n, n! is calculated by multiplying the integers from 1 to n.
The algorithm starts with f = 1 and i = 1. While i ≤ n, it multiplies f by i and then increases i by 1. For factorial(4), the loop runs until i becomes greater than 4.
Questions students ask
Short, direct answers to what students search for in this chapter.
What are the three notations used to represent algorithms?
The three main notations are programming language, pseudo code, and flowcharts. A programming language lets a computer execute an algorithm. Pseudo code helps people understand it. A flowchart shows its control flow visually with boxes and arrows.
What is the difference between a program and pseudo code?
A program is an algorithm written in a formal programming language, and a computer can execute it after translation. Pseudo code is mainly for people to understand. It can mix programming-like structures with plain English and does not have to follow programming-language grammar.
What do the shapes in a flowchart mean?
A rectangle contains a statement. A diamond contains a condition and has true and false arrows. A parallelogram represents input or output. Special boxes marked Start and End indicate the beginning and finish of execution.
What is a sequential statement?
A sequential statement is a sequence of statements that run one after another in the order written. For a sequence made from S1 and S2, execute S1 first and then S2. The arrow from S1 to S2 in a flowchart shows this order.
What is decomposition in algorithms?
Decomposition is a problem-solving technique that breaks an algorithm into parts. Each part can be refined into smaller parts that show more detail, or treated as a function. The parts help organise an algorithm’s structure.
What is an alternative statement in an algorithm?
An alternative statement tests a condition. If the condition is true, it executes S1. If the condition is false, it executes S2. Only one of these statements is carried out, and control then continues after the alternative statement.
Show 11 more questions
What is the difference between a conditional statement and an alternative statement?
An alternative statement chooses between two statements: S1 when its condition is true and S2 when it is false. A conditional statement has no action for the false case. It executes S when the condition is true and does nothing when it is false.
What are exhaustive and disjoint cases in case analysis?
Cases are exhaustive when at least one case applies. They are disjoint when only one case applies. In case analysis, conditions are checked in order, and the statement for the first true condition is executed. A default case runs when none of the conditions is true.
What is an iteration in a loop?
An iteration is one test of the loop condition followed by one execution of the loop body. When the condition is true, the body runs and the condition is tested again. When the condition becomes false, the loop ends.
How does repeated subtraction find the quotient and remainder?
Start with q = 0 and r = A. While r ≥ B, increase q by 1 and subtract B from r. When the condition becomes false, q is the quotient and r is the remainder. For (A, B) = (22, 5), the result is q = 4 and r = 2.
What is problem decomposition?
Problem decomposition breaks a problem into smaller, manageable subproblems. Each subproblem can be broken down further until it is simple enough to solve. The solutions of the smaller problems are then combined to solve the original problem.
What is refinement in problem solving?
Refinement is the process of expanding a statement into more detailed steps. Each step can be expanded again into finer steps. Refinement may use sequences, conditions, or repeated actions, depending on what the task requires.
What is the difference between refinement and abstraction?
Refinement expands a subproblem into increasingly detailed steps. Abstraction lets us use a subproblem by knowing its input property and input-output relation, without needing to know its solution method or implementation.
Why is a function called a black box?
A function is called a black box because its users need to know its name, input property, and input-output relation, but do not need to know how it is implemented. They provide suitable inputs and use the outputs specified for the function.
How can a function test whether a triangle is right-angled?
Use square(y) to find the square of each side. Compare square(c) with square(a) + square(b), where c is the longest side. The result is true if c2 = a2 + b2; otherwise, it is false.
How do I circulate the contents of variables A, B and C?
Circulating the contents means moving the original value of A to B, the original value of B to C, and the original value of C to A. The assignments must be arranged so that each original value is still available when it is needed.
What are the initial and final states in the decanting problem?
Initially, the 5L bottle has 0L, the 8L bottle has 8L, and the 3L bottle has 0L. The final state has 4L in the 5L bottle, 4L in the 8L bottle, and 0L in the 3L bottle.
Book back answers
17 textbook exercises, solved and checked against the book. Questions we could not prove are left out.
SECTION – A Choose the correct answer
Suppose u, v = 10 ,5 before the assignment. What are the values of u and v after the sequence of assignments? 1 u := v 2 v := u
- (a) u, v = 5 ,5
- (b) u, v = 5 ,10
- (c) u, v = 10 ,5
- (d) u, v = 10 ,10
Try it, then show the answerAnswer
(a) u, v = 5 ,5
The first assignment makes u equal to 5. The second then assigns this new value of u to v.
From the book: “Statements may be composed of other statements, leading to hierarchical structure of algorithms.”
Which of the following properties is true after the assignment (at line 3? 1 --i, j = 0, 0 2 i, j := i+1, j-1 3 -- ?
- (a) i+j >0
- (b) i+j < 0
- (c) i+j =0
- (d) i = j
Try it, then show the answerAnswer
(c) i+j =0
Starting with i = 0 and j = 0, the assignment gives i = 1 and j = -1. Their sum is zero.
From the book: “Let the input property be P, and the input-output relation be Q, for a problem.”
If C1 is false and C2 is true, the compound statement 1 if C1 2 S1 3 else 4 if C2 5 S2 6 else 7 S3 executes
- (a) S1
- (b) S2
- (c) S3
- (d) none
Try it, then show the answerAnswer
(b) S2
Since C1 is false, control goes to the else part and tests C2. Because C2 is true, S2 is executed.
From the book: “If C is true, then do S1; otherwise do S2.”
If C is false just before the loop, the control flows through 1 S1 2 while C 3 S2 4 S3
- (a) S1 ; S3
- (b) S1 ; S2 ; S3
- (c)S1 ; S2 ; S2 ; S3
- (d) S1 ; S2 ; S2 ; S2 ; S3
Try it, then show the answerAnswer
(a) S1 ; S3
The loop body S2 is not executed when C is false. Control proceeds from S1 to the statement after the loop, S3.
From the book: “If C is false, the loop ends and the control flows to the next box after the loop.”
Practice
Which notation is intended to help people understand an algorithm rather than be executed directly by a computer?
Exam questions
Practice questions with model answers. These are not from an official paper.
Distinguish between a programming language and pseudo code.2 marks
A programming language is formal and must follow exact grammar so a computer can execute its program. Pseudo code is not formal or exact and is written to help people understand an algorithm; it is not executed directly by a computer.
How does an iterative statement work?2 marks
It tests its condition. If the condition is true, it executes the loop body and tests again. When the condition is false, the loop ends and control moves to the next statement.
What is problem decomposition, and how does it help solve a problem?2 marks
Decomposition breaks a problem into smaller, more manageable problems. The smaller problems can be solved separately, and their solutions combined to solve the original problem.
Why is a function an abstraction, and what does its user need to know?2 marks
A function hides how its subproblem is solved and presents its specification. The user needs to know the function name, input property and input-output relation, but not its implementation.
Watch out
- Do not confuse a conditional statement with an alternative statement: a conditional statement does nothing when its condition is false.
- Do not assume a loop body always runs; if the condition is false before the loop starts, the body is skipped.
Still stuck on this lesson?
Get an AI answer grounded in your actual textbook — with the exact page reference.
Ask AI about this topic →