Вы находитесь на странице: 1из 60

CS112 - PROGRAMMING FUNDAMENTAL

Lecture # 04 – Pseudocode and Flow Chart


Syed Shahrooz Shamim
Junior Lecturer,
CS Department, UIT
Algorithms
How to express an ALGORITHM?
• Algorithms can be expressed in many
kinds of notation, including:
– Natural language
– Pseudo Code
– Flowcharts
– Programming Language
PseudoCode
• Pseudocode (pronounced SOO-doh-kohd) 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. It
allows designers or lead programmers to
express the design in great detail and provides
programmers a detailed template for the next
step of writing code in a specific programming
language.
PseudoCode
• The first thing we do when designing a
program is to decide on a name for the
program.
PseudoCode
• The first thing we do when designing a
program is to decide on a 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 the use of CamelCase.
PseudoCode
• So we start the program as:

PROGRAM CalculateInterest:
PseudoCode
• So we start the program as:

PROGRAM CalculateInterest:

• And in general it’s:

PROGRAM <ProgramName>:
PseudoCode
• Our program will finish with the following:

END.

• And in general it’s the same:

END.
PseudoCode
• So the general structure of all
programs is:

PROGRAM <ProgramName>:
<Do stuff>
END.
SEQUENCE
PseudoCode
• When we write programs, we assume that
the computer executes the program starting
at the beginning and working its way to the
end.
• This is a basic assumption of all algorithm
design.
• We call this SEQUENCE.
PseudoCode
• In Pseudo code it looks like this:

Statement1;
Statement2;
Statement3;
Statement4;
Statement5;
Statement6;
Statement7;
Statement8;
PseudoCode
• For example, for making a cup of tea:

Organise everything together;


Plug in kettle;
Put teabag in cup;
Put water into kettle;
Wait for kettle to boil;
Add water to cup;
Remove teabag with spoon/fork;
Add milk and/or sugar;
Serve;
PseudoCode
• Or as a program:

PROGRAM MakeACupOfTea:
Organise everything together;
Plug in kettle;
Put teabag in cup;
Put water into kettle;
Wait for kettle to boil;
Add water to cup;
Remove teabag with spoon/fork;
Add milk and/or sugar;
Serve;
END.
SELECTION
PseudoCode
• 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.
PseudoCode
• So, we could state this as:

IF (sugar is required)
THEN add sugar;
ELSE don’t add sugar;
ENDIF;
PseudoCode
• Or, in general:

IF (<CONDITION>)
THEN <Statements>;
ELSE <Statements>;
ENDIF;
PseudoCode
• Or to check which number is biggest:

IF (A > B)
THEN Print A + “is bigger”;
ELSE Print B + “is bigger”;
ENDIF;
PseudoCode
• Adding a selection statement in the program:
PROGRAM MakeACupOfTea:
Organise everything together;
Plug in kettle;
Put teabag in cup;
Put water into kettle;
Wait for kettle to boil;
Add water to cup;
Remove teabag with spoon/fork;
Add milk;
IF (sugar is required)
THEN add sugar;
ELSE do nothing;
ENDIF;
Serve;
END.
ITERATION
PseudoCode
• What if we need to tell the computer to keep
doing something until some condition
occurs?
• Let’s say we wish to indicate that the you
need to keep filling the kettle with water
until it is full.
• We need a loop, or ITERATION.
PseudoCode
• Or, in general:

WHILE (<CONDITION>)
DO <Statements>;
ENDWHILE;
PseudoCode
• Or to print out the numbers 1 to 5:

A = 1;
WHILE(A < 5)
DO Print A;
A = A + 1;
ENDWHILE;
PseudoCode
• What is the benefit of using a loop?
PseudoCode
• Consider the problem of searching for an
entry in a phone book with only condition:
Get first entry
If this is the required entry
Then write down phone number
Else get next entry
If this is the correct entry
then write done entry
else get next entry
if this is the correct entry
…………….
PseudoCode
• This could take forever to specify.

• There must be a better way to do it.


PseudoCode
• We may rewrite this as follows:

Get first entry;


Call this entry N;
WHILE N is NOT the required entry
DO Get next entry;
Call this entry N;
ENDWHILE;

This is why we love loops!


PseudoCode
• Or as a program:
PROGRAM MakeACupOfTea:
Organise everything together;
Plug in kettle;
Put teabag in cup;
WHILE (Kettle is not full)
DO keep filling kettle;
ENDWHILE;
Wait for kettle to boil;
Add water to cup;
Remove teabag with spoon/fork;
Add milk;
IF (sugar is required)
THEN add sugar;
ELSE do nothing;
ENDIF;
Serve;
END.
Examples
Pseudocode
• 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.
Pseudocode
• So let’s say we want to express the
following algorithm:
– Read in a number and print it out double the
number.

PROGRAM PrintDoubleNumber:
Read A;
B = A*2;
Print B;
END.
Pseudocode
• So let’s say we want to express the
following algorithm:
– Read in a number, check if it is odd or even.

PROGRAM IsOddOrEven:
Read A;
IF (A/2 gives a remainder)
THEN Print “It’s Odd”;
ELSE Print “It’s Even”;
ENDIF;
END.
Pseudocode
– Read in two numbers, call them A and B. Is A is
bigger than B, print out A, otherwise print out B.

PROGRAM PrintBiggerOfTwo:
Read A;
Read B;
IF (A>B)
THEN Print A;
ELSE Print B;
ENDIF;
END.
Flow Chart
Flow Charts
• A diagrammatic representation that illustrates the sequence of
operations to be performed to get the solution of a problem.

• Generally drawn in the early stages of formulating computer


solutions.

• Facilitate communication between programmers and business


people/end users.

• Once the flowchart is drawn, it becomes easy to write the


program in any high level language.

• Must for the better documentation of a complex program.


Flow Charts
A flow chart can be used to:

• Define and analyse processes.


• Build a step-by-step picture of the process for
analysis, discussion, or communication.
• Define, standardise or find areas for improvement in
a process.
Flow Charts
Symbols for drawing a flowchart:

Start or End of the program


Flow Charts

Input or output operation


Flow Charts

Computational Steps or Processing Function of a


program
Flow Charts

Decision Making and Branching


Flow Charts

Connector or joining of two parts of program


Flow Charts

Magnetic Tape
Flow Charts

Magnetic Disk
Flow Charts
HYBRID

For Display
Flow Charts

Flow lines
Flow Charts

Annotation (foot note)


Flow Charts
Guidelines in flowcharting -

• In drawing a proper flowchart, all necessary


requirements should be listed out in logical order.
• The flowchart should be clear, neat and easy to
follow. There should not be any room for
ambiguity in understanding the flowchart.
• The usual direction of the flow of a procedure or
system is from left to right or top to bottom.
Flow Charts
…Guidelines in flowcharting -
• Only one flow line should come out from a
process symbol.

OR
Flow Charts
…Guidelines in flowcharting -
• Only one flow line should enter a decision
symbol, but two or three flow lines, one for each
possible answer, should leave the decision
symbol.
Flow Charts
…Guidelines in flowcharting –
Only one flow line is used in conjunction with
terminal symbol.

Start Stop/End
Flow Charts
…Guidelines in flowcharting –
Write within standard symbols briefly. As necessary,
you can use the annotation symbol to describe data
or computational steps more clearly.

This is confidential data


Flow Charts
…Guidelines in flowcharting –

• In case of complex flowchart, it is better to use


connector symbols to reduce the number of flow
lines. Avoid the intersection of flow lines.
• Ensure that the flowchart has a logical start and
finish.
• It is useful to test the validity of the flowchart by
passing through it with a simple test data.
Advantages Of Using Flowcharts
• Communication: Flowcharts are better way of
communicating the logic of a system to all concerned.
• Effective analysis: With the help of flowchart, problem can be
analyzed in more effective way.
• Proper documentation: Program flowcharts serve as a good
program documentation, which is needed for various purposes.
• Efficient Coding: The flowcharts act as a guide or blueprint
during the systems analysis and program development phase.
• Proper Debugging: The flowchart helps in debugging
process.
• Efficient Program Maintenance: The maintenance of
operating program becomes easy with the help of flowchart. It
helps the programmer to put efforts more efficiently on that
part
Limitations of using Flowcharts
1. Complex logic: Sometimes, the program logic is
quite complicated.
2. Alterations and Modifications: Alterations may
require re-drawing completely.
3. Reproduction: As the flowchart symbols cannot be
typed, reproduction of flowchart becomes a
problem.
Example - Add three numbers
A program is required to read three numbers,
add them together and print their total.

Defining diagram
Input Processing Output
Number1 Read three Total
Number2 numbers
Number3 Add number
together
Print total
number
Solution
Start

Read
Number1
Number2
number3

Add numbers to total

Print total

Stop
Assignment
• Fill in the blanks-
1. A program flowchart indicates the_________ to be performed and the
__________ in which they occur.
2. A program flowchart is generally read from _____________ to
________________
3. Flowcharting symbols are connected together by means of
___________________
4. A decision symbol may be used in determining the ____________ or
___________ of two data items.
5. __________ are used to join remote portions of a flowchart
6. ____________ connectors are used when a flowchart ends on one page and
begins again on other page
7. A ________ symbol is used at the beginning and end of a flowchart.
8. The flowchart is one of the best ways of ________ a program..
9. To construct a flowchart, one must adhere to prescribed symbols provided by
the __________ .
10. The program uses a ____________ to aid it in drawing flowchart symbols.
Answers
1. Operations, sequence
2. Top, down
3. Flow line
4. Equality, inequality
5. connectors
6. Off -page
7. Terminal
8. documenting
9. ANSI (American National Standards Institute)
10. Flowcharting template

Вам также может понравиться