An algorithm is a set of instructions to perform a specific task. It can be a simple process, such as adding two numbers, bake a cookie, or perform a complex operation, such as creating a war game.
An algorithm in Computer Science
An algorithm is a set of step-by-step instructions in order to solve a problem or perform a task. Algorithms are commonly used in Software Engineering for planning and solving problems.
Characteristics of an algorithm
Input: An algorithm needs data for processing, in a few case data inputs is not required
Output: An algorithm is expected to produce the desired outcome(s)
Definite: The instruction must be clean and with any contradictions (unambiguous).
Finite: An algorithm must terminate after a finite number of steps.
Control structures in the algorithm
1. Sequence: In this structure, steps are placed one after the other starting from up to down to solve the problems.
2. Branching: In this structure, the decision of based on logical outcome (TRUE or FALSE). It creates two branches, one is executed in case of the TRUE condition, the other is executed in case of the FALSE condition.
3. Repetition(Loop): The Repetition or loop allows a step or set of steps to be executed repeatedly until a condition is matched.
How to write an algorithm
Example 1: Algorithm add two numbers