Back to Topics
Term 3 DI10-4

Programming Fundamentals

Learning Objectives

  • Declare and use variables with appropriate data types
  • Write conditional statements (if/else)
  • Implement loops (while, for)
  • Work with basic data structures (arrays/lists)

What is Programming?

Programming is the process of writing instructions that a computer can execute. These instructions are written in a programming language that follows specific syntax rules.

Variables

A variable is a named container that stores a value. Think of it as a labeled box that holds data.

// Declaring variables
name = "Alice"         // String (text)
age = 15               // Integer (whole number)
height = 1.65          // Float (decimal)
isStudent = true       // Boolean (true/false)

// Using variables
OUTPUT "Hello, " + name
OUTPUT "You are " + age + " years old"

Data Types

Type Description Examples
String Text characters "Hello", "ABC123"
Integer Whole numbers 42, -7, 0
Float/Real Decimal numbers 3.14, -0.5
Boolean True or false true, false
Array/List Collection of values [1, 2, 3], ["a", "b"]

Naming Variables

  • Use meaningful names (studentAge not x)
  • Start with a letter (not a number)
  • No spaces (use camelCase or underscores)
  • Case-sensitive (nameName)

Operators

Arithmetic Operators

+    Addition         10 + 5 = 15
-    Subtraction      10 - 5 = 5
*    Multiplication   10 * 5 = 50
/    Division         10 / 5 = 2
%    Modulus (remainder)  10 % 3 = 1

Comparison Operators

==   Equal to            5 == 5  → true
!=   Not equal to        5 != 3  → true
>    Greater than        5 > 3   → true
<    Less than           5 < 3   → false
>=   Greater or equal    5 >= 5  → true
<=   Less or equal       5 <= 3  → false

Logical Operators

AND   Both must be true      true AND true  → true
OR    At least one true      true OR false  → true
NOT   Reverses the value     NOT true       → false

Selection (Conditionals)

Selection allows the program to make decisions and execute different code based on conditions.

// Simple IF
IF age >= 18 THEN
    OUTPUT "You can vote"
ENDIF

// IF-ELSE
IF temperature > 25 THEN
    OUTPUT "Wear shorts"
ELSE
    OUTPUT "Wear pants"
ENDIF

// Nested conditions
IF score >= 80 THEN
    OUTPUT "Distinction"
ELSEIF score >= 50 THEN
    OUTPUT "Pass"
ELSE
    OUTPUT "Fail"
ENDIF

Iteration (Loops)

Iteration repeats a block of code multiple times.

WHILE Loop

Repeats while a condition is true. Checks condition first.

count = 1
WHILE count <= 5
    OUTPUT count
    count = count + 1
ENDWHILE
// Output: 1 2 3 4 5

FOR Loop

Repeats a set number of times.

FOR i = 1 TO 5
    OUTPUT i
NEXT i
// Output: 1 2 3 4 5

// Counting by 2s
FOR i = 0 TO 10 STEP 2
    OUTPUT i
NEXT i
// Output: 0 2 4 6 8 10

When to use which loop?

  • FOR loop - When you know how many times to repeat
  • WHILE loop - When you don't know, and need to check a condition

Arrays (Lists)

An array stores multiple values in a single variable. Each value has an index (position number, starting from 0).

// Creating an array
fruits = ["apple", "banana", "cherry"]

// Accessing elements (index starts at 0)
OUTPUT fruits[0]    // "apple"
OUTPUT fruits[1]    // "banana"
OUTPUT fruits[2]    // "cherry"

// Changing an element
fruits[1] = "blueberry"

// Array length
OUTPUT LENGTH(fruits)   // 3

// Looping through an array
FOR i = 0 TO LENGTH(fruits) - 1
    OUTPUT fruits[i]
NEXT i

// Or using FOR EACH
FOR EACH fruit IN fruits
    OUTPUT fruit
NEXT fruit

Input and Output

// Getting input from user
OUTPUT "Enter your name: "
INPUT name

OUTPUT "Enter your age: "
INPUT age

// Showing output
OUTPUT "Hello, " + name
OUTPUT "Next year you will be " + (age + 1)

String Operations

text = "Hello World"

// Concatenation (joining)
greeting = "Hello" + " " + "World"

// Length
OUTPUT LENGTH(text)         // 11

// Uppercase/Lowercase
OUTPUT UPPER(text)          // "HELLO WORLD"
OUTPUT LOWER(text)          // "hello world"

// Substring
OUTPUT SUBSTRING(text, 0, 5)  // "Hello"

Complete Example: Number Guessing Game

BEGIN NumberGuessingGame
    secretNumber = 7
    guess = 0
    attempts = 0

    OUTPUT "Guess a number between 1 and 10"

    WHILE guess != secretNumber
        INPUT guess
        attempts = attempts + 1

        IF guess < secretNumber THEN
            OUTPUT "Too low! Try again."
        ELSEIF guess > secretNumber THEN
            OUTPUT "Too high! Try again."
        ELSE
            OUTPUT "Correct! You got it in " + attempts + " attempts."
        ENDIF
    ENDWHILE
END

Key Terminology

  • Variable - Named storage for a value
  • Data type - The kind of data (string, integer, etc.)
  • Operator - Symbol for operations (+, -, *, ==, etc.)
  • Conditional - IF statement for making decisions
  • Loop - Repeating code (WHILE, FOR)
  • Array - Collection of values with index positions
  • Index - Position number in an array (starts at 0)
🏠

Project Connection

In Simpson's House...

The Python code on the Raspberry Pi uses every programming fundamental on this page. It's not a toy example — it's a real program running on real hardware, and you can point to a specific line for each concept.

Variables & Data Types

Pin numbers, payloads, and device states

LED_PIN = 17 (integer), payload = msg.payload.decode() (string), is_open = False (boolean) — all three core types in one program.

Selection

String comparison drives every device action

if payload == "ON": GPIO.output(LED_PIN, True) — a direct IF/ELSE using string equality. The entire system hinges on this comparison being exact.

Operators

Arithmetic operators calculate servo duty cycles

The servo angle formula: duty = (angle / 180) * 10 + 2 uses division, multiplication, and addition — arithmetic operators on real hardware values.

Iteration

The program loops forever, waiting for commands

client.loop_forever() is an infinite loop — the program keeps running until interrupted. Without it, the Pi would handle one message and exit.

Explore the full Simpson's House project →