# What is an Algorithm?

**Algorithm:** A step-by-step solution to a given problem. In this article, you will learn about what is an algorithm, and how to write an algorithm with examples and explanations.

We use computers to solve complex problems in a systematic and easy manner. In order to solve a problem there should be a set of sequential steps, and each of these steps should specify some simple action that needs to be performed.

Thus an algorithm may be defined as a finite and ordered sequence of steps that when performed lead to the solution of the problem in a definite time.

The ordered sequence implies that the execution takes place in the same order in which the statements are written, i.e. the steps of the algorithm or instructions are written in a sequence that the next instruction follows automatically.

Now to maintain the order or sequence, you need to assign positive integers to the steps. The order BEGIN and END normally refers to the beginning and the end of the algorithm.

**Characteristics of Algorithm?**

An algorithm must possess the following characteristics.

**Finiteness:**Implies that the algorithm must have a finite number of steps so that time taken to execute all the steps of the algorithm should be finite and within a reasonable limit.

**Definiteness**: By definiteness, it is implied that each step of the algorithm must specify a definite action, i,e. The steps should not be vague. Moreover, the steps should be such that it is possible to execute this manually in a finite length of time.

**Input**: The term input means supplying initial data for an algorithm must be presented Before any operations can be performed on it sometimes no data is needed because initial data may be generated within the algorithm. Thus the algorithm may have more inputs. Generally, the initial data is supplied by a read instruction initial value using set instruction.

**Output**: The term output refers to the results obtained when all the steps of the algorithms have been executed. An algorithm must have at least one input.

**Effectiveness**: Effectiveness implies that all the operations involved in an algorithm must be sufficiently basic in nature so that they can be carried out manually in finite intervals of time.

**Advantage of Algorithm**

- An algorithm is written in simple English-like language.
- It is simple to understand, as it gives you a step-by-step solution to a problem.
- It is easy to debug i.e., errors can be easily pointed out.
- It is independent of any programming language, which means a well-written algorithm can be easily coded in any programming language

**How to write an algorithm?**

The process for expressing the algorithm is quite simple. The language used to write algorithms is similar to our day-to-day life language. In addition, some special symbols are also used which are described below.

- Assignment Symbol (← ): This symbol is called an assignment symbol which is used to assign values to a variable while writing an algorithm. For Example, Let me take A and B as two separate variables or constants or an expression.

Then the statement

A ← B

Is called an assignment statement which implies that A is assigned the value stored in B.

If A contains any previous value then the previous value will be destroyed and the value will be assigned.

**Relational Symbols:**You may need to define the relationship between to operand or expression or statements while writing an algorithm. In such cases use the following table for your reference.

### Relational Symbols Used in Algorithm

- Brackets( {} ). The pair of braces is used to write comments for the purpose of documentation.

For example:

- BEGIN {Start of the Algorithm}
- Set N ← N + 1 {increase the value of N by 1}
- END {End of the Algorithm}

**Basic Control Structure Used in Algorithm**

To write a good and efficient algorithm you need to use the following control structure

**Selection : **

The selection structure is used to perform a given set of instructions if a condition is TRUE and an alternative set of instructions if the condition is FALSE. The basic statement available for selection is IF-THEN-ELSE.

The Syntax is as given below:

If (condition is true)

{

Statement 1

Statement 2

Statement 3

………….

………….

………….

Statement N

}

ELSE

{

Statement 4

Statement 5

……………

……………

Statement N

}

**For example:**

We will consider an algorithm that finds the larger number between two given numbers. The steps are precisely given below.

**BEGIN**

**Step 1** Read NUM1, NUM2

**Step 2** If NUM1> NUM2

**Step 3 ** Then

Write(NUM1,” is greater”)

**Step 4** ELSE

Write (NUM2, “ is greater”)

**END**

**Branching: **

The branching statement is required when we want to transfer the control of execution from one part or step of the algorithm to another part or step. The statement used for branching is GOTO.

Syntax:

GOTO n

Where N is a positive integer and specifies the step number where the control of execution is to be transferred.

**Looping: **

The looping structure is used when a statement or a set of statements is to be executed a number of times.

The following two loop control structures are commonly used in algorithms:

**WHILE-DO****REPEAT-UNTIL**

**WHILE-DO: **

The Syntax of WHILE-DO is given below

Step 1 WHILE (Condition) DO

Step 2 Statement 1

Step 3 Statement 2

…………………..

……………………

…………………….

Step N +1 Statemment N

Step N+2 END-WHILW (End of WHILE-Do Loop)

**Explanation:**

This control loop structure implies that as long as the condition remains TRUE. All the steps listed between WHILE-DO and END-WHILE are executed again and again. As soon as the condition becomes FALSE, the execution of the loop stops, and the control is transferred to the next statement following END-WHILE.

**Example:**

**BEGIN**

**Step 1** Set N ← 1

**Step 2** WHILE( N<= 10) DO

**Step 3** Write N

**Step 4** Set N ←N+1

**Step 5** END-WHILE

**END**

**Explanation:**

The Algorithm starts with the word BEGIN.

**Step 1** Set N ← 1

Here the Set N ← 1 statement is initializing the number 1 as the value of N.

**Step 2** WHILE( N<= 10) DO

We are checking the value of N whether it is less than or equal to 10.

If the value of N meets the condition then the execution will proceed to step 3.

**Step 3** Write N

Where the value of N will be printed.

**Step 4** Set N ←N+1

This is a very crucial statement where two things are happening;

- N+1 will be executed first that will add 1 to the existing value of N
- And the new value (obtained by the N+1 statement) will be stored on N again replacing the previous value.

Once the statement in step 4 is executed, the control will be transferred to step 2, where the WHILE( N<= 10) DO statement will check the value of N again. This process will be repeatedly executed again and again until its greater than 10.

**REPEAT-UNTIL:**

This is similar to WHILE-DO except for the fact that the loop is executed till the condition remains FALSE or the condition becomes TRUE.

**Syntax:**

**Step 1** REPEAT

**Step 2** Statement 1

**Step 3** Statement 2

………………….

……………………

…………………..

**Step N+1** Statement N

**Step N+2** Until (Condition)

**Explanation:**

This control loop structure implies that as long as the condition remains FALSE, all the steps listed between REPEAT and UNTIL are executed again and again.

As soon as the condition becomes TRUE, the execution of the loop stops and control is transferred to the next statement following UNTIL(condition).

**Example:**

Consider this algorithm.

**BEGIN**

**Step 1** Set N ←1

**Step 2** REPEAT

**Step 3** Write N

**Step 4** Set N ← N + 1 (increment N by 1)

**Step 5** UNTIL ( N > 10)

**END**

Here, you see that initially, the value of N is set to 1 and the loop executes till the value of N exceeds 10.

After this, the control goes to the next statement following UNTIL ( N > 10).

### Conclusion:

Thank you for reading this, I hope you find this article interesting and you can check, What is Flowchart? if you have any questions, feel free to write in the comment box.