What is a program?
An algorithm is a step-by-step plan to solve a problem. A program is that plan written in a language a computer understands, such as Python, Scratch, Java or C. The computer does exactly what the program says, nothing more.
Most programs follow input → process → output: take data in, work on it, give a result. Before coding, plan with pseudocode (plain-English steps) or a flowchart.
Sequence means instructions run in order, from top to bottom. Changing the order can change the result.
Variables and data types
A variable is a named storage place for a value. score = 0 is an assignment: it puts 0 in the box called score. score = score + 5 reads the old value, adds 5, and stores the new value.
Common data types:
- Integer: whole numbers, e.g. 42
- Real / float: decimals, e.g. 3.5
- String: text, e.g. "Asha"
- Boolean: True or False
A constant is a value that does not change while the program runs, like the number of days in a week. Good names (total_marks, not x) make code easy to read.
Selection and iteration
Selection (if / else)
A program checks a condition that is either True or False, and runs one branch.
if marks >= 33:
print("Pass")
else:
print("Try again")Conditions use comparison operators (==, !=, <, >, <=, >=) and logical operators (and, or, not).
Iteration (loops)
- Count-controlled (for): repeats a set number of times.
for i in range(1, 5):runs with i = 1, 2, 3, 4. - Condition-controlled (while): repeats while a condition is True. It must change something inside, or it loops for ever.
Functions and modular design
A function (also called a subprogram, procedure or method) is a named block of code. You define it once and call it whenever you need it.
def area(length, width):
return length * width
print(area(4, 3)) # 12- Parameters (length, width) are inputs to the function; the values passed in (4, 3) are arguments.
- return sends a result back.
- A variable made inside a function is local; it is not seen outside.
Modular design means breaking a big problem into small parts (decomposition), each written and tested as its own function. Benefits: less repeated code, easier testing, teamwork, and one fix updates every use.
Testing, debugging and code maintenance
Three kinds of errors:
- Syntax error: breaks the language rules (missing colon or bracket). The program will not run.
- Runtime error: happens while running, e.g. dividing by zero.
- Logic error: the program runs but gives the wrong answer, e.g. a loop that runs one time too few (off-by-one).
Testing uses normal data, boundary data (on the edge, like exactly 33 marks) and invalid data (like -5 marks). A trace table records each variable's value line by line, to find where things go wrong.
Maintainable code has meaningful names, comments explaining why, consistent indentation, small functions, and no copy-pasted blocks. Programmers also use version control to keep a history of changes.
Try it: unplugged robot
Ask a friend to be the robot. Write instructions on paper using only: step, turn left, turn right, repeat n and if wall: turn. Guide them from the door to a chair. Did the robot do exactly what you wrote? Fix any bugs and try again. Then open the 3D free play and predict where the robot stops before you press anything.
Key formulas and definitions
- Three building blocks: Sequence, Selection (if/else), Iteration (for/while)
- Assignment: variable = expression (right side is worked out first)
- for i in range(a, b) runs b − a times (a, a+1, …, b−1)
- Function: def name(parameters): … return result
Worked examples
1. x = 4, then x = x * 2, then x = x - 3. What is x?
Line by line: x = 4 → x = 8 → x = 5. So x = 5.
2. What does this print? for i in range(1, 4): print(i * 10)
i takes 1, 2, 3, so it prints 10, 20, 30.
3. age = 15. What does this print? if age >= 18: print("Adult") else: print("Minor")
15 >= 18 is False, so the else branch runs: Minor.
4. Write a function that returns the bigger of two numbers.
def bigger(a, b): if a > b: return a else: return b
5. Trace: total = 0; for n in range(1, 4): total = total + n. What is total at the end?
n = 1 → total 1; n = 2 → total 3; n = 3 → total 6. Final total = 6.
6. A program should print numbers 1 to 5 but uses for i in range(1, 5). What error is this and how do you fix it?
It is a logic (off-by-one) error; it prints only 1 to 4. Fix: range(1, 6).
Common mistakes
- Reading = as "equals" in maths. In code, = means "store the right side in the left side". Use == to compare.
- Forgetting that range(1, 5) stops before 5.
- Writing a while loop whose condition never becomes False, so it runs for ever.
- Copying the same block of code many times instead of writing one function.