IGCSE COMPUTER SCIENCE
ALGORITHM DESIGN & PROBLEM SOLVING – EASY NOTES
1. What an Algorithm Is
An algorithm is a set of step-by-step instructions used to solve a problem.
2. Variables and Data
A variable is a named location in memory used to store a value.
Examples:
- total ← 0 (total is a variable)
- name ← "Ray"
Input: Data the program receives from the user.
Example: INPUT age
Output: Data the program sends out to the user.
Example: OUTPUT "Hello"
Assignment: Giving a variable a value.
Example: count ← count + 1
3. Decision Structure (IF, ELSE, ELSEIF)
IF condition THEN
statements
ELSEIF another condition THEN
statements
ELSE
statements
ENDIF
Explanation:
- IF checks a condition.
- ELSE runs when IF is false.
- ELSEIF adds more conditions.
- ENDIF closes the structure.
4. Loops
A loop repeats code.
A. Count-controlled loop (FOR)
FOR i ← 1 TO 5
OUTPUT i
NEXT i
Explanation:
- i is a variable that counts.
- FOR starts the loop.
- NEXT ends one cycle of the loop.
B. Condition-controlled loop (WHILE)
WHILE answer <> "yes"
INPUT answer
ENDWHILE
Explanation:
- WHILE keeps running while the condition is true.
- Loop stops when the condition becomes false.
5. Common Algorithms
A. Sum of Numbers
sum ← 0
FOR i ← 1 TO 5
sum ← sum + i
NEXT i
Explanation:
- sum stores total.
- i increases each time.
B. Find Largest Number
largest ← numbers[1]
FOR i ← 2 TO 10
IF numbers[i] > largest THEN
largest ← numbers[i]
ENDIF
NEXT i
C. Count Items
count ← 0
FOR i ← 1 TO 10
IF numbers[i] > 0 THEN
count ← count + 1
ENDIF
NEXT i
D. Sentinel Loop (Input until -1)
sum ← 0
count ← 0
INPUT mark
WHILE mark <> -1
sum ← sum + mark
count ← count + 1
INPUT mark
ENDWHILE
6. Trace Tables
Used to test logic.
You make columns for variables and follow each step.
7. Flowchart Shapes
Start/End: Oval
Input/Output: Parallelogram
Process: Rectangle
Decision: Diamond