Module - II.pptx
Module - II.pptx
Compiled By
Ms Divya K S
Assistant Professor
Dept. of AI & DS
Muthoot Institute of Technology & Science
Syllabus(9 Hours)
ALGORITHM AND PSEUDOCODE REPRESENTATION:-
Meaning and Definition of Pseudocode, Reasons for using
pseudocode, The main constructs of pseudocode - Sequencing,
selection (if-else structure, case structure) and repetition (for, while,
repeat-until loops), Sample problems*
Algorithm
Ex: Evaluate d = a + b ∗ c.
Pseudocode
Why pseudocodes?
•Wondering why pseudocodes are important? Here are a few
motivating reasons:
• Ease of understanding: Since the pseudocode is programming language
independent, novice developers can also understand it very easily.
• Focus on logic: A pseudocode allows you to focus on the algorithm’s
logic without bothering about the syntax of a specific programming
language.
• More legible: Combining programming constructs with English phrases
makes pseudocode more legible and conveys the logic precisely.
• Consistent: As the constructs used in pseudocode are standardized, it is
useful in sharing ideas among developers from various domains.
• Easy translation to a program: Using programming constructs makes
mapping the pseudocode to a program straightforward
• Identification of flaws: A pseudocode helps identify flaws in the solution
logic before implementation.
Difference between Algorithm and Pseudocode
Feature Algorithm Pseudocode
Definition A set of steps of a problem-solving A high-level representation of an
procedure or method. algorithm using a mixture of technical
format and human-readable text.
Formality Generally, it’s a more abstract and Less formal than code but more
informal concept, not tied to a particular structured than plain language. It uses
programming language or syntax. common language elements and
keywords.
Language Language-neutral; can be expressed in Language-neutral; it aims to be easily
Neutrality plain language or pseudocode. understood by humans as well as easily
converted into code in any
programming language.
Execution Not directly executable; it’s a plan for Not executable on its own; it provides
solving a problem. a high-level overview of the solution’s
logic. We can use it for a dry run.
Syntax No specific syntax rules; described in Uses structured language constructs
plain language or a diagram. like loops, conditionals, and functions
but without strict syntax rules.
Usage Used for planning, design, and Used for communicating and
communication of problem-solving documenting an algorithm before
approaches. writing code.
Constructs of a pseudocode
•A good pseudocode should follow the structured programming
approach.
•Structured coding aims to improve the readability of pseudocode
by ensuring that the execution sequence follows the order in which
the code is written.
•The main constructs are
1. Sequencing
2. Selection
3. Repetition (loop)
Constructs of a pseudocode - Sequence
•This is the most elementary construct where the instructions of the
algorithm are executed in the order listed.
•It is the logical equivalent of a straight line.
•Consider the code below.
1. Start
2. Read the side length of the square
3. area: area = side_length * side_length
4. Print the area
5. End
NOTE:
Once a condition is evaluated to be TRUE, the corresponding block is
executed, and the rest of the structure is skipped. If none of the
conditions are met, the final else part is executed.
Programming the same in Python
•a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Using elif and else
•a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Case Structure
•The case structure is a refined alternative to if else if else structure.
•The general form of this structure is:
If a break statement is omitted from a block, the program will continue executing
the subsequent blocks, regardless of whether the subsequent cases match, until
a break is encountered or the end of the case structure is reached.
Case Structure – Example
The pseudocode
PRINTDIRECTION(dir)
prints the direction
name based on the
value of a character
called dir.
Constructs of a pseudocode - Repetition or loop
•When a certain block of instructions is to be repeatedly executed,
we use the repetition or loop construct.
•Each execution of the block is called an iteration or a pass.
•Definite iteration: If the number of iterations (how many times the
block is to be executed) is known in advance.
•Indefinite or conditional iteration: If the number of iterations is
not known in advance
•The block that is repeatedly executed is called the loop body.
•There are three types of loop constructs as discussed below:
1. while loop
2. repeat-until loop
3. for loop
Constructs of a pseudocode – while loop
•A while loop is generally used to implement indefinite iteration.
The general form of the while loop is as follows: