Building Blocks of Algorithms (Statements, State, Control Flow, Functions)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

19CS101 Building Blocks of Algorithms (Statements, State, Control Flow, Functions)

Building Blocks of Algorithms (Statements, State, Control Flow, Functions)

Building blocks of algorithm


It has been proven that any algorithm can be constructed from just three basic building
blocks. These three building blocks are Sequence, Selection, and Iteration.

Building Block Common name


Sequence Action
Selection Decision
Iteration Repetition or Loop
A sequence is one of the basic logic structures in computer programming. In a
sequence structure, an action, or event, leads to the next ordered action in a
predetermined order. The sequence can contain any number of actions, but no actions
can be skipped in the sequence. Once running, the program must perform each action in
order without skipping any.
A selection (also called a decision) is also one of the basic logic structures in
computer programming. In a selection structure, a question is asked, and depending on
the answer, the program takes one of two courses of action, after which the program
moves on to the next event
An iteration is a single pass through a group/set of instructions. Most programs
often contain loops of instructions that are executed over and over again. The computer
repeatedly executes the loop, iterating through the loop

Statements:
Statement is a single action in a computer.
In a computer statements might include some of the following actions
➢ input data-information given to the program
➢ process data-perform operation on a given input
➢ output data-processed result

State:
Transition from one process to another process under specified condition
with in a time is called state.
SNSCT – Department of CSE/19CS101/PPS/S.R.Janani Page 1
19CS101 Building Blocks of Algorithms (Statements, State, Control Flow, Functions)

Control flow:
The process of executing the individual statements in a given order is called
control flow.
The control can be executed in three ways
1. sequence
2. selection
3. iteration

Sequence:
All the instructions are executed one after another is called sequence
execution.
Write an algorithm to add two numbers entered by user.

Step 1: Start

Step 2: Declare variables num1, num2 and sum. Step 3: Read values num1 and num2.

Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2

Step 5: Display sum

SNSCT – Department of CSE/19CS101/PPS/S.R.Janani Page 2


19CS101 Building Blocks of Algorithms (Statements, State, Control Flow, Functions)

Step 6: Stop

Selection:
A selection statement causes the program control to be transferred to a
specific part of the program based upon the condition.
If the conditional test is true, one part of the program will be executed,
otherwise it will execute the other part of the program.

Example
Write an algorithm to check whether he is eligible to vote?
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop
Iteration:
In some programs, certain set of statements are executed again and again
based upon conditional test. i.e. executed more than one time. This type of
execution is called looping or iteration.
Example
Write an algorithm to print all natural numbers up to n
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop

SNSCT – Department of CSE/19CS101/PPS/S.R.Janani Page 3


19CS101 Building Blocks of Algorithms (Statements, State, Control Flow, Functions)

Functions:
❖ Function is a sub program which consists of block of code(set of
instructions) that performs a particular task.
❖ For complex problems, the problem is been divided into smaller and
simpler tasks during algorithm design.
Benefits of Using Functions
❖ Reduction in line of code
❖ code reuse
❖ Better readability
❖ Information hiding
❖ Easy to debug and test
❖ Improved maintainability
Example:
Algorithm for addition of two numbers using function
Main function()
Step 1: Start
Step 2: Call the function add()
Step 3: Stop
sub function add()
Step 1: Function start
Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
Step 5: Return

SNSCT – Department of CSE/19CS101/PPS/S.R.Janani Page 4


19CS101 Building Blocks of Algorithms (Statements, State, Control Flow, Functions)

SNSCT – Department of CSE/19CS101/PPS/S.R.Janani Page 5

You might also like