0% found this document useful (0 votes)
4 views39 pages

Algorithm and Pseudocode Representation

Algorithm and Pseudocode Representation

Uploaded by

ANU JAYAN
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
4 views39 pages

Algorithm and Pseudocode Representation

Algorithm and Pseudocode Representation

Uploaded by

ANU JAYAN
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 39

ALGORITHM AND PSEUDOCODE

REPRESENTATION
ALGORITHM

• Systematic way of solving a problem


• a step-by step procedure that produces an output
when given the necessary inputs.
• uses pure English phrases or sentences to
describe the solution to a problem.
• First step of solving a program
• Roadmap of a program
CHARACTERISTICS OF A GOOD ALGORITHM

• Precision-steps are precisely stated & defined


• Uniqueness- results of each step are uniquely defined and only
depend on input & result of preceding steps
• Finiteness-Stops after a certain number of steps
• Receives input
• Produces output
PSEUDOCODE

• Not real code


• high-level representation of an algorithm
• uses a mixture of natural language and
programming language-like syntax.
• more structured than an algorithm  uses
mathematical expressions with English phrases to
capture the essence of a solution concisely.
ALGORITHM TO EVALUATE: d = a + b ∗ c

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

• Building blocks of a pseudocode


• The main constructs of pseudocode -
Sequencing, selection (if-else structure, case
structure) and repetition (for, while, repeat-
until loops)
SEQUENCE

• 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

• S1 is executed first, followed by statement S2, so on


and so forth, Sn until all the instructions are
executed.
• no instruction is skipped
Types
SELECTIO • If
N OR • Case

DECISION consists of a test condition together


with one or more blocks of statements.

The result of the test determines which


of these blocks is executed.
IF

3 variations

If else If else if else


If structure
structure structure
IF STRUCTURE

if (condition)
true_instructions
Endif

If the test condition True true_instructions are executed.


false true instruction
statements are skipped.
PSEUDOCODE TO CHECK IF AN INPUT A VALUE
AND CHECK IF IS POSITIVE

Read X
if (x > 0)
Print(x,“ is positive”)
endif
ALGORITHM

• Input the Value of x


• If x is greater than zero,it is a positive
number.Otherwise ,it is not positive
• print x is positive,if the number is positive
PYHTON CODE TO CHECK IF X IS POSITIVE

x=int(input(“Enter Value of X”))


if x>0:
print(x,"is positive")
IF ELSE STRUCTURE

if (condition)
true_instructions
else
false_instructions
endif
• two blocks of statements.
• condition Truethe first block (denoted by
true_instructions) is executed ,skips over the
second block (denoted by false_instructions).
• condition Falsethe 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

age=int(input(“Enter Your age”))


if age>=18:
print(“You are a major”)
else:
print(“You are a minor”)
IF ELSE IF ELSE STRUCTURE

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

• When a certain block of instructions is to be repeatedly.


Each execution of the block is called an iteration or a
pass.
• number of iterations (how many times the block is to be
executed) is known in advance—>definite iteration.
• Otherwiseindefinite or conditional iteration.
• The block that is repeatedly executed is called the loop
body
WHILE LOOP

• to implement indefinite iteration.

while (condition)
true_instructions
Endwhile

• the loop body (true_instructions) is executed repeatedly as long as


condition evaluates to True.
• When the condition is evaluated as False, the loop body is bypassed
REPEAT UNTIL LOOP

• 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

• for var = begin to end


loop_instructions
Endfor
• for var = begin downto end
loop_instructions
Endfor
• for var = begin to end by step
loop_instructions
Endfor
FLOWCHARTS

• diagrammatic representation of an algorithm


• depicts how control flows in it.
• composed of various blocks interconnected by flow-lines.
• Each block in a flowchart represents some stage of
processing in the algorithm.
• Different types of blocks are defined to represent the
various programming constructs of the algorithm.
• Flow lines indicate the order in which the algorithm
steps are executed.
• lines entering a block  data (or control) flow into the
block
• lines emerging from a block  data (control) outflow.
• Most blocksonly single incoming and outgoing flow
lines.
• selection and loop constructs have multiple exits,
one for each possible outcome of the condition being
tested and each outcomea branch
TO FIND SIMPLE INTEREST-PSEUDOCODE

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.

You might also like