Computer Programming and Fundamentals
Lesson 2: Pseudo Code
Lesson 1: Algorithm
Pseudo Code and Flowcharts
• Two commonly used tools to document program
What is an Algorithm?
logic (the algorithm):
1. Flowcharts
An algorithm is a set of steps to accomplish a task. 2. Pseudocode
• Flowcharts work well for small problems, while
Examples: Pseudocode is used for larger problems.
• Daily Life Example:
o Step 1: Bike to the station
o Step 2: Take the train Pseudocode
o Step 3: Walk to school
• Computer Science Definition:
• When designing a program, the first step is to
o An algorithm is a set of steps for a
decide on a name.
computer program to accomplish a task.
• Example:
o Process:
o If writing a program to calculate interest,
▪ Start with input data
a good name would be:
▪ Perform complex calculations
CalculateInterest
▪ Stop when the answer is found
o Uses CamelCase for naming.
• Importance of Algorithms:
o Algorithms put the science in computer
science. General Pseudocode Structure:
o Knowing good algorithms and when to
apply them helps in writing efficient PROGRAM <ProgramName>:
programs. <Do stuff>
• Characteristics of an Algorithm: END.
o A well-defined procedure that allows a
computer to solve a problem. • Example for calculating interest:
o A sequence of unambiguous
instructions (no room for subjective PROGRAM CalculateInterest:
interpretation). <Do calculations>
END.
SEQUENCE in Pseudocode
• Definition:
o When writing programs, we assume that
the computer executes the program step
by step, from beginning to end.
o This is a fundamental concept in
algorithm design.
o We call this SEQUENCE.
Computer Programming and Fundamentals
IF (sugar is required)
THEN add sugar;
General Structure in Pseudocode: ELSE do nothing;
ENDIF;
Serve;
Statement1;
END.
Statement2;
Statement3;
Statement4;
Statement5; Iteration (Loops)
Statement6;
Statement7;
Statement8;
Example: Printing Numbers 1 to 5
Example: Making a Cup of Tea A = 1;
WHILE (A < 5)
PROGRAM MakeACupOfTea: DO
Organise everything together; Print A;
Plug in kettle; A = A + 1;
Put teabag in cup; ENDWHILE;
Put water into kettle;
Wait for kettle to boil;
This loop prints numbers from 1 to 4 because the condition
Add water to cup;
stops at A<5A < 5A<5.
Remove teabag with spoon/fork;
Add milk and/or sugar;
Serve;
END.
Benefit of Using Loops
SELECTION in Pseudocode Loops help avoid unnecessary repetition. Instead of writing
a long list of steps, we can use a loop to repeat actions until
a condition is met.
• Definition:
o When a program makes a choice based on
a condition, we call this SELECTION.
o Example: Deciding whether to add sugar
to tea. Example: Searching for an entry in a phone book without
loops would look like this:
General Structure in Pseudocode:
Get first entry;
IF (<CONDITION>) If this is the required entry
THEN <Statements>; Then write down phone number;
ELSE <Statements>; Else get next entry;
ENDIF; If this is the correct entry
Then write down entry;
Else get next entry;
Example: Checking the Biggest Number If this is the correct entry
..........
IF (A > B)
THEN Print A + " is bigger";
This process could go on forever.
ELSE Print B + " is bigger";
ENDIF;
Using a loop, we can simplify the process:
Example: Adding a Selection Statement to a Tea-Making Get first entry;
Program Call this entry N;
WHILE N is NOT the required entry
DO
PROGRAM MakeACupOfTea: Get next entry;
Organise everything together; Call this entry N;
Plug in kettle; ENDWHILE;
Put teabag in cup;
Put water into kettle;
Wait for kettle to boil;
Add water to cup;
Remove teabag with spoon/fork;
Add milk;
Computer Programming and Fundamentals
This loop ensures all numbers from 1 to 5 are added, and the
total is printed.
Example: Making a Cup of Tea (Using a Loop in a
Program)
PROGRAM MakeACupOfTea: Checking for a Prime Number
Organise everything together;
Plug in kettle; Understanding Prime Numbers:
Put teabag in cup;
WHILE (Kettle is not full) A prime number is a number only divisible by 1 and itself.
DO
keep filling kettle; • Example: 7 is prime because dividing it by 6, 5, 4,
ENDWHILE; 3, and 2 all give a remainder.
• Example: 9 is not prime because dividing it by 3
Wait for kettle to boil; gives no remainder.
Add water to cup;
Remove teabag with spoon/fork; Algorithm for Checking a Prime Number:
Add milk;
1. Read a number.
IF (sugar is required)
2. Check if it is only divisible by 1 and itself.
THEN
3. If it is divisible by any number between 2 and (N-
add sugar;
ELSE 1) without a remainder, it is not prime.
do nothing; 4. Otherwise, it is prime.
ENDIF;
Serve;
END. Guidelines for Writing Pseudocode
1. Use a Numbered List for Order:
Pseudocode & Iteration (Loops) o This enforces a clear sequence of
operations.
Example 1: Adding Numbers from 1 to 5 o Example: 3.1 comes after 3 but before 4
for substeps.
2. Write Clear, Unambiguous Instructions:
Problem Statement: o Each step must be understandable and
effective.
• Add up the numbers 1 to 5 and print the result. o Ensure completeness, leaving nothing out.
o
Pseudocode: 3. Use Simple English Statements:
o Pseudocode is meant for humans, not
PROGRAM PrintSum1to5: computers.
o It should be readable by anyone with
Total = 0; basic programming knowledge.
4. Translate Pseudocode into a Programming
Language:
A = 1; o Example: Pseudocode can be converted
into C++, Python, or Java for execution.
WHILE (A != 6)
DO
Total = Total + A;
A = A + 1;
ENDWHILE:
Print Total;
END.
Computer Programming and Fundamentals
Flowchart Purpose:
A flowchart:
Lesson 3: Flowcharting
• Shows the logic of an algorithm.
• Emphasizes individual steps and their
interconnections.
What is a Flowchart? • Illustrates the control flow from one action to the
next.
A flowchart is a graphical representation of a sequence of
operations.
Definitions: Basic Flowchart Symbols
• General Definition (Dictionary): Terminals (Start/End Points):
A schematic representation of a sequence of
operations, as seen in a manufacturing process or • Symbol: Rounded rectangles (often called ovals).
computer program. • Use: Represent the starting and ending points of
• Technical Definition: the program or process.
A graphical representation of the sequence of
operations in an information system or program.
Types of Flowcharts:
1. Information System Flowcharts
o Show how data flows from source
documents through a computer to final
distribution to users.
2. Program Flowcharts
o Show the sequence of instructions in a
single program or subroutine.
o Use different symbols to represent
various steps. Input/Output Operations
• Symbol: Parallelograms
• Use: Represent input or output operations.
Purpose of a Flowchart: o Input: For example, reading user input or
retrieving data.
o Output: For example, displaying results
A flowchart helps visualize the logic of an algorithm by:
or printing messages.
Emphasizing individual steps in a process.
Showing how steps interconnect in a system.
Clearly illustrating the control flow from one action to
the next.
Computer Programming and Fundamentals
Decision Structure
Processes • One of two possible actions is taken based on a
condition.
• Symbol: Rectangles • A diamond symbol is used to represent a yes/no
• Use: Represent a process such as: question.
o Mathematical computations (e.g., o If the answer is yes, the flow follows one
addition, subtraction). path.
o Variable assignments (e.g., setting a o If the answer is no, the flow follows
variable value). another path.
• In this flowchart segment, the question “Is x < y?”
is asked.
• If the answer is no, Process A is performed.
• If the answer is yes, Process B is performed.
This structure illustrates how the flowchart diverges based
on the outcome of the decision.
Four Flowchart Structures
Sequence Structure
• A series of actions are performed in a specific Repetition Structure
order.
• Example: A pay-calculating flowchart follows a • A repetition structure represents part of the
sequence of steps, where each action occurs one program that repeats.
after the other. • This type of structure is commonly known as a
loop.
Loops are used when certain actions need to be repeated
based on specific conditions.
• The diamond symbol is used in loops to test a
condition.
• If the condition is true, an action is performed.
• After the action, the condition is tested again.
• If the condition still holds true, the action is
repeated.
• This process continues until the condition no
longer exists.
Computer Programming and Fundamentals
This loop structure ensures that actions are repeated until the Case Structure
condition is no longer met.
• In a case structure, one of several possible actions
• The action performed by a repetition structure is taken, depending on the contents of a variable.
must eventually cause the loop to terminate. • This structure allows for multiple branches based
• If this doesn’t happen, an infinite loop will be on the value of the variable, often used when there
created. are multiple conditions to check.
• In the flowchart segment below, x is never
changed. Once the loop starts, it will never end.
• The structure below indicates the actions to perform
• Answer: The flowchart can be modified by adding depending on the value in years employed.
an action within the repetition that changes the
value of x. • Based on the value of the variable years employed, the
• This ensures that the condition will eventually fail, program will take different actions, allowing for different
causing the loop to terminate. branches of execution.
• This type of structure is known as a pre-test repetition
structure.
• In this structure, the condition is tested before any
actions are performed.
- If the condition is true, the action is
carried out. If false, the loop terminates
• This flowchart segment shows a post-test
repetition structure.
• In this structure, the condition is tested after the
actions are performed.
• A post-test repetition structure always performs
its actions at least once before testing the
condition.
Computer Programming and Fundamentals