Vous êtes sur la page 1sur 15

PROGRAM LOGIC FORMULATION

ALGORITHM
PSEUDOCODE
FLOWCHART
PROBLEM SOLVING USING ALGORITHMS

What is an Algorithm?
Your favourite app, the online game that you always play, the
document you type, the spreadsheet that you use to compute
and analyze data, the GPS in your car. You may not be aware
of it, but these computer programs wont be able to perform
their specific task without creating an algorithm.

ALGORITHM
You can think of an algorithm as similar to a food recipe. If you
make a sandwich, you follow a set of steps to put the different
ingredients together. You bring ingredients together, assemble
them as you like, and produce a final product - the sandwich. If
you were asked to write down instructions to make a sandwich,
you could create a written algorithm.
PROGRAM LOGIC FORMULATION

Algorithm is a formula or set of steps used in solving a problem. It can be


expressed using:

• Pseudocode - a simple way of describing a set of instructions that does


not have to use specific syntax or strict rules.

• Flowchart- a graphical representation of how a process works. It shows


the sequential steps that are connected using arrows and flow lines.
Each sequence is represented by a shape which symbolizes a process.
PROGRAM LOGIC FORMULATION
PSEUDOCODE
http://mr-west.uk/algorithms/pseudocode/
PROGRAM LOGIC FORMULATION

The basic assumption of all algorithm design is when a programmer


writes a program, it is assumed that the computer executes the
program starting at the beginning and will work its way till the end.
This is called SEQUENCE.

In a pseudocode, the general structure looks like this:

Statement 1;
Statement 2;
Statement 3;
Statement 4;
Statement 5;
Statement 6;
and so on…
PROGRAM LOGIC FORMULATION
But, what if we want to make a choice? What if we want to add sugar
or not the tea? We call this SELECTION.
In a pseudocode, the example looks like this:

IF (sugar is required)
THEN add sugar;
ELSE don't add sugar;
ENDIF;

General structure/format looks like this:

IF (<CONDITION>)
THEN <Statements>;
ELSE <Statements>;
ENDIF;

a pseudocode to check which number is the biggest:

IF (A > B)
THEN Print A + “is bigger”;
ELSE PrintB + “is bigger”;
ENDIF;
PROGRAM LOGIC FORMULATION
PSEUDOCODE THAT SHOWS SELECTION
STATEMENT IN A PROGRAM
PROGRAM LOGIC FORMULATION
What if we need the computer to keep doing something until some condition
occurs? What if we would like to indicate that the kettle should be filled with water
until it is full. In this case, we need a loop or ITERATION.

In a pseudocode, this could be stated as:

WHILE (Kettle is not full)


DO keep filling kettle;
ENDWHILE;

it has a general form:

WHILE (<Condition>)
DO <Statements>;
ENDWHILE;

therefore, a pseudocode to print out the numbers 1 to 5 would be stated as:

A = 1;
WHILE (A < 5)
DO Print A:
A = A + 1;
ENDWHILE;

DISCOVERY: What is the advantage of using loops or iteration? Search the answer
on the Internet.
PROGRAM LOGIC FORMULATION
More examples!
PSEUDOCODE
Problem: Read in a number and print it out
Solution using a pseudocode: Problem: Read in a number and check if
that number is odd or even/
PROGRAM PrintNumber: Solution using a pseudocode:
Read A;
Print A; PROGRAM IsAddOreven:
END. IF (A > B)
THEN Print “It’s Odd”
ELSE Print “It’s Even”
Problem: Read in a number and print it out END.
and double the number
Solution using a pseudocode:
Problem: Print out the bigger of two numbers
PROGRAM PrintDoubleNumber: Solution using a pseudocode:
Read A;
B = A*2 PROGRAM PrintBiggerOfTwo:
Print B; Read A;
END. Read B;
IF (A/2 gives a remainder)
THEN Print A
ELSE Print B
END.
FLOWCHART
represents
FLOWCHARTING algorithm in a
graphical manner.
It uses shapes
ADVANTAGES: and symbols.
• 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 analysed 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 or fixing the errors in the 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

DISADVANTAGES:
• Complex logic: - Sometimes, the program logic is quite complicated. In that case, flowchart
becomes complex and clumsy.
• Alterations and Modifications: - If alterations are required the flowchart may require re-drawing
completely.
• Reproduction: - As the flowchart symbols cannot be typed, reproduction of flowchart becomes a
problem.
FLOWCHART SYMBOLS
FLOWCHART
SYMBOLS
Examples of Flowchart

A flowchart that shows


the process of
displaying the total
price of the items
entered
Examples of Flowchart

A flowchart that shows


the process of
determining if a student
passed or failed a
subject
Examples of Flowchart

A flowchart that shows


the process of
displaying the total
based on the number
that was entered. Each
time a number is
entered, it will be
added to the total. The
total will be displayed 5
times.

You may browse the


internet to research for
other examples of
flowcharts.

Vous aimerez peut-être aussi