Sequence means that each step or process in the algorithm is executed in the specified order.In the Kettle example,each process must be in the proper place otherwise the algorithm will fail.
THE DECISION CONSTRUCTS -IF...THEN,IF...THEN...ELSE...
In algorithms the outcome of a decision is either true or false;there is no state in between.The outcome of the decision is based on some condition that can only result in a true or false value.
For example:
if today is Monday then collect pay
Here,the above statement is a decision and the decision takes the general form: if proposition then process
A proposition,in this sense,is a statement,which can only be true or false.It is either true that 'today is Monday' or it is false.If the proposition is true,then the process or procedure that follows the proposition is executed.
The decision can also be stated as:
if proposition
then process 1
else process 2
This is the if...then...else... form of decision.This means that if the proposition is true,then execute process 1,else execute process 2.
The first form of the decision if proposition then process has a null else,that is,there is no else.
LET US TAKE AN EXAMPLE TO UNDERSTAND SEQUENCE AND DECISION.
Q1)Write an algorithm to check whether a number given by user is even or odd.
Solution: Let the number to be checked be represented by 'N'.
The number 'N' is divided by 2 to give an integer quotient,denoted by Q.If the remainder,designated as 'R' is zero,'N' is even,otherwise 'N' is odd.This logic is applied in following algorithm.
- START
- [Ask Input]
Print "Enter the number" - [Take Input]
Input N - [Logic Applied to find whether number is even or odd]
Q <- N/2 (i.e. Q=N/2) - [Finding remainder]
R <- N-Q*2 (i.e. R=N-Q*2) - [Decision Construct]
If R = 0 then Print "N is EVEN" - [Decision Construct] If R!=0 then Print "N is ODD"
- STOP
Q2)Print the largest among three numbers.
Solution: Let the three number be represented by A,B, and C.There can be three ways of solving the problem.The three algorithms with some differences are given below:
Type 1: By using AND operator.
- START
- Print "Enter the Number"
- Input A,B,C
- If A >= B AND B>=C
Then print A - If B >= C AND C >= A
Then Print B
ELSE Print C - STOP
- START
- Print "Enter THREE NUMBERS"
- Input A,B,c
- MAX <-A (i.e. MAX=A)
- If B > MAX Then MAX <- B (i.e. MAX=B)
- If C > MAX Then MAX <- C (i.e. MAX=C)
- Print MAX
- STOP
- START
- Print "Enter three number"
- Input A,B,C
- If A > B then
If A > C Then
Print AElse Print C
Else If B > C Then
Print B
Else Print C - STOP
Comments