Theory LDCAI Reference · definitions and examples
PT EN
📚 Practice ← Assessment

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.

Block Programming Algorithmic Logic Data / AI Segmentation
How to use Read the theory, solve the practice questions and use the hints when you get stuck. Then take the assessment — hints are pointers, not solutions.
Curriculum links Mathematics (logic, functions, statistics) · Biology (image analysis, species classification) · Physics/Chemistry (data processing) · ICT (algorithms and artificial intelligence).

1. Block Programming (Google Blockly)

Related to: Q3, Q4, Q6, Q11 in the assessment and the block questions in practice.

Definition Block programming is a visual way of writing programs: instead of text, you fit together blocks that represent instructions and values. Each block has a shape that only fits in the right place, which helps avoid syntax errors. Google Blockly is the library used in the simulator.

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

CategoryBlockWhat it does
Variablesset x to … / xStores a value in a variable / reads it.
Math+ − × ÷, remainder of a ÷ bArithmetic; the remainder (mod) tests parity.
Logic= ≠ < ≤ > ≥, and, or, notComparisons and logic operations (true/false).
Conditionsif … then … else …Runs one path or another.
Loopsrepeat N times, for i from … to …, while …Repeats instructions.
Listslength of list, get item #iGoes through and accesses elements (index starts at 1).
Textprint …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.

List indexes In Blockly, the first element is #1 (not #0). If a list has length 5, valid indexes are 1 to 5. An index outside this range causes an error.

Example 1 — add the numbers from 1 to 5

How it works 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

How it works The loop goes through the list indexes. The condition 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

How it works 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

How it works For each value in the list, the condition 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

ErrorExampleHow to fix it
Inverted conditionWanting evens but testing mod 2 = 1Test mod 2 = 0.
Uninitialised accumulatorAdding without soma ← 0Initialise before the loop.
Infinite loopwhile i > 0 without changing iUpdate the variable inside the loop.
Off-by-one errorfor i from 0 to 4 = 5 iterationsCount both ends.
Index out of boundsUsing #0 or #6 on a list of 5Use 1…length.
Empty input / loose blockA condition with no value connectedConnect a value block to the slot.

Error example — inverted condition

What is wrong? The goal was to add the even numbers, but 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.
Before submitting Use Run, Step by step and Analyze errors. Make sure the program prints the result with the 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.

Definition An algorithm is a finite, ordered sequence of instructions that solves a problem. A variable is a named “box” that stores a value; an assignment (x ← 5) stores a value and x ← x + 1 uses the current value and updates it.

Operators

TypeOperatorsResult
Arithmetic+ − × ÷, mod (remainder)A number.
Comparison= ≠ < ≤ > ≥True or false.
Logicand, or, notTrue 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) and repeat N times.

Accumulators and counters

Accumulator A variable that adds values. It starts at 0 and does accumulator ← accumulator + value. E.g. the sum of a list.
Counter A variable that counts occurrences. It starts at 0 and does 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:

isoma beforesoma after
101
213
336
4610
51015

Loop termination

Guaranteeing termination In a 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

a ← 3
b ← 5
a ← b   # a becomes 5
b ← a   # b becomes 5 (the 3 is lost!)
Result a = 5, b = 5. To swap correctly you need a temporary variable: temp ← a; a ← b; b ← temp → a = 5, b = 3.
Best practices Initialise accumulators/counters; use clear names; guarantee termination; test with concrete values; check that the condition matches the goal (even/odd, greater/less).

3. Data and AI Segmentation

Related to: Q7, Q8, Q9 in the assessment and the data questions in practice.

Definition In deep learning, a model learns from annotated examples (images + labels). The label is the reference “truth” (ground truth). Its quality determines the model's quality.

Image basics

Definition A pixel is the smallest unit of a digital image: a point with a colour value. In a colour image, each pixel combines three channels — red (R), green (G) and blue (B). In a segmentation mask, each pixel has a class (e.g. dog, cat or background).
one pixel
Definition A digital image is a rectangular grid of pixels (width × height). The resolution is the number of pixels; higher resolution means more detail and more computational cost.
Definition A segmentation mask is an image the same size as the original where each pixel indicates the class/object. It is the typical output of a segmentation model.
Definition A bounding box is a rectangle (x, y, width, height) around an object; it is used in detection. Segmentation uses masks, which are more precise than a box.

Tasks

TaskOutputExample
ClassificationOne class for the whole image“dog”, “cat” or “none”
DetectionBoxes + classA box around the dog
SegmentationOne 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:

🐶 dog
0.62
🐱 cat
0.31
🚫 none
0.07
Decision rule Choose the class with the highest probability (argmax). If that probability is ≥ threshold (e.g. 0.50), accept the class. Otherwise, the model should abstain (“none” or send for review). Here: 0.62 ≥ 0.50 → dog.

False positives and false negatives

When we compare the prediction with reality, each case is TP, TN, FP or FN. The two errors are:

Definition False positive (FP) — the model says the object exists (positive), but in reality it does not exist. It is a false alarm.
  • 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.
Definition False negative (FN) — the model says the object does not exist (negative), but in reality it exists. It is a missed object.
  • 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.
error real object
How not to mix them up False Positive = said Positive (yes), but it was Negative (no) → false alarm.
False Negative = said Negative (no), but it was Positive (yes) → missed it.
RealityPredictionResult
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)
Link to the metrics FP lowers Precision (the model “invents” detections); FN lowers Recall (the model “misses” objects).
Precision = TP / (TP + FP)  ·  Recall = TP / (TP + FN)
That is why the threshold matters: lowering it increases FP; raising it increases FN.
Threshold A high threshold reduces false positives (more demanding), but may increase false negatives. A low threshold does the opposite. The right threshold depends on the cost of each error.

Metrics

Compare the predictions with the reference. The confusion matrix has four cases:

Prediction: positivePrediction: negative
Reality: positiveTP (true positive)FN (false negative)
Reality: negativeFP (false positive)TN (true negative)

Precision

TP / (TP + FP)

Of the detections the model called “positive”, how many were correct. Penalises false positives.

Recall

TP / (TP + FN)

Of the real objects, how many were found. Penalises false negatives.

IoU

intersection / union

Overlap between the predicted mask and the reference (segmentation). 1 = perfect.

F1

2 · P · R / (P + R)

Balance between Precision and Recall.

Definition — IoU IoU (Intersection over Union) measures the overlap between two masks: the area of the intersection divided by the area of the union.
IoU = intersection area ÷ union area
It ranges from 0 (no overlap) to 1 (identical masks). It is the key segmentation metric: a low IoU indicates misaligned outlines, a mask larger/smaller than the object, or wrong labels.
reference (ground truth) prediction intersection
Numeric example A model made 10 detections: 6 correct (TP), 4 false (FP) and missed 2 objects (FN).
Precision = 6 / (6 + 4) = 0.60 · Recall = 6 / (6 + 2) = 0.75 · F1 ≈ 0.67.
IoU example Intersection = 60 pixels and union = 100 pixels → IoU = 0.60.

Label / segmentation errors

ErrorWhat happensAffected metricDecision
Over-segmentationMask larger than the object; includes backgroundPrecision ↓, IoU ↓ (FP)Correct the outline
Under-segmentationMask smaller; leaves parts outRecall ↓, IoU ↓ (FN)Expand the mask
Wrong classCat labelled as dogLearns the wrong associationCorrect the class
Imprecise outlineIrregular / shifted edgesIoU ↓Refine the outline
Missing objectObject not annotatedRecall ↓ (FN)Annotate the object
Validate, correct or reject?
  • 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.
Impact on the model Wrong labels corrupt the training signal: the model learns wrong associations and real-world performance drops. Data quality is as important as quantity. That is why labels are reviewed, corrected and validated before training.

Review checklist

  1. Confirm the object class.
  2. Check that the mask covers the object without including background.
  3. Look for unannotated objects.
  4. Be consistent with edges across images.
  5. 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.

📚 Go to practice Go to the assessment →
Erasmus+ LEARN DEEP CODE AI (LDCAI) project · Theoretical contents · Blocks · Logic · Data/Segmentation
Photographs: Joselodos (CC0), Trougnouf (CC BY 4.0), Tim Evanson (CC BY-SA 2.0) — Wikimedia Commons.