Programming Fundamentals
Subject: Computer Science
Topic: 1
Cambridge Code: 0478
Programming Concepts
Algorithm - Step-by-step procedure to solve a problem
Program - Set of instructions in a language computer understands
Variable - Named storage location for data
Data Types
Integer
- Whole numbers: -5, 0, 100
- Size: typically 2 or 4 bytes
- Example: age, count
Float/Real
- Decimal numbers: 3.14, -2.5
- Size: typically 4 or 8 bytes
- Example: temperature, price
String
- Text: "hello", "John Smith"
- Sequence of characters
- Example: names, messages
Boolean
- True or False
- Used in conditions
- Example: isReady, isValid
Character
- Single character: 'A', '5'
- Size: 1 byte
Variables and Assignment
Declaration: Specify type and name
INTEGER age
Assignment: Give variable a value
age ← 25
Initialization: Declare and assign
INTEGER age ← 25
Naming Rules
- Must start with letter or underscore
- Can contain letters, digits, underscores
- Case-sensitive
- Descriptive names (firstName not f1)
Operators
Arithmetic
+, -, *, /, MOD, ^
MOD - Remainder after division
- 17 MOD 5 = 2
DIV - Integer division
- 17 DIV 5 = 3
Comparison
Output: Boolean (True/False)
Logical
AND: Both conditions true OR: At least one condition true NOT: Opposite of condition
Truth Tables:
- TRUE AND TRUE = TRUE
- TRUE OR FALSE = TRUE
- NOT TRUE = FALSE
Control Flow
Selection (IF Statement)
IF condition THEN
statements
ELSE
statements
ENDIF
Iteration (Loops)
WHILE loop:
WHILE condition
statements
ENDWHILE
FOR loop:
FOR counter ← start TO end
statements
ENDFOR
DO-UNTIL loop:
DO
statements
UNTIL condition
Arrays
Array - Collection of same data type
Declaration:
INTEGER numbers[10]
Accessing:
numbers[3] ← 25 // Set element
value ← numbers[3] // Get element
Two-dimensional:
INTEGER matrix[5][5]
matrix[2][3] ← 10
Subroutines
Procedure: Returns no value, performs task
PROCEDURE greet(name)
OUTPUT name
ENDPROCEDURE
Function: Returns a value
FUNCTION add(a, b)
RETURN a + b
ENDFUNCTION
Parameters: Input values Return value: Output of function
File Handling
File Operations:
- OPENFILE (for reading/writing)
- READFILE
- WRITEFILE
- CLOSEFILE
OPENFILE "data.txt" FOR READ
READFILE "data.txt", variable
WRITEFILE "output.txt", variable
CLOSEFILE "data.txt"
Key Points
- Variables store data with specific types
- Operators: arithmetic, comparison, logical
- Control flow: IF, WHILE, FOR loops
- Arrays: indexed collections
- Subroutines: procedures and functions
- File operations: read/write data
Practice Questions
- Declare variables for student name, age, grade
- Write IF statement for grading
- Create WHILE loop to sum numbers
- Write function to calculate area of circle
- Use arrays to store 5 test scores
Revision Tips
- Learn all operators and syntax
- Practice control flow structures
- Understand array indexing
- Know parameter passing methods
- Practice reading/writing files