Constituents of Algorithms

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


Explain Sequence, selection and repetition (Constituents of algorithms) with examples.

Explanation Video:

Custom Image

Explanation:

Elements of Algorithms: Sequence, Selection, and Repetition

The algorithm consists of three fundamental structures: Sequence, Selection, and Repetition. Such structures give meaning and weight to the solutions of any problem proposed and help ensure that logical and effective solutions are derived for them.

1.Sequence (Step-by-Step Execution)

The sequence structure means that instructions are carried out one by one in a specified manner.
Each step must happen exactly as stated; no loops or jumps are involved.
Example: Adding Two Numbers
Algorithm:
1. Begin
2. Read two numbers.
3. Add two numbers.
4. Print the value.
5. End
The linear flow was towards executing each step in a direct sense-one after the other-without any decision making or repetition.

2. Selection (Decision Making / Conditional Execution)

The selection structure allows the algorithm to make a selection based on the prevalent condition.
This is done through if-else statements, wherein a comparison between the condition and our requirement decides which pathway of the algorithm is followed.
Example: Checking if the Number is Positive or Negative
Algorithm:
1. Begin
2. Read a number.
3. If the number is more than zero:
      Print "Positive".
4. Else:
     Print "Negative".
5. End
The algorithm checks the condition (in this case, whether the number is positive) and chooses a path accordingly. There is no repetition of steps but rather a decision taken.

3. Repetition (Loops / Iteration)

The repetition structure allows the algorithm to run a block of instructions over and over until a specified condition is met.
It is done through loops like for and while.
It is very useful in carrying out operations for printing numbers, searching, and processing data.
Example: Printing numbers 1 to 5
Algorithm:
1. Start
2. Set number i = 1.
3. If i ≤ 5 then:
     Print i.
     Add 1 to i.
4. Repeat Step 3 until i > 5.
5. End

Share this Article & Support Us:
Status
printf('Loading...');