Theoretical Contents
Definitions and examples for the assessed topics: block programming (Google Blockly), algorithmic logic and data / label review and segmentation in the context of deep learning (dog · cat · none).
🎓 Designed for Secondary Education — Sciences and Technologies (ages 15–18). No previous programming experience is required.
1. Block Programming (Google Blockly)
Related to: Q3, Q4, Q6, Q11 in the assessment and the block questions in practice.
A block program has three kinds of pieces:
- Instruction blocks — do something (set a variable, print, repeat, decide).
- Value blocks — produce a value (number, text, variable, operation).
- Body blocks — fit inside a loop or condition (the “body”).
Main blocks
| Category | Block | What it does |
|---|---|---|
| Variables | set x to … / x | Stores a value in a variable / reads it. |
| Math | + − × ÷, remainder of a ÷ b | Arithmetic; the remainder (mod) tests parity. |
| Logic | = ≠ < ≤ > ≥, and, or, not | Comparisons and logic operations (true/false). |
| Conditions | if … then … else … | Runs one path or another. |
| Loops | repeat N times, for i from … to …, while … | Repeats instructions. |
| Lists | length of list, get item #i | Goes through and accesses elements (index starts at 1). |
| Text | print … | Shows the result in the console. |
The “if” block holds a condition and a body. The “for” block creates a loop variable that changes on each repetition. Values go into the slots and bodies stay inside the block.
Example 1 — add the numbers from 1 to 5
soma starts at 0. The loop goes through i = 1, 2, 3, 4, 5 and on each
repetition does soma ← soma + i. At the end it prints 15
(1 + 2 + 3 + 4 + 5). soma is an accumulator.
Example 2 — count the even numbers in a list
item mod 2 = 0 is true only for even numbers. When it is true,
the counter increases by 1. At the end, it prints the count.
Example 3 — find the largest value
maior starts with the first element. The loop compares each element with
maior; if it finds a larger value, it updates the variable. At the end, it prints the maximum.
Example 4 — add the values above a threshold
value > threshold decides whether the value
enters the accumulator. Values equal to or below the threshold are ignored. This pattern is used in
data analysis (e.g. adding only detections with confidence above a threshold).
Common errors
| Error | Example | How to fix it |
|---|---|---|
| Inverted condition | Wanting evens but testing mod 2 = 1 | Test mod 2 = 0. |
| Uninitialised accumulator | Adding without soma ← 0 | Initialise before the loop. |
| Infinite loop | while i > 0 without changing i | Update the variable inside the loop. |
| Off-by-one error | for i from 0 to 4 = 5 iterations | Count both ends. |
| Index out of bounds | Using #0 or #6 on a list of 5 | Use 1…length. |
| Empty input / loose block | A condition with no value connected | Connect a value block to the slot. |
Error example — inverted condition
i mod 2 = 1 selects the odd numbers.
From 1…10, the program prints 25 (1+3+5+7+9) instead of 30 (2+4+6+8+10). To fix it,
use i mod 2 = 0.
print block and test both test cases.
2. Algorithmic Logic
Related to: Q1, Q2, Q5, Q10 in the assessment and the logic questions in practice.
x ← 5) stores a value and x ← x + 1 uses the current value and updates it.
Operators
| Type | Operators | Result |
|---|---|---|
| Arithmetic | + − × ÷, mod (remainder) | A number. |
| Comparison | = ≠ < ≤ > ≥ | True or false. |
| Logic | and, or, not | True or false. |
7 mod 2 = 1 (7 is odd); 8 mod 2 = 0 (8 is even). mod is the
usual way to test parity.
Control structures
- Sequence — instructions run in the order written.
- Decision —
if condition then … else …. - Repetition —
for(known number of times),while(while the condition is true) andrepeat N times.
Accumulators and counters
accumulator ← accumulator + value. E.g. the sum of a list.
counter ← counter + 1 when the condition is true.
Tracing
To predict the result, track the values in a table. Example for the loop that adds 1 to 5:
i | soma before | soma after |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 1 | 3 |
| 3 | 3 | 6 |
| 4 | 6 | 10 |
| 5 | 10 | 15 |
Loop termination
while loop, the variable used in the condition must be updated. If the condition
is always true (e.g. i = i), the loop never ends. To count from 10 down to 1, use
while i > 0 and i ← i − 1.
Example — swapping two variables
b ← 5
a ← b # a becomes 5
b ← a # b becomes 5 (the 3 is lost!)
a = 5, b = 5. To swap correctly you need a temporary variable:
temp ← a; a ← b; b ← temp → a = 5, b = 3.
3. Data and AI Segmentation
Related to: Q7, Q8, Q9 in the assessment and the data questions in practice.
Image basics
Tasks
| Task | Output | Example |
|---|---|---|
| Classification | One class for the whole image | “dog”, “cat” or “none” |
| Detection | Boxes + class | A box around the dog |
| Segmentation | One mask per object/class (pixel by pixel) | The exact outline of the cat |
Segments.ai is an image annotation and review platform used to create training datasets (classification, detection and segmentation).
Probabilities and decision threshold
A classifier returns probabilities (a softmax output) that add up to 1. Example:
False positives and false negatives
When we compare the prediction with reality, each case is TP, TN, FP or FN. The two errors are:
- Classification: in an image with no dog, the model predicts «dog».
- Detection: a box appears where there is no object (or one box too many).
- Segmentation: the mask includes background pixels (over-segmentation) → false-positive pixels.
- Classification: in an image with a dog, the model predicts «none».
- Detection: a real dog receives no box.
- Segmentation: the mask is too small (under-segmentation) and leaves object pixels out.
False Negative = said Negative (no), but it was Positive (yes) → missed it.
| Reality | Prediction | Result |
|---|---|---|
| No dog | «dog» | FP — false alarm |
| Dog present | «none» | FN — missed |
| Dog present | «dog» | TP — correct |
| No dog | «none» | TN — correct |
| Cat present | «dog» | FP for «dog» and FN for «cat» (wrong class) |
Metrics
Compare the predictions with the reference. The confusion matrix has four cases:
| Prediction: positive | Prediction: negative | |
|---|---|---|
| Reality: positive | TP (true positive) | FN (false negative) |
| Reality: negative | FP (false positive) | TN (true negative) |
Precision
Of the detections the model called “positive”, how many were correct. Penalises false positives.
Recall
Of the real objects, how many were found. Penalises false negatives.
IoU
Overlap between the predicted mask and the reference (segmentation). 1 = perfect.
F1
Balance between Precision and Recall.
Precision = 6 / (6 + 4) = 0.60 · Recall = 6 / (6 + 2) = 0.75 · F1 ≈ 0.67.
Label / segmentation errors
| Error | What happens | Affected metric | Decision |
|---|---|---|---|
| Over-segmentation | Mask larger than the object; includes background | Precision ↓, IoU ↓ (FP) | Correct the outline |
| Under-segmentation | Mask smaller; leaves parts out | Recall ↓, IoU ↓ (FN) | Expand the mask |
| Wrong class | Cat labelled as dog | Learns the wrong association | Correct the class |
| Imprecise outline | Irregular / shifted edges | IoU ↓ | Refine the outline |
| Missing object | Object not annotated | Recall ↓ (FN) | Annotate the object |
- Validate — the annotation is correct and can go into training.
- Correct — there are small errors (class, outline, missing object) that can be repaired.
- Reject — the sample is ambiguous, unrecoverable or inconsistent with the dataset criteria.
Review checklist
- Confirm the object class.
- Check that the mask covers the object without including background.
- Look for unannotated objects.
- Be consistent with edges across images.
- Record the decision: validate, correct or reject.
4. Glossary
- Algorithm
- A finite sequence of instructions that solves a problem.
- Variable
- A name that stores a value which can change.
- Accumulator
- A variable that adds values; starts at 0.
- Counter
- A variable that counts occurrences; starts at 0 and increases by 1.
- Condition
- An expression that evaluates to true or false.
- Loop
- A structure that repeats instructions (for, while, repeat).
- Termination
- The guarantee that a loop ends.
- Blockly
- A visual block-programming library.
- List
- A sequence of values; in Blockly indexes start at 1.
- Tracing
- Tracking values step by step.
- Pixel
- The smallest unit of a digital image; a point with a colour value.
- Digital image
- A rectangular grid of pixels.
- Resolution
- The number of pixels (width × height) in an image.
- Mask
- An image where each pixel indicates the class/object (segmentation).
- Bounding box
- A rectangle (x, y, width, height) around an object (detection).
- RGB channel
- A colour component (red, green, blue) of a pixel.
- Classification
- Assigning one class to the whole image.
- Detection
- Locating objects with boxes and a class.
- Segmentation
- Classifying each pixel and producing masks.
- Label
- The reference annotation (ground truth).
- Softmax
- A function that turns scores into probabilities that add up to 1.
- Threshold
- The minimum confidence to accept a decision.
- TP / TN / FP / FN
- True/false positives and negatives in the confusion matrix.
- False positive (FP)
- The model predicted positive where there was no object (false alarm); lowers Precision.
- False negative (FN)
- The model predicted negative where there was an object (missed it); lowers Recall.
- Precision
- TP / (TP + FP): quality of positive detections.
- Recall
- TP / (TP + FN): coverage of real positives.
- IoU
- Intersection / union between masks.
- Over-segmentation
- Mask larger than the object (false positives).
- Under-segmentation
- Mask smaller than the object (false negatives).
- Ground truth
- The reference annotation considered correct.
- Loss
- The model error during training; high values indicate difficulty.
Keep studying
Read the theory, practise with immediate feedback and then take the assessment.