0% found this document useful (0 votes)
0 views

Basics of Programming Lecture 03

The document provides an overview of pseudocode, including its definition, characteristics, and the importance of using it in algorithm design. It outlines the basic operations of computers, how to write pseudocode, and the key constructs such as sequence, selection, and iteration. Additionally, it includes examples and exercises to illustrate the application of pseudocode in solving programming problems.

Uploaded by

athumankasele9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Basics of Programming Lecture 03

The document provides an overview of pseudocode, including its definition, characteristics, and the importance of using it in algorithm design. It outlines the basic operations of computers, how to write pseudocode, and the key constructs such as sequence, selection, and iteration. Additionally, it includes examples and exercises to illustrate the application of pseudocode in solving programming problems.

Uploaded by

athumankasele9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Basics of Programming

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

Key Characteristics of Pseudocode


• Human-Readable
• Structured Format
• Language-agnostic
• Focus on Logic
Why Use Pseudocode?
• Bridges the gap between logic and programming languages.
• Simplifies algorithm design before coding.
• Enhances readability and documentation.
• Reduces logical errors before actual implementation.
How to Write Pseudo-code
1. Understand the problem
2. Breakdown the problem into small steps
3. Write out pseudocode
4. Test pseudo code
5. Translate pseudo code into actual code
How to Write Pseudo-code..
To write pseudocode, we must consider the Six Basic Computer
Operations.
1. A computer can receive information.
2. A computer can output information.
3. A computer can perform arithmetic
4. A computer can assign a value to a variable or memory location
5. A computer can compare two variables and select one of two
alternative actions
6. A computer can repeat a group of actions
1. Computer Can Receive Information
When a computer is required to receive information or input from a
particular source, whether a terminal, a disk, or any other device, the
verbs Read, Input, and Get are used in pseudo-code.

• Read student name


• Get system date
• Read number_l, number_2
• Get tax-code
• Input marks
2. A Computer Can Put Out Information
When a computer is required to supply information or output to a
device, the verbs Print, Write, Put, Output or Display are used in
pseudo-code.

• Print 'Program Completed’.


• Write customer record to master file
• Output name, address and postcode
• Output total-tax
• Display 'End of data'
3. Computer Can Perform Arithmetic
Computers were invented to perform arithmetic. To write a
mathematical calculation or formula either actual mathematical
symbols or the words for those symbols can be used.

• Add number to total


• total = total + number
4. Computer Can Assign a Value to a Variable or
Memory Location
Computers can assign or change the value of a variable. Some common
commands for assignment are SET, =, STORE, INITIALIZE

• Initialize count to zero


• Set student_count to 0
• Total_price = cost_price + VAT
• Store customer-num in lost-customer-num
5. Can Compare Two Variables and Select One of Two
Alternative Actions
Computers can compare two variables and then, based on the comparison,
select one of the two alternative actions.
Special keywords IF, THEN, and ELSE are used to represent this operation in
pseudo-code. If the question in the IF clause evaluates to True, the statements
in the THEN path are executed. Otherwise, the statements in the ELSE path are
executed.

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.

Some common commands for repeat are:


• FOR loop,
• WHILE loop,
• REPEAT UNTIL loop
The While Construct
Get value for C. As long as C is not zero, convert C into F and get
another value for C. When C is zero, stop.
START
Integer C, F
Get C
WHILE (C !=0)DO
F = 32 + (9* C/5)
Display 'Centigrade=‘, C
Display 'Fahrenheit =‘, F
Get C
ENDWHILE
STOP
The While Construct
A WHILE Loop consists of:
• The word WHILE
• A condition (C is not zero, in this example)
• The word DO
• One or more statements (3, in this example)
• The word, END WHILE, indicates the end of the loop

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

Note that we used a PascalCase naming convention.


So, we start a program as:
PROGRAM CalculateInterest:

Generally syntax:
PROGRAM <ProgramName>:

Our program will finish with the following:


END
So the general structure of all programs is:

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

In Pseudocode, it looks like this:


Statement1;
Statement2;
Statement3;

StatementNth;
For example, making a cup of tea.

1. Organize everything together;


2. Plug in kettle;
3. Put the teabag in a cup;
4. Put water into kettle;
5. Wait for kettle to boil;
6. Add water to cup;
7. Remove teabag with the spoon;
8. Serve;
Our 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;
Serve;
END
Selection
• What if we want to make a choice? For example, do we want to add
sugar or not to the tea?
• We call this SELECTION.
So, we could state this as:
IF (sugar is required)
THEN add sugar;
ELSE don't add sugar;
ENDIF;

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

We may rewrite as follows:


Get first entry;
Call this entry N;
WHILE N is NOT the required entry;
DO Get the next entry;
Call this entry N;
ENDWHILE
The Three Essential Programming Constructs.
• Loop (The repetition construct)
• IF ...THEN ...ELSE Construct (The selection construct)
• The Sequence construct

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.

You might also like