2.3.3 Representing Algorithms (Flowcharts, Pseudocode)
2.3.3 Representing Algorithms (Flowcharts, Pseudocode)
Once we understand what a good algorithm is, the next step is to figure out how to write it down so that both humans and computers can understand it. Since algorithms are like recipes or plans, we need special ways to show their steps clearly. Two common ways to represent algorithms are flowcharts and pseudocode.
Flowcharts
Imagine drawing a map for a set of instructions. That's a bit like a flowchart! A flowchart is a diagram that uses special shapes to show the steps of an algorithm and arrows to show the order in which those steps happen. Each shape has a specific meaning:
- Ovals (Start/End): These shapes are used at the very beginning and very end of the algorithm. They tell you where the process starts and finishes.
- Rectangles (Process): These boxes show a specific action or task that needs to be done. For example, "Add two numbers" or "Calculate total."
- Diamonds (Decision): These shapes are used when the algorithm needs to make a choice. There's usually a question inside the diamond, and arrows lead out in different directions based on whether the answer is "yes" or "no" (or "true" or "false").
- Parallelograms (Input/Output): These shapes show when information is being put into the algorithm (input) or when the algorithm is giving out results (output). For example, "Get user's name" or "Display result."
- Arrows (Flow Lines): These connect all the shapes and show the direction of the flow of the algorithm, from one step to the next.
Flowcharts are good because they are visual. You can quickly see the entire path of the algorithm, including where decisions are made and how the flow might change. However, for very large and complex algorithms, flowcharts can become messy and hard to read.
- Source: GeeksforGeeks. "Flowchart Tutorial." Accessed July 7, 2025. https://www.geeksforgeeks.org/competitive-programming/an-introduction-to-flowcharts/
Pseudocode
Pseudocode is another popular way to represent algorithms, and its name gives you a big hint about what it is: "pseudo" means fake, so pseudocode is like "fake code." It's a way of writing out the steps of an algorithm using a mix of everyday language and some programming-like keywords, but without following the strict rules of any actual programming language.
Think of it as a detailed outline for a computer program. It uses simple English phrases for actions and often includes words like:
START
/END
: To show the beginning and end of the algorithm.READ
/GET
/INPUT
: To get information from the user or a source.PRINT
/DISPLAY
/OUTPUT
: To show results.IF ... THEN ... ELSE
: To show decisions (if something is true, do this; otherwise, do that).WHILE ... DO
/FOR ... END FOR
: To show repetition (do something over and over again as long as a condition is true, or for a certain number of times).
Example of Pseudocode:
START
GET number1
GET number2
SET sum = number1 + number2
DISPLAY sum
END
Pseudocode is very flexible. Because it doesn't follow strict programming rules, it's easier to write quickly and understand for anyone, even those who don't know a specific programming language. It acts as a bridge between a human's idea for an algorithm and the actual computer code that will eventually be written. It helps programmers plan their code before they start writing it in a specific language like Python or Java.
Both flowcharts and pseudocode are valuable tools. Flowcharts are great for visualizing the overall flow and decisions, especially for simpler algorithms. Pseudocode is excellent for planning out the logical steps in a more textual, detailed way, which can then be easily translated into actual computer code. Programmers often use both, starting with a general idea, maybe sketching a small flowchart, and then writing detailed pseudocode before typing out the final program.
Source: GeekforGeeks.org. "What is Pseudocode: A Complete Tutorial." https://www.geeksforgeeks.org/dsa/what-is-pseudocode-a-complete-tutorial/