Home | | Computer Science 11th std | Building Blocks of Algorithms

# Building Blocks of Algorithms

We construct algorithms using basic building blocks such as • Data • Variables • Control flow • Functions

Building Blocks of Algorithms

We construct algorithms using basic building blocks such as

•      Data

•      Variables

•      Control flow

•      Functions

## 1. Data

Algorithms take input data, process the data, and produce output data. Computers provide instructions to perform operations on data. For example, there are instructions for doing arithmetic operations on numbers, such as add, subtract, multiply and divide. There are different kinds of data such as numbers and text.

## 2. Variables

Variables are named boxes for storing data. When we do operations on data, we need to store the results in variables. The data stored in a variable is also known as the value of the variable. We can store a value in a variable or change the value of variable, using an assignment statement.

Computational processes in the real-world have state. As a process evolves, the state changes. How do we represent the state of a process and the change of state, in an algorithm? The state of a process can be represented by a set of variables in an algorithm. The state at any point of execution is simply the values of the variables at that point. As the values of the variables are changed, the state changes.

Example 6.4. State: A traffic signal may be in one of the three states: green, amber, or red. The state is changed to allow a smooth flow of traffic. The state may be represented by a single variable signal which can have one of the three values: green, amber, or red.

## 3. Control flow

An algorithm is a sequence of statements. However, after executing a statement, the next statement executed need not be the next statement in the algorithm. The statement to be executed next may depend on the state of the process. Thus, the order in which the statements are executed may differ from the order in which they are written in the algorithm. This order of execution of statements is known as the control flow.

There are three important control flow statements to alter the control flow depending on the state.

•      In sequential control flow, a sequence of statements are executed one after another in the same order as they are written.

•      In alternative control flow, a condition of the state is tested, and if the condition is true, one statement is executed; if the condition is false, an alternative statement is executed.

•      In iterative control flow, a condition of the state is tested, and if the condition is true, a statement is executed. The two steps of testing the condition and executing the statement are repeated until the condition becomes false.

## 4. Functions

Algorithms can become very complex. The variables of an algorithm and dependencies among the variables may be too many. Then, it is difficult to build algorithms correctly. In such situations, we break an algorithm into parts, construct each part separately, and then integrate the parts to the complete algorithm.

The parts of an algorithm are known as functions. A function is like a sub algorithm. It takes an input, and produces an output, satisfying a desired input output relation.

Example 6.5. Suppose we want to calculate the surface area of a cylinder of radius r and height h.

A = 2πr2 + 2πrh

We can identify two functions, one for calculating the area of a circle and the other for the circumference of the circle. If we abstract the two functions as circle_ area(r) and circle_circumference(r), then cylinder_area(r, h) can be solved as

cylinder_area (r,h) = 2 X circle_area (r) + circle_circumference (r) X h

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 6 : Specification and Abstraction : Building Blocks of Algorithms |