An algorithm has a finite number of steps. Some steps may involve decision-making and repetition. Broadly speaking, an algorithm may employ one of the following control structures:
- sequence
- decision
- repetition
Sequence
By sequence, we mean that each step of an algorithm is executed in a specified order
Decision
Decision statements are used when the execution of a process depends on the outcome of some condition. For example, if x = y, then print EQUAL. So the general form of IF construct can be given as:
IF condition Then process
A condition in this context is any statement that may evaluate to either a true value or a false value. In the above example, a variable x can be either equal to y or not equal to y. However, it cannot be both true and false. If the condition is true, then the process is executed.
A decision statement can also be stated in the following manner
IF condition
Then process1
ELSE process2
Repetition
Repetition, which involves executing one or more steps for a number of times, can be implemented using constructs such as while, do–while, and for loops. These loops execute one or more steps until some condition is true