Algorithm and Pseudocode Representation
Algorithm and Pseudocode Representation
REPRESENTATION
ALGORITHM
1 Start
2 Read the values of a, b and c.
3 Find the product of b and c.
4 Store the product in a temporary variable temp.
5 Find the sum of a and temp.
6 Store the sum in d.
7 Print the value of d.
8 Stop.
PSEUDOCODE TO EVALUATE: d = a + b ∗ c
1 Start
2 Read(a, b, c)
3 d=a+b∗c
4 Print(d)
5 Stop
WHY PSEUDOCODES?
1. Ease of understanding: language independent novice
developers can also understand it very easily.
2. Focus on logic: focus on the algorithm’s logic without
bothering about the syntax of a specific programming language.
3. More legible: Combining programming constructs with
English phrases makes pseudocode more legible and conveys
the logic precisely.
4. Consistent: sharing ideas among developers from various
domains.
5. Easy translation to a program: Using programming constructs makes mapping
the pseudocode to a program straightforward.
CONSTRUCTS OF A PSEUDOCODE
• elementary construct
• instructions of the algorithm are
executed in the order listed
• logical equivalent of a straight line
• All steps are executed one after other in a
sequence
Consider code below:
S1
S2
S3
..
Sn
3 variations
if (condition)
true_instructions
Endif
Read X
if (x > 0)
Print(x,“ is positive”)
endif
ALGORITHM
if (condition)
true_instructions
else
false_instructions
endif
• two blocks of statements.
• condition Truethe first block (denoted by
true_instructions) is executed ,skips over the
second block (denoted by false_instructions).
• condition Falsethe first block is skipped and
only the second block is executed.
PSEUDOCODE TO ENTER A PERSONS AGE
AND CHECK IF HE IS A MAJOR OR MINOR
Read age
if (age >= 18)
Print(“You are a major”)
else
Print(“You are a minor”)
endif
PYTHON CODE TO ENTER A PERSONS AGE
AND CHECK IF HE IS A MAJOR OR NOT
if (condition1)
true_instructions1
else if (condition2)
true_instructions2
else
false_instructions
endif
• if condition1 is met, true_instructions1
will be executed.
• Else condition2 is checked. If i True,
true_instructions2 will be selected.
• Otherwise false_instructions will be
executed
CASE STRUCTURE- REFINED ALTERNATIVE TO IF ELSE
IF ELSE STRUCTURE
caseof (expression)
case 1 value1:
block1
case 2 value2:
block2 ...
default :
default_block
endcase
• First, the value of expression (you can also have a single variable
in the place of expression) is compared with value1.
If match block1 will be executed.
Typically, each block will have a break at the end which causes the
case structure to be exited.
• no match,the value of the expression (or of the variable) is
compared with value2. if match here, block2 is executed and the
structure is exited at the corresponding break statement.
• This process continues until either a match for the expression
value is found or until the end of the cases is encountered.
• The default_block will be executed when the expression does not
match any of the cases.
• If the break statement is omitted from the block for the matching
case, then the execution continues into subsequent blocks even
if there is no match in the subsequent blocks, until either a break
is encountered or the end of the case structure is reached.
a=1
match a:
case 1:
print("a=1")
case 2:
print("a=2")
case _:
print("a is neither 1 nor 2")
REPETITION OR LOOP
while (condition)
true_instructions
Endwhile
• Indefinite interation
• Not in python
repeat
false_instructions
until (condition)
FOR LOOP
• Definite iteration
• for loop constructs use a variable (call it the
loop variable) as a counter that starts counting
from a specific value called begin and updates
the loop variable after each iteration. The loop
body repeats execution until the loop variable
value reaches end
FOR -VARIATIONS
1 Start
2 Read(principal , rate, years)
3 SI = (principal ∗ rate ∗ years)/100
4 Print(SI )
5 Stop
TO FIND
SIMPLE
INTEREST-
FLOWCHART
•
TO FIND LARGER OF 2 NUMBERS
PRINT THE NUMBERS FROM 1 TO 50 IN
DESCENDING ORDER
TO FIND FACTORIAL OF
A NUMBER
•
TO DETERMINE THE AVERAGE
AGE OF STUDENTS IN A
CLASS. THE USER
WILL STOP GIVING THE INPUT
BY GIVING THE AGE AS 0.