Module 2
Algorithm design with Pseudocode and flowcharts
Algorithms and pseudocodes
Algorithm → Written in plain English (step-by-step instructions). Focuses
on what to do.
Pseudocode → Written in a structured, code oriented format, but not in
any specific programming language. Focuses on how to do it.
Example
Algorithm Pseudo Code
1. Start BEGIN
2. Read two numbers INPUT num1, num2
3. Add the two numbers sum ← num1 + num2
4. Show the result OUTPUT sum
5. Stop END
Reasons for using pseudocode
1. Ease of understanding-Uses plain English + programming style, so both programmers and
non-programmers can follow.
2. Bridges the Gap → Acts as a link between algorithm (concept) and program (actual code).
3. Focus on logic
4. More legible
5. Consistent
6. Easy translation to a program
7. Identification of flaws
Constructs of a pseudocode
basic building block used to write pseudocode or programs.
1. Sequence → Steps executed one after another in order.
2. Decision-making step (chooses one path among many).
3. Iteration → A repeating step (looping until a condition is met).
Sequence
instructions are carried out one after another in the exact order they are
written.
no skipping, no repeating.
Decision
Selection is a construct where the program chooses a path based on a
condition
Consists of a test condition together with one or more blocks of
statements.
Result of the test determines which of these blocks is executed.
Decision Types
1. IF
1. If
2. If else
3. If else if else
2. CASE
if structure
Python program to check whether a number is
positive
if else structure
Python program to check whether a person is
Major/Minor
if else-if else structure- if else if ladder
A
Program to compare two numbers
No limit to the number of else if statements, but in the end, there has to
be an else statement.
Conditions tested one by one starting from the top, proceeding
downwards.
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.
Case Structure
Questions on if
Check if a number is even.
Check if a number is divisible by 5.
Questions on if else
Find out if a number is even or odd.
Question on if else if ladder
Program to enter total marks of a student and display the grade based on
the following criteria
≥ 90 → A
≥ 75 → B
≥ 50 → C
< 50 → F
Case Structure
A
Iteration
Repeatedly executes certain block of instructions
Each execution of block is called an iteration or a pass.
If the number of iterations (how many times the block is to be executed)
is known in advance, it is called definite iteration.
Otherwise, it is called indefinite or conditional iteration.
Block that is repeatedly executed is called the loop body.
Types
while loop
repeat-until loop
for loop
while loop
used to implement indefinite iteration.
Loop body (true_instructions) is executed repeatedly as long as condition
evaluates to True.
When the condition is evaluated as False, loop body is bypassed.
Program to print numbers from 1-10 using while loop
[Link]
Print numbers in reverse (10 to 1)
Print even numbers up to 20
Sum of first 5 natural numbers
Multiplication table of a number
Demonstration of infinite loop. Logical mistake
for loop
Implements definite iteration.
There are 3 variants
All three 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.
Loop body repeats execution until the loop variable value reaches end.
First Variant
Loop variable (var) is first assigned (initialized with) the value begin.
Then condition var <= end is tested. If the outcome is True, the loop body is executed.
After first iteration, loop variable is incremented (increased by 1).
Condition var <= end is again tested with the updated value of var and the loop is entered (loop
body is executed), if the condition evaluates to True.
Python program to print numbers from 1 to 10
Print characters of a string
Second Variant
Loop variable is decremented (decreased by 1) after every iteration.
Condition being tested is var >= end.
Third variant
Updates loop variable by an amount other than 1 after every iteration.
Value by which the loop variable is increased or decreased is known as
step.
Repeat until
loop body is repeated as long as condition evaluates to False.
When the condition evaluates to True, loop is exited.
while loop- condition is tested at the beginning (entry controlled loop)
repeat-until loop- condition is tested at the end. (exit controlled loop)
Flowchart
A diagrammatic representation of an algorithm that illustrates how
control flows in it.
blocks
flow-lines.
Inflow and outflow
Problems –
Algorithms and Flowcharts
Problem 2.1 To find simple interest.
Problem 2.2 To determine the largest among two
numbers.
To find the smallest among three numbers
• Problem 2.4 To determine the entry-ticket fare in a zoo
based on age as follows:
• Problem 2.6 To print the numbers from 1 to 50 in
descending order.
• Problem 2.7 To find the factorial of a number.
• Problem 2.8 To determine the largest of n
numbers.
• Problem 2.11 To find the average height of boys and
average height of girls in a class of n students.