You are on page 1of 31

ALGORITHM AND

PSEUDOCODE

Prepared by:
Joven R. Ramos
CPEN 21A – Programming Logic and Design
ALGORITHM AND PSEUDOCODE
 A typical programming task can be divided into
two phases:
 Problem solving phase
 Implementation phase
What is an Algorithm?
 An algorithm is procedure consisting of a finite
set of unambiguous rules (instructions) which
specify a finite sequence of operations that
provides the solution to a problem, or to a
specific class of problems for any allowable set
of input quantities (if there are inputs).
 In other word, an algorithm is a step-by-step
procedure to solve a given problem.
What is a Pseudocode?
 Pseudocode is one of the tools that can be
used to write a preliminary plan that can be
developed into a computer program.
 Pseudocode is a generic way of describing an
algorithm without use of any specific
programming language syntax.
 It is, as the name suggests, pseudocode — it
cannot be executed on a real computer, but it
models and resembles real programming code,
and is written at roughly the same level of detail.
What is a Procedure?
 A procedure is a finite sequence of well-defined
instructions, each of which can be mechanically
carried out in a finite amount of time.
Introduction to Algorithm
 Algorithms for making things will often be divided
into sections:
 The parts/components/ingredients (inputs)
required to accomplish the task
 The actions/steps/methods (processing) to
produce the required outcome (output).
Properties of an Algorithm

 Finiteness
 Absence of Ambiguity
 Sequence of Execution
 Input and Output
 Effectiveness
 Scope of Definition
Why is Pseudocode
Necessary?
How to Write an Algorithm
using Pseudocode
Statements?
 There are six basic computer operations:
1. A computer can receive information
2. A computer can put out information
3. A computer can perform arithmetic
4. A computer can assign a value to a piece of data
5. A computer can compare two piece of information
and select one of two alternative actions
6. A computer can repeat a group of actions
How to Write an Algorithm
using Pseudocode
Statements?
 A computer can receive information:
 Read (information from a file)
 Get (information from the keyboard)
How to Write an Algorithm
using Pseudocode
Statements?
 A computer can put out information:
 Write (information to a file)
 Display or Print (information to the screen)
How to Write an Algorithm
using Pseudocode
Statements?
 A computer can perform arithmetic
 Use actual mathematical symbols or the
words for the symbols
 Example: Add number to total
 Total = total + number

 +, -, *, /, %
 Calculate and Compute also used
How to Write an Algorithm
using Pseudocode
Statements?
 A computer can assign a value to a piece of data
 to give data an initial value: Initialize and Set
 to assign a value as a result of some
processing “=“ or “ “
 x = 5 + y

 x 5+y
 to keep a piece of information for later use
Save and Store
How to Write an Algorithm
using Pseudocode
Statements?
 A computer can compare two pieces of
information and select one of two alternative
actions
IF condition THEN
some action
ELSE
alternative action
ENDIF
How to Write an Algorithm
using Pseudocode
Statements?
 A computer can repeat a group of actions
WHILE condition (is true)
some action
ENDWHILE

FOR a number of times


some action
ENDFOR
Control or Logical Structures
of an Algorithm

 Sequence Structure

 Decision or Selection Structure

 Repetition or Iteration Structure


 Sequence Structure
For example, suppose you are required to design an algorithm for
finding the average of six numbers, and the sum of the numbers is
given. The pseudocode will be as follows:

1. Start
2. Get the value of variable sum
3. Calculate the average:
average = sum / 6
4. Print the average
5. Stop
 Decision or Selection Structure

1. If Statement

2. If-else Statement

3. If-elseif Statement
 Decision or Selection Structure

The conditional operators used in pseudo-code are:

== is equal to
> is greater than
< is less than
>= is greater than or equal
<= is less than or equal
!= is not equal to
 Decision or Selection Structure
A simple use of an if statement is to assign letter grades. Suppose that
scores 90 and above are A’s. Here is one way to do this:

1. Start
2. Get the value of variable grade
3. If grade >= 90 then
Print “A”
End if
4. Stop
 Decision or Selection Structure
A simple use of an if-else statement. Suppose that scores 90 and
above are passing grade, otherwise, failing grade. Here is one way to
do this:

1. Start
2. Get the value of variable grade
3. If grade >= 90 then
Print “Passed!”
Else
Print “Failed!”
End if
4. Stop
 Decision or Selection Structure
A simple use of an if-elseif statement is to assign letter grades.
Suppose that scores 90 and above are A’s, scores in the 80s are B’s,
70s are C’s, 60s are D’s, and anything below 60 is an F. Here is one
way to do this:
1. Start
2. Get the value of variable grade
3. If grade >= 90 then
Print “A”
Else if grade >= 80 then
Print “B”
Else if grade >= 70 then
Print “C”
Else if grade >= 60 then
Print “D”
Else
Print “F”
End if
4. Stop
 Decision or Selection Structure

The compound logical operators used in pseudo-code are:

 Logical AND

 Logical OR

 Logical NOT
 Decision or Selection Structure
The program is to input a examination mark and test it for the award of a grade.
The mark is a whole number between 1 and 100. Grades are awarded
according to the following criteria:
>= 80 Distinction
>= 60 Merit
>= 40 Pass
< 40 fail
1. Start
2. Get the value of variable mark
3. If mark >= 80 then
display “distinction!”
Else If mark >= 60 and mark < 80
display “merit!”
Else If mark >= 40 and mark < 60
display “pass!”
Else If mark < 40
display “fail!”
End if
4. Stop
 Repetition or Iteration Structure

The Repetition structure can be


implemented using:

1. while Statement (Pre-test and Post-test


condition)

2. for Statement
 Repetition or Iteration Structure

The Pre-test condition loop. The syntax is:

WHILE (a condition is true)

A statement or block of statements

ENDWHILE
 Repetition or Iteration Structure

Example: A program segment to print out each character typed at


a keyboard until the character ‘q’ is entered.

1. Start
2. Get the value of variable letter
3. WHILE letter != ‘q’
ACCEPT letter
DISPLAY “The character you typed is”, letter
ENDWHILE
Stop
 Repetition or Iteration Structure

The Post-test condition loop. The syntax is:

REPEAT

A statement or block of statements

UNTIL a true condition


 Repetition or Iteration Structure

Example: A program segment repeatedly asks for entry of a


number in the range 1 to 100 until a valid number is entered.

1. Start
2. REPEAT
Get a value of variable number
3. UNTIL number <1 OR number < 100
4. Stop
 Repetition or Iteration Structure

The FOR Loop. The pseudo-code syntax will be:

FOR variable name in range( number of times to


repeat ):

statements to be repeated

ENDFOR
 Repetition or Iteration Structure

Example: The pseudocode below asks the user for a


number and prints its square, then asks for another number
and prints its square, etc. It does this three times.

1. Start
2. for i in range(3):
Get the value of variable num
square = num * num
Display square
3. End for
4. Stop

You might also like