Reading history

Algorithms and Flowcharts

4 min read 0 views

Learn what an algorithm is, the standard flowchart symbols, sequence, decision and loop structures, pseudocode, and how to trace an algorithm by hand.

A recipe for khichuri tells you exactly what to do, in what order, until the dish is ready. A computer needs the same thing for every task: a clear, step-by-step method called an algorithm. Before writing a program, programmers often plan the algorithm as a flowchart or in pseudocode. In this lesson you will learn what makes a good algorithm, the standard flowchart symbols, and how to show decisions and loops.

In this lesson you will learn

  • What an algorithm is and the features of a good one
  • The standard flowchart symbols
  • How to show sequence, decisions and loops
  • How to write pseudocode
  • How to trace an algorithm by hand

You should know what software and programs are. No programming experience is needed.

What Is an Algorithm?

An algorithm is a finite set of clear steps that solves a problem. The word comes from the name of al-Khwarizmi, a ninth-century mathematician in Baghdad. A good algorithm has these features:

  • Input: it takes zero or more values to work on.
  • Output: it produces at least one result.
  • Definiteness: every step is clear and has only one meaning.
  • Finiteness: it always stops after a limited number of steps.
  • Effectiveness: every step is simple enough to actually carry out.
Algorithm: find the average of three marks
Step 1: Start
Step 2: Read marks A, B and C
Step 3: Sum = A + B + C
Step 4: Average = Sum ÷ 3
Step 5: Print Average
Step 6: Stop

Flowchart Symbols

SymbolNameUsed for
Rounded rectangle (oval)TerminalStart and Stop
ParallelogramInput/OutputRead a value, print a result
RectangleProcessA calculation or assignment, such as Sum = A + B
DiamondDecisionA yes/no question, with two exits
ArrowFlow lineShows the order of steps
Small circleConnectorJoins parts of a large flowchart

Sequence, Decision and Loop

Every algorithm, however large, is built from just three structures:

  • Sequence: steps done one after another, like the average example above.
  • Decision (selection): choose a path depending on a condition, such as “Is the mark 33 or more?”
  • Loop (repetition): repeat steps while a condition is true, such as adding numbers until a counter reaches 10.

The flowchart below uses all three. It adds up the numbers from 1 to N.

StartRead NSum = 0, i = 1i ≤ N ?Sum = Sum + ii = i + 1Print SumStopYesNoloopShapesoval: start / stopparallelogram: input / outputrectangle: processdiamond: decision

Pseudocode

Pseudocode describes an algorithm in structured plain English. It looks a bit like a program but ignores the strict rules of any real language.

READ N
SET Sum = 0
SET i = 1
WHILE i <= N
    Sum = Sum + i
    i = i + 1
END WHILE
PRINT Sum

A decision looks like this:

READ Mark
IF Mark >= 33 THEN
    PRINT "Pass"
ELSE
    PRINT "Fail"
END IF

Tracing an Algorithm

To check an algorithm, trace it with a small input, writing down each variable after every step. Here is the sum loop with N = 4:

i   i <= 4?   Sum
1     yes      1
2     yes      3
3     yes      6
4     yes     10
5     no      stop → print 10

The answer 10 matches 1 + 2 + 3 + 4, so the algorithm works for this case. Tracing also catches common errors, such as a loop that never ends because the counter is never increased.

Common Mistakes

  1. A decision with only one exit. Every diamond needs a Yes path and a No path.
  2. Forgetting to set starting values. Sum must start at 0 before the loop adds to it.
  3. Infinite loops. Make sure something inside the loop changes so the condition will eventually be false.
  4. Using a rectangle for input. Reading and printing use the parallelogram.

Practice Exercises

Exercise 1. Write steps for an algorithm that reads two numbers and prints the larger one.

Show answer

Start. Read A and B. If A > B, print A; otherwise print B. Stop.

Exercise 2. Which flowchart symbol would you use for “Is the number even?”

Show answer

A diamond (decision), with one arrow labelled Yes and one labelled No.

Exercise 3. Trace the sum loop with N = 3. What is printed?

Show answer

Sum goes 1, 3, 6, then i becomes 4 and the loop stops. It prints 6.

Frequently Asked Questions

What is the difference between an algorithm and a flowchart?

An algorithm is the step-by-step method for solving a problem. A flowchart is one way of drawing that algorithm, using standard symbols joined by arrows. The same algorithm can also be written as numbered steps or pseudocode.

What are the main symbols used in a flowchart?

An oval for start and stop, a parallelogram for input and output, a rectangle for a process, a diamond for a decision, and arrows to show the flow from one step to the next.

Summary

  • An algorithm is a finite, clear, step-by-step method with input and output.
  • Flowcharts use ovals, parallelograms, rectangles, diamonds and arrows.
  • All algorithms are built from sequence, decision and loop.
  • Pseudocode and tracing help plan and test an algorithm before coding it.

Next: Database Basics and MS Access.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *