Skip to main content

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

  1. Variables store data with specific types
  2. Operators: arithmetic, comparison, logical
  3. Control flow: IF, WHILE, FOR loops
  4. Arrays: indexed collections
  5. Subroutines: procedures and functions
  6. File operations: read/write data

Practice Questions

  1. Declare variables for student name, age, grade
  2. Write IF statement for grading
  3. Create WHILE loop to sum numbers
  4. Write function to calculate area of circle
  5. 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