MODULE 3: ALGORITHMS
AND PSEUDOCODE
By
Dr. Johnwendy C.N.
ALGORITHMS AND PSEUDOCODE
Unit 3.1: Meaning and Properties of Algorithms
Unit 3.2: Writing Algorithms (Step-by-step approach)
Unit 3.3: Pseudocode Structure and Syntax
Unit 3.4: Examples of Simple Algorithms (Sum,
Average, Max/Min)
Unit 3.5: Dry Run and Algorithm Testing
Learning Objectives
➢Define algorithms
➢Understand properties
➢Write step-by-step solutions
➢Use pseudocode
➢Perform dry run testing
What is an Algorithm
An algorithm is a finite, well-defined computational
procedure that transforms input into output.
That means:
It is not just steps
It is a logical transformation process
Common Student Mistake
Students think:
“Any steps = algorithm”
Wrong
If steps are:
ambiguous
infinite
illogical
→ It is NOT an algorithm.
Properties of an Algorithm
1. Finiteness
Must terminate.
Why?
Because computers cannot run forever.
Example:
WHILE TRUE
PRINT "Hello“
This is NOT a valid algorithm (no termination)
Properties of an Algorithm
2. Definiteness (Precision)
Each step must be:
unambiguous
exact
Example:
“Process the data”
“Add A and B”
Computers do not guess — they execute.
Properties of an Algorithm
3. Input
Must accept zero or more inputs.
Even no-input algorithms exist (e.g., generate random
number)
4. Output
Must produce at least one result.
No output = useless algorithm
Properties of an Algorithm
5. Effectiveness
Each step must be:
simple
executable in real time
Critical Insight
Algorithm ≠ Code
Algorithm = logic independent of programming
language
That is why:
Same algorithm → can be written in Java, Python, C++
Unit 3.2: Writing Algorithms (Step-by-Step
Approach)
Step 1: Problem Abstraction
Remove unnecessary details
Example:
“Calculate student performance”
→ Reduce to:
Inputs: scores
Output: grade
Unit 3.2: Writing Algorithms (Step-by-Step
Approach)
Step 2: Define Input/Output Clearly
Bad:
→ “Take numbers”
Good:
→ “Take 3 integers: A, B, C”
→ Input three integers: a,b,c
Unit 3.2: Writing Algorithms (Step-by-Step
Approach)
Step 3: Logical Structuring
Use:
Sequence
Selection (if)
Iteration (loops)
Unit 3.2: Writing Algorithms (Step-by-Step
Approach)
Problem: Find the largest of 3 numbers
Naive approach:
→ Compare randomly
Correct structured approach:
start
Input A, B, C
Set MAX = A
If B > MAX → MAX = B
If C > MAX → MAX = C
Output MAX
end
This is efficient and scalable.
Unit 3.3: Pseudocode Structure and Syntax
A formalized representation of algorithmic logic, bridging
human thinking and machine implementation.
Students think:
Pseudocode = random English
Wrong
It must follow:
structure
indentation
logic flow
Control Flow
Control flow is the order in which instructions are executed in
an algorithm.
In simple terms:
It controls how your program thinks and decides
The 3 Core Types of Control Flow
The 3 Core Types of Control Flow
Every program in the world is built from just three structures:
Sequence (straight execution)
Selection (decision making)
Iteration (repetition / loops)
Master these = you can solve any problem
SEQUENCE (Straight Flow)
Instructions are executed one after another from top to bottom
Example 1
START
INPUT A, B
SUM = A + B
OUTPUT SUM
END
How it flows:
1 → 2 → 3 → 4 → End
No skipping, no decision, no repetition
SEQUENCE (Straight Flow)
START
INPUT score1, score2
TOTAL = score1 + score2
AVERAGE = TOTAL / 2
OUTPUT AVERAGE
END
Key Point
Sequence is the default flow
Everything else modifies it
Selection
The algorithm chooses between alternatives
Types of Selection
IF (single condition)
IF–ELSE (two options)
IF–ELSE IF (multiple options)
Nested IF
Selection
1. IF (Single Condition)
Executes a block only if the condition is TRUE
START
INPUT age
IF age >= 18 THEN
OUTPUT "Adult"
END IF
END
No alternative path is defined.
Selection
2. IF–ELSE (Two Options)
Chooses between two path
START
INPUT number
IF number % 2 == 0 THEN
OUTPUT "Even"
ELSE
OUTPUT "Odd"
END IF
END
Exactly one block executes.
Selection
3. IF–ELSE IF (Multiple Options)
Used when there are many conditions
START
INPUT score
IF score >= 70 THEN
OUTPUT "A"
ELSE IF score >= 60 THEN
OUTPUT "B"
ELSE IF score >= 50 THEN
OUTPUT "C"
ELSE
OUTPUT "Fail"
END IF
END
Selection
4. NESTED IF (Very Important)
What is Nested IF?
An IF statement placed inside another IF statement
It allows multi-level decision making
Selection
Why Nested IF is Needed
Some problems require:
Multiple related conditions
Hierarchical decisions
Example:
First check eligibility
Then check another condition inside it
Selection
START
INPUT username, password
IF username == "admin" THEN
IF password == "1234" THEN
OUTPUT "Access Granted"
ELSE
OUTPUT "Wrong Password"
END IF
ELSE
OUTPUT "Invalid Username"
END IF
END
Selection
Type Structure Use Case
IF One condition Simple check
IF–ELSE Two outcomes Binary decision
IF–ELSE IF Multiple conditions Grading, classification
Nested IF Decision within decision Complex logic
ITERATION (Loops / Repetition)
Repeating a block of code multiple times
Types of Loops
FOR loop → fixed number of times
WHILE loop → depends on condition
DO WHILE → runs at least once
ITERATION (Loops / Repetition)
Example 1: Print Numbers 1–5
START
FOR i = 1 TO 5
OUTPUT i
END FOR
END
ITERATION (Loops / Repetition)
Example 2: Sum of 5 Numbers
START
SUM = 0
FOR i = 1 TO 5
INPUT num
SUM = SUM + num
END FOR
OUTPUT SUM
END
ITERATION (Loops / Repetition)
Example: Count Down
START
INPUT n
WHILE n > 0 DO
OUTPUT n
n=n-1
END WHILE
END
ITERATION (Loops / Repetition)
DO WHILE LOOP
START
DO
INPUT number
WHILE number < 0
END
ITERATION (Loops / Repetition)
Critical Differences
Loop Type When it stops
FOR After fixed count
WHILE When condition becomes false
DO-WHILE Runs first, then checks
COMBINING CONTROL FLOWS
Example: Find Even Numbers from 1–10
START
FOR i = 1 TO 10
IF i % 2 == 0 THEN
OUTPUT i
END IF
END FOR
END
COMBINING CONTROL FLOWS
Example: Student Pass Count
START
count = 0
FOR i = 1 TO 5
INPUT score
IF score >= 50 THEN
count = count + 1
END IF
END FOR
OUTPUT count
END
Unit 3.5: Dry Run and Algorithm Testing
Manual simulation of algorithm execution using sample
data.
Why Students Fail Here
They:
skip testing
assume correctness
This leads to:
logical errors
wrong outputs
Example 1
INPUT A = 2, B = 5, C = 3
MAX = A
IF B > MAX then
MAX = B
End if
IF C > MAX then
MAX = C
End if
OUTPUT MAX
Step-by-step Table
Step MAX
Start 2
Compare B (5) 5
Compare C (3) 5
Output → 5
What Dry Run Reveals
➢Logic errors
➢Missing steps
➢Wrong conditions
FINAL CRITICAL SUMMARY
What you must truly understand
• Algorithm = logical blueprint
• Pseudocode = structured expression of logic
• Dry run = verification tool