Computer Science · Topic 04

Algorithms and Flowcharts

Learn how to describe a solution step by step, represent its logic visually, and evaluate whether it solves a problem effectively.

1. What Is an Algorithm?

An algorithm is a finite, ordered set of clear instructions used to solve a problem or complete a task.

Consider the everyday task of making a cup of tea. Its steps form a simple algorithm:

  1. Fill a kettle with water.
  2. Heat the water.
  3. Place a tea bag in a cup.
  4. Pour hot water into the cup.
  5. Wait for the tea to brew, then serve.

Characteristics of a good algorithm

An algorithm describes the solution logic. A program implements that logic in a programming language.

2. Writing an Algorithm

A useful way to design an algorithm is to identify the input, decide the processing steps, and determine the expected output.

Example: Find the sum of two numbers

Algorithm: AddTwoNumbers

1. Start
2. Read A and B
3. Sum = A + B
4. Display Sum
5. Stop

Example input: A = 7, B = 5

Processing: Sum = 7 + 5 = 12

Output: 12

Example: Find the larger of two numbers

1. Start
2. Read A and B
3. If A > B, display A
4. Otherwise, if B > A, display B
5. Otherwise, display "Both are equal"
6. Stop

3. What Is Pseudocode?

Pseudocode expresses an algorithm in a structured, readable form that resembles programming code without following the exact syntax of any particular language.

INPUT marks

IF marks >= 40 THEN
    OUTPUT "Pass"
ELSE
    OUTPUT "Fail"
END IF

Pseudocode helps you plan the logic before writing actual code.

Pseudocode versus a programming language

Pseudocode Programming language
Uses flexible, readable conventions. Follows defined syntax and rules.
Usually cannot be executed directly. Can be executed or compiled using appropriate tools.
Focuses on planning the solution. Turns the solution into an executable program.

4. What Is a Flowchart?

A flowchart is a visual representation of an algorithm. It uses standard symbols connected by arrows to show the order of operations and the direction of control.

Terminator — Oval Represents the Start or End of a process.
Process — Rectangle Represents a calculation or instruction, such as Sum = A + B.
Input / Output — Parallelogram Represents reading input or displaying output.
Decision — Diamond Represents a condition with branches, often Yes/No or True/False.
Flowline — Arrow Shows the direction and sequence of execution.
Connector — Small Circle Connects separated parts of a flowchart when needed.
The symbol's shape communicates its purpose. For example, a decision diamond asks a question, while a process rectangle performs an action.

5. A Simple Flowchart in Text

This example checks whether a number is positive. The following text diagram shows the flow; it is a simplified representation rather than a drawing of the standard shapes.

          [START]
             |
        [Input number]
             |
       [Number > 0?]
          /       \
        Yes        No
         |          |
 [Display Positive] [Display Not Positive]
          \        /
             |
           [END]

A decision creates different possible paths. Both paths can later join again before the algorithm ends.

6. Three Fundamental Control Structures

Sequence

Instructions run one after another in a specified order.

Read length
Read width
area = length * width
Display area

Selection

A condition determines which path the algorithm follows. Common constructs include IF, ELSE, and SWITCH.

IF temperature > 30 THEN
    OUTPUT "Hot"
ELSE
    OUTPUT "Not hot"
END IF

Iteration

A set of instructions repeats while a condition holds or for a specified number of times. Loops include FOR, WHILE, and REPEAT-UNTIL.

FOR number = 1 TO 5
    OUTPUT number
END FOR

The output is 1, 2, 3, 4, 5.

7. Tracing an Algorithm

Tracing means following an algorithm step by step while keeping track of how its variables and output change.

total = 0

FOR i = 1 TO 4
    total = total + i
END FOR

OUTPUT total
Iteration i total after addition
111
223
336
4410

Final output: 10

Tracing is especially useful when checking loops, conditions, and unexpected results.

8. Correctness and Efficiency

An algorithm should solve the intended problem correctly and use resources sensibly.

Two common examples of time complexity are:

These are simplified introductions to Big-O notation. Actual running time also depends on implementation, hardware, and input characteristics.

9. Knowledge Check

Try answering each question before revealing the answer.

1. What is an algorithm?

A finite, ordered set of clear instructions for solving a problem or completing a task.

2. Which flowchart symbol represents a decision?

A diamond, usually with outgoing branches for possible outcomes.

3. Which control structure repeats instructions?

Iteration, commonly implemented with loops.

4. What does pseudocode help a programmer do?

Plan and communicate an algorithm's logic without relying on the exact syntax of a programming language.

5. What is the final value of total?

total = 2
total = total * 3
total = total + 4
10. First, 2 × 3 = 6; then 6 + 4 = 10.

Key Takeaways