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

03-Unit2-CS 1 – PROGRAMMING LOGIC FORMULATION

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

03-Unit2-CS 1 – PROGRAMMING LOGIC FORMULATION

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

AN

INTRODUCTION
TO ALGORITHM,
PSEUDOCODE
AND FLOWCHART
In Java, a term used for programming
and algorithm-based fields is referred
to as pseudocode. It allows us to
define the implementation of an
algorithm. In simple words, we can
define it as an algorithm's cooked-up
representation.
In the past decade, the algorithms are
defined with the help of the pseudocode as
they can be interpreted by programmers,
regardless of their programming background
or knowledge. Pseudocode is the false code
or representation of a code that even a
layperson having school-level programming
knowledge can understand.
Algorithm is an organized, logical
sequence of actions or attitudes
towards a particular problem. In order
to solve a problem, a programmer
implements an algorithm. The
algorithm is expressed using natural
verbal but few technical observations.
It is written in the form of annotations
and informational text that is written in
plain English only. Just like
programming languages, it doesn't
have any syntax, so it cannot be
compiled or interpreted by the
compiler.
Advantages of Pseudocode
In order to improve the readability of any approach,
pseudocode plays a very important role.
•In between the program and the algorithm,
Pseudocode work as a bridge. It is treated as a
document so that the developer can understand
the program easily.
•Pseudocode focuses on explaining the working on
each line of the program. Due to this, it is very
easy for the programmer to construct the code.
Disadvantages of Pseudocode
•The visual representation of the programming code
can be easily understood, and the pseudocode doesn't
provide it.
•There is no well-defined format to write the
pseudocode.
•There are no standards available for pseudocode.
Companies use their own standards to write it.
•If we use pseudocode, we need to maintain one more
document for our code.
A simple example in Java
• Problem: Calculate the sum of two numbers.
• Algorithm (Pseudocode):
1. Start
2. Input num1
3. Input num2
4. Add num1 and num2, store the result in sum
5. Display sum
6. End
HOW TO WRITE ALGORITHM AND
PSEUDOCODE
Writing an algorithm and pseudocode using
the Java programming language involves
breaking down a problem into a step-by-step
sequence of instructions that a computer can
follow. Pseudocode is a way to express this
algorithm in a human-readable and high-
level format before actually writing the Java
code.
Let's go through the process with a simple example:
Finding the sum of two numbers.

Step 1: Understand the Problem In this example,


we want to write an algorithm to find the sum of
two numbers.
Step 2: Write the Algorithm An algorithm is a set of
clear and precise instructions to solve a problem.
Here's an algorithm to find the sum of two
numbers:
1.Start
2.Input the first number (num1).
3.Input the second number (num2).
4.Calculate the sum by adding num1 and
num2: sum = num1 + num2.
5.Display the sum.
6.End
Step 3: Write Pseudocode
Pseudocode is an informal way to
represent the algorithm using
plain language. It doesn't follow
the strict syntax of Java, making it
more human-readable.
Here's the pseudocode for the sum of
two numbers:
Step 4: Translate Pseudocode into Java Code Now, let's
translate the pseudocode into actual Java code:
Step 6: Test Your Code After writing your Java
code, test it with various inputs to ensure it
works as expected. Fix any issues or bugs that
you encounter.
Step 7: Optimize and Refactor (If Necessary) If
your code works but can be improved in terms of
readability, efficiency, or maintainability,
consider making optimizations or refactoring.
That's a basic overview of how to
write an algorithm and pseudocode
and then implement it in the Java
programming language.
FLOWCHARTING
Flowcharting is a visual
representation of an algorithm or a
process, and it's not specific to any
programming language, including
Java. Flowcharts use standardized
symbols to represent various
actions, decisions, loops, and
processes within a program.
Flowcharts can help you plan and
visualize the logic of your program
before you start coding. Once you
have a flowchart, you can then
translate it into Java code.
Here's a basic guide on how to create a
flowchart for a Java program and then
implement it in Java:
1. Understand the Problem:
Before you start creating a
flowchart, you should have a clear
understanding of the problem
you're trying to solve with your
Java program.
2. Identify Inputs and Outputs:
Determine what data your
program will take as input and
what it should produce as output.
3. Start/End Symbols: A
flowchart typically begins with a
"Start" symbol and ends with an
"End" symbol.
4. Process Symbols: Use
rectangular boxes to represent
processes or actions in your
program. Each box should
describe what operation is being
performed.
5. Decision Symbols: Use diamond-
shaped symbols to represent
decision points in your program
where you have to make a choice
based on a condition (e.g., if-else
statements).
6. Input/Output Symbols: Use
parallelogram-shaped symbols to
represent input and output
operations (e.g., reading data from
the user or displaying results).
7. Arrows and Flowlines: Use
arrows or flowlines to connect
the symbols and show the flow
of control in your program.
8. Annotations and Comments:
Add comments or annotations to
explain complex logic or provide
additional information.
FLOWCHART
SYMBOLS
Start/End Symbol or
Terminal
•The terminator symbol marks
the starting or ending point of
the system. It usually
contains the word "Start" or
"End."
Process Symbol
•A box can represent a
single step ("add two cups
of flour"), or an entire sub-
process ("make bread")
within a larger process.
Input/Output Symbol
(I/O)
•Represents material or
information entering or
leaving the system, such
as customer order (input)
or a product (output).
Decision Symbol
•A decision or branching
point. Lines representing
different decisions
emerge from different
points of the diamond.
Flowline Symbol:
Shows the process’
direction. Each flowline
connects two blocks.
Annotation / Comment
Symbol:
Indicates additional
information regarding a step
in a process.
Start
Input A
Input B
Get the sum of A and B
Display sum
End
Start
Read A and B
If A > B
Display A is Large
Else
Display B is Large
End
CREATE A PSEUDOCODE AND
FLOWCHART FOR THIS.

ACTIVITY: MAKING A PEANUT BUTTER


AND JELLY SANDWICH
HOW TO MAKE PEANUT BUTTER
AND JELLY SANDWICH
Start
Get two slices of bread
Spread peanut butter on one slice
Spread jelly on the other slice
Press the two slices of bread
together
Serve the sandwich
End

You might also like