PseudoCode
PseudoCode
- Pseudo code is a kind of structured English for representing algorithms.
- Pseudo code is a text based detailed design tool for human understanding.
Basic Guidelines
- Statements should be written in simple English.
- Steps must be understandable and when the steps are followed they should produce the solution.
- Pseudo code should be concise.
- Capitalize keywords like READ, PRINT, and so on.
Keywords Used
- Input : READ, INPUT, OBTAIN, GET
- Output : PRINT, OUTPUT, DISPLAY
- Compute : COMPUTE, CALCULATE
- Initialize : SET, INIT
- Add one : INCREMENT
Basic constructs of Pseudocode
- Sequence
- Selection
- Repetition
Sequence
Each statement in the program is executed one after another in the given order. The statements are executed sequentially from top to bottom
Pseudocode to add two numbers
READ a,b
CALCULATE sum=a+b
PRINT sum
Selection
It is a decision in which a choice is made between two alternative choices of actions. It consists of 2 types.
- IF…THEN…ELSE
- CASE…ENDCASE
(1)IF…THEN…ELSE
Binary choice on a given Boolean condition is indicated by the use of four keywords:
IF, THEN, ELSE and ENDIF
Syntax:
IF condition THEN
sequence1
ELSE
sequence2
ENDIF
Pseudocode to check whether the number is odd or even
READ n
IF n MOD 2 = 0 THEN
PRINT “The number is even”
ELSE
PRINT “The number is odd”
ENDIF
(2)CASE…ENDCASE
CASE is a multiway branch based on the value of an expression. Four keywords are used:
CASE, OF, OTHERS and ENDCASE.
Syntax:
CASE expression OF
condition1:sequence 1
condition2:sequence 2
…………………………
condition n:sequence n
OTHERS:
default:sequence
ENDCASE
Pseudocode for simple calculator
READ num1, num2, choice
CASE choice OF
+ : PRINT num1 + num2
- : PRINT num1 – num2
* : PRINT num1 * num2
/ : PRINT num1 / num2
ENDCASE
Repetition
It is a loop based on the satisfaction of some conditions. It consists of 3 constructs.
- WHILE…ENDWHILE
- REPEAT…UNTIL
- FOR…ENDFOR
(1)WHILE…ENDWHILE
It is a loop based on a simple condition at the beginning. The beginning and ending of the loop is indicated by the keywords WHILE and ENDWHILE.
The loop is entered only if the condition is true. The sequence is performed for each iteration. At the end of each iteration, the condition is executed and the loop continues as long as the condition is true.
Syntax:
WHILE condition
sequence
ENDWHILE
Pseudocode to print number from 1 to 100
SET n=1
WHILE n is less than or equal to 100
DISPLAY n
INCREMENT n by 1
ENDWHILE
(2) REPEAT…UNTIL
It is loop with a simple condition at the bottom. It is similar to WHILE loop except that the test is performed at the bottom of the loop.
The sequence is performed at least once because the condition is evaluated after the sequence is performed. At the end of each iteration, the condition is evaluated and the loop repeats if the condition is false. The loop ends when the condition become true.
Syntax:
REPEAT
sequence
UNTIL condition
Pseudocode to print number from 1 to 100
SET n=1
REPEAT
DISPLAY n
INCREAMENT n by 1
UNTIL n is greater than 100
(3) FOR…ENDFOR
For is counting loop. The loop is used for interating a specific number of times. The keywords used are FOR and ENDFOR.
Syntax:
FOR iteration bounds
sequence
ENDFOR
Pseudocode to print number from 1 to 100
FOR n=1 to 100
DISPLAY n
ENDFOR
ADVANTAGES
- Can be read and understood easily.
- Can be modified easily.
DISADVANTAGES
- Creates an additional level of documentation to maintain
- Introduces error possibilities.

No comments:
Post a Comment