3. Pseudocode
What is pseudocode
What is algorithm
Advantages of pseudocode
How to write pseudocode
Pseudocode Flow of control
4. Specific Objectives
By the end of this lesson, you should
1. Write pseudo code for a given problem
2. Display teamwork in completing task
3. Cooperate in group work
5. What is pseudocode?
is a detailed yet readable
description of what a computer
program or algorithm must do,
expressed in a formally-styled
natural language rather than in
a programming language.
Pseudocode is sometimes used
as a detailed step in
the process of developing a
program.
6. Application of pseudocode
Textbooks and scientific publications related to computer
science and numerical computation often use pseudocode in
description of algorithms, so that all programmers can
understand them, even if they do not all know the same
programming languages.
A programmer who needs to implement a specific algorithm,
especially an unfamiliar one, will often start with a
pseudocode description, and then "translate" that description
into the target programming language and modify it to
interact correctly with the rest of the program.
7. Syntax of pseudocode
As the name suggests, pseudocode generally does not actually
obey the syntax rules of any particular language; there is no
systematic standard form, although any particular writer will
generally borrow style and syntax; for example, control
structures from some conventional programming language.
8. What is algorithm?
Algorithm: It’s an organized logical sequence of the actions or
the approach towards a particular problem. A programmer
implements an algorithm to solve a problem in a specific
programming language.
Pseudo code: It’s simply an implementation of an algorithm in
the form of annotations and informative text written in plain
English. It has no syntax like any of the programming language
and thus can’t be compiled or interpreted by the computer.
9. Advantages of pseudocode
Improves the readability of any approach. It’s one of the
best approaches to start implementation of an algorithm.
Acts as a bridge between the program and the algorithm or
flowchart. Also works as a rough documentation, so the
program of one developer can be understood easily when a
pseudo code is written out. In industries, the approach of
documentation is essential. And that’s where a pseudo-code
proves vital.
The main goal of a pseudo code is to explain what exactly
each line of a program should do, hence making the code
construction phase easier for the programmer.
10. RULES FOR PSEUDOCODE
1. Write only one statement per line
2. Capitalize initial keyword
3. Indent to show hierarchy
4. End multiline structures
5. Keep statements language independent
11. RULES FOR PSEUDOCODE
1. Write only one
statement per line
Each statement in
your pseudocode
should express just
one action for the
computer. If the task
list is properly drawn,
then in most cases
each task will
correspond to one line
of pseudocode.
1
2
3
4
5
12. RULES FOR PSEUDOCODE
2. Capitalize initial keyword
In the example above, READ
and WRITE are in caps. There
are just a few keywords we
will use:
READ, WRITE, IF, ELSE, ENDIF,
WHILE, ENDWHILE, REPEAT,
UNTIL
Input: READ, OBTAIN, GET
Output: PRINT, DISPLAY, SHOW
Compute: COMPUTE,
CALCULATE, DETERMINE
Initialize: SET, INIT
Add one: INCREMENT, BUMP
13. RULES FOR PSEUDOCODE
3. Indent to show
hierarchy
We will use a particular
indentation pattern in each of
the design structures:
SEQUENCE: keep statements
that are “stacked” in sequence
all starting in the same column.
SELECTION: indent the
statements that fall inside the
selection structure, but not the
keywords that form the selection
LOOPING: indent the statements
that fall inside the loop, but not
the keywords that form the loop
14. RULES FOR PSEUDOCODE
4. End multiline
structures
See how the
IF/ELSE/ENDIF is
constructed above.
The ENDIF (or END
whatever) always is in
line with the IF (or
whatever starts the
structure).
15. RULES FOR PSEUDOCODE
5. Keep staments language independent
Resist the urge to write in whatever language you are most
comfortable with. In the long run, you will save time! There
may be special features available in the language you plan to
eventually write the program in; if you are SURE it will be
written in that language, then you can use the features. If not,
then avoid using the special features.
17. Exercises
1. Create a pseudocode that will compute and display the area of a rectangle
2. Create a pseudocode of the activities that you do when you get up in the morning
3. Create a pseudocode on how to cook a noodles
4. Create a pseudocode on how to compute your age
5. Create a pseudocode on how to computer your grades in highschool
6. Create a pseudocode on how to withdraw money from ATM
7. Create a pseudocode that will computer the sum of 5 numbers and then display the average.
8. Write pseudocode that will compute your salary based on the basic formula (salary = (number of days work * rate per
day) – deduction)
9. Write a java program that will compute the number of respondents using the sloven’s formula. To compute the
number of respondents the user must enter the total population size.
Sloven’s formula:
n = N/(1+(Ne2))
where:
n = number of respondents
N = total population
e = margin of error , 0.05 in this case
20. Selection or Conditional Flow
During algorithm development, we need statements which
evaluate expressions and execute instructions depending on
whether the expression evaluated to True or False.
IF — ELSE IF — ELSE
This is a conditional that is used to provide statements to be
executed if a certain condition is met. This also applies to
multiple conditions and different variables.
Types of Selection
Single Alternative (if statement)
Double Alternative (if else statement)
Multiple Alternative (if else if statement)
24. Basic Math Symbols
When we have to make a
choice between actions,
we almost always base
that choice on a test.
The test uses phrases like
“is less than” or “is equal
to”. There is a
universally accepted set
of symbols used to
represent these phrases:
25. Logical Operators
AND: if any of the conditions
are false, the whole expression
is false.
IF day = “Saturday” AND weather = “sunny”
WRITE “Let’s go to the beach!”
ENDIF
OR: if any of the conditions are
true, the whole expression is
true
IF month = “June” OR month = “July” OR
month = “August”
WRITE “Yahoo! Summer vacation!”
ENDIF
NOT: reverses the outcome of
the expression; true becomes
false, false becomes true.
IF day <> “Saturday” AND day <> “Sunday”
WRITE “Yuk, another work day”
ENDIF
29. Nested if statement
Nested If...Then...
Else statements te
st for multiple
conditions by
placing If...Then...
Else statements ins
ide other If...Then.
..Else statements.
32. Exercises
1. Write a pseudocode that will determine if the number is an odd or even number
2. Write a pseudocode that will print passed if the grade is above 74, failed if 74 and below
3. Write a pseudocde that will determine the highest number between two numbers.
4. Write a pseudocde that converts currency to another currency. It is a peso to dollar, dollar to peso converter program. The final
result will be the converted amount of the currency.
5. Write a pseudocde that will ask the user to enter 1-10 and call that program as PrintNumberInWord which prints "ONE", "TWO",... ,
"NINE", "OTHER" if the int variable "number" is 1, 2,... , 9, or other, respectively. Use (a) a "if.elseif.else" statement
6. Write a java program that will ask the user to enter 2 numbers and will ask what operator to use (1 for addition, 2 for subtraction, 3
for division, and 4 for multiplication). It will also print the result.
7. ATM Machine Pseudocode. Suppose you have a balance of 5000 pesos, create a java program that will ask the user to select the
transaction he/she want to access, w to withdraw (ask the user to input amount to withdraw and output the new balance), d for
deposit (ask the user to input amount to deposit and output the new balance), i for inquiry (display the balance).
8. Writea a pseudocode that will ask the user to enter its grade then display the remarks.
95-100 - excellent
90-94 - very good
85-89 - good
80-84- satisfactory
76-79 - study harder
75 - passed
74 below – failed
33. Iterative Flow
To iterate is to repeat a set of instructions in
order to generate a sequence of outcomes. We
iterate so that we can achieve a certain goal.
Types of Iteration
for loop
while loop
do-while loop
35. Assessment
By the end of this lesson, you should
1. Write pseudo code for a given problem – Quiz and
Exercises
2. Display teamwork in completing task – Project and
Reporting
3. Cooperate in group work - Project and Reporting