Basics of Programming Lecture 03
Basics of Programming Lecture 03
Lecture 03
Unit 3: Apply Pseudocode Concepts in Constructing Algorithms for
Computer Program Solutions
• Definition of pseudocode
• Common pseudocode terminologies and syntax
• Identifying action flows for a given problem
• Representation of conditional and repetitive structures in pseudocode
• Writing pseudocode to represent problem-solving steps
Pseudocode
Pseudocode is a high-level, human-readable description of a computer
program or algorithm.
It uses a mixture of natural language and programming language syntax to
outline the logic of the program without getting bogged down in the
technical details of a specific programming language. Unlike a specific
programming language, pseudocode is independent of syntax rules, making
it easy to read and understand
IF (age>18) THEN
display 'Adult'
ELSE
display 'Child'
END IF
The IF...THEN...ELSE Construct
A program that asks a user for 2 numbers (A, B) and calculates the value of A
divided by B. However, if B is 0, a message is printed that says that division by 0
is not allowed.
START
Integer A, B, C
Get A, B
IF (B ==0) THEN
Display 'Division by 0 is not allowed'
ELSE
C=A/B
Display 'A=', A
Display 'B=', B
Display 'Answer=', C
END IF
STOP
The IF...THEN...ELSE Construct consists of:
• The word IF
• A condition (B = 0, in this example)
• The word THEN, one or more statements called 'the THEN part’
• One or more statements called 'the ELSE part’
• The word END IF indicates the end of the construct.
Also, the statements are indented so that we can see at a glance the structure of
the construct, especially which statements belong to the THEN and ELSE parts.
6. Computer Can Repeat a Group of Actions
When there is a sequence of processing steps that need to be repeated,
two special keywords, WHILE DO and END WHILE, are used in pseudo-
code. The condition for the repetition of a group of actions is
established in the WHILE DO clause, and the actions to be repeated are
listed beneath it.
Also, the statements are indented so that we can see at a glance the
structure of the loop.
Representing the Flow of Actions Using Pseudocode
A well-structured pseudocode ensures:
• Clear input and output steps.
• Logical sequencing of operations.
• Proper use of conditionals and loops.
Identifying the Flow of Actions
When designing an algorithm using pseudocode:
• Identify the input data needed.
• Define the process or calculations required.
• Determine decision-making conditions (if any).
• Decide on looping structures if repetition is needed.
• Specify the expected output.
Rules for Pseudo-code
• Write only one statement per line
• Capitalize initial keyword
• Indent to show hierarchy
• End multi-line structures
• Keep statements, language independent
• Use descriptive names
Meaningful/Descriptive names
The programmer must introduce some unique names, which will be used to
represent the variables in the problem. All names should be meaningful.
Often, a name describes the type of data stored in a particular variable.
For example, number1, number2, and number3 are more meaningful
names for three numbers than A, B, and C.
When more than one word is used in the name of a variable, then
underscores are useful as word separators; for example, sales_tax and
word_count
Do not have a space in a variable name, as a space would signal the end of
the variable name and thus imply that there were two variables.
Example. Income Tax
PSEUDOCODE KEYWORDS
Pseudocode uses specific keywords to structure algorithms in a way that is easy to
read and understand. Below is a list of commonly used pseudocode keywords
along with their meanings and examples.
Keyword Meaning
START / END Marks the beginning and end of an algorithm.
INPUT / READ Accepts input from the user.
OUTPUT / DISPLAY / PRINT Displays output to the user.
SET / ASSIGN Assigns a value to a variable.
IF...THEN...ELSE...ENDIF Defines a decision-making structure.
Keyword Meaning
WHILE...DO...ENDWHILE Defines a loop that executes while a condition is true.
FOR...TO...DO...ENDFOR Defines a counted loop.
REPEAT...UNTIL Defines a loop that runs at least once.
FUNCTION Defines a block of reusable code that returns a value.
PROCEDURE Defines a block of reusable code that does not return a value.
CALL Executes a function or procedure.
DECLARE Declares variables before use.
RETURN Sends a value from a function.
COMMENT (// or /* */) Adds explanations in the code.
Arithmetic Operators used in Pseudo-code.
+ Addition
- Subtraction
* Multiplication
/ Division
= Assignment
Relational Operators Used in Pseudo-code
< Less than
> Greater than
<= Less than or Equal to
>= Greater than or Equal to
<> Or != Not Equal to
== Equal to
How to write Pseudocode Program
The first thing we need to do when we decide to write a program Is to
design the name for the program. Let’s say we want to write a program
to calculate interest;
A good name for the program would be CalculateInterest
Generally syntax:
PROGRAM <ProgramName>:
PROGRAM <ProgramName>:
<Do stuff>
END
Sequence
When we write programs, we assume that the computer executes the program
starting at the beginning and works its way to the end
That is a basic assumption of algorithm design
We call this SEQUENCE
Or General:
IF (<Condition>)
THEN <Statemment>;
ELSE <Statemment>;
ENDIF;
Or to check which number is biggest:
IF (A > B)
THEN Print A + "is bigger";
ELSE Print B + "is bigger";
ENDIF;
Adding a selection statement in the program:
PROGRAM MakeACupOfTea:
Organize everything together;
Plug in kettle;
Put the teabag in a cup;
Put water into kettle;
Wait for the kettle to boil;
Add water to the cup;
Remove the teabag with a spoon;
Add milk;
IF (sugar is required)
THEN add sugar;
ELSE do nothing;
ENDIF;
Serve;
END.
Iteration
• What if we need to tell the computer to keep doing something until
some condition occurs?
• Let’s say we wish to indicate that you need to keep filling the kettle
with water until it is full. We need a loop or Iteration
So we could state that as:
WHILE (Kettle is not full)
DO keep filling the kettle;
ENDWHILE
Or, in general:
WHILE (<CONDITION>)
DO <Statements>;
ENDWHILE
Or to print the numbers 1 to 5:
WHILE (1 < 5)
DO Print A;
A = A + 1;
ENDWHILE
Or as a program:
PROGRAM MakeACupOfTea:
Organize everything together; Plugin kettle;
Put the teabag in a cup;
WHILE (Kettle is not full)
DO keep filling the kettle;
ENDWHILE;
Wait for the kettle to boil;
Add water to the cup;
Remove the teabag with a spoon; Add milk;
IF (sugar is required)
THEN add sugar;
ELSE do nothing;
ENDIF;
Serve;
END.
Benefits of using a loop
Consider the problem of searching for an entry in a phonebook with
only the following conditions:
Get first entry
If this is the required entry
Then, write down the phone number
Else get the next entry
If this is the correct entry
then write a done entry
else get the next entry
if this is the correct entry ……
This could take forever to specify, but there must be a better way to do it
With these 3 constructs, one can specify any algorithm without the use
of 'go to' statements. "These constructs form the basis for structured
programming. Sometimes, we use variations of these.
Examples
So, let’s say we want to express the following algorithm: Read in a
number and print it out
PROGRAM PrintNumber:
Read A;
Print A;
END
Examples
So, let’s say we want to express the following algorithm: Read in a
number and print out double the number.
PROGRAM PrintNumber:
Read A;
B = A * 2;
Print B;
END
Examples
So, let’s say we want to express the following algorithm: Read a
number and check if it is odd or even.
PROGRAM IsOddOrEven:
Read A;
IF (A/2 gives a reminder)
THEN Print “It’s Odd”
ELSE Print “It’s Even”
ENDIF;
END
Problem 01: Algorithm to Find the Largest of Three Numbers
Problem Statement: Write a pseudocode to find the largest number
among three given numbers.
Flow of Actions:
1. Start
2. Input three numbers: A, B, C
3. Compare A, B, and C to find the largest
4. Output the largest number
5. Stop
Pseudocode presentation:
START
READ A, B, C
IF A > B AND A > C THEN
PRINT "A is the largest“
ELSE IF B > A AND B > C THEN
PRINT "B is the largest“
ELSE
PRINT "C is the largest“
ENDIF
END
Or Pseudocode presentation:
BEGIN FindTheLargestOfThreeNumbers:
INPUT A, B, C
IF A > B AND A > C THEN
OUTPUT "A is the largest“
ELSE IF B > A AND B > C THEN
OUTPUT "B is the largest“
ELSE
OUTPUT "C is the largest“
ENDIF
END
Class Activity
Write a pseudocode that allows a user to enter a series of positive
numbers. The user should continue entering numbers until they enter
0, which signals the end of input. After the input ends, the system
should display the largest number entered.