Learn Modula3 - 10 Code Examples & CST Typing Practice Test
Modula-3 is a high-level, statically typed programming language designed for safe systems programming, modularity, and object-oriented programming. It emphasizes simplicity, safety, and readability while providing features suitable for building large, robust software systems.
Learn MODULA3 with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
Modula-3 Counter and Theme Toggle
MODULE CounterTheme;
IMPORT IO;
VAR
count: INTEGER := 0;
isDark: BOOLEAN := FALSE;
PROCEDURE updateUI():=BEGIN
IO.Put("Counter: "); IO.PutInt(count, 0); IO.PutLn();
IF isDark THEN
IO.PutLn("Theme: Dark")
ELSE
IO.PutLn("Theme: Light")
END;
END updateUI;
PROCEDURE increment():=BEGIN
count := count + 1;
updateUI();
END increment;
PROCEDURE decrement():=BEGIN
count := count - 1;
updateUI();
END decrement;
PROCEDURE reset():=BEGIN
count := 0;
updateUI();
END reset;
PROCEDURE toggleTheme():=BEGIN
isDark := NOT isDark;
updateUI();
END toggleTheme;
BEGIN
updateUI();
increment();
increment();
toggleTheme();
decrement();
reset();
END CounterTheme.
Demonstrates a simple counter with theme toggling using Modula-3 variables, procedures, and safe I/O.
Modula-3 Random Number Generator
MODULE RandomGen;
IMPORT IO, SYSTEM;
VAR i, num: INTEGER;
BEGIN
FOR i := 1 TO 3 DO
num := SYSTEM.Random() MOD 100 + 1;
IO.Put("Random "); IO.PutInt(i,0); IO.Put(" : "); IO.PutInt(num,0); IO.PutLn();
END;
END RandomGen.
Generates random numbers between 1 and 100 and prints them.
Modula-3 Todo List
MODULE TodoList;
IMPORT IO;
VAR todos: ARRAY [0..9] OF STRING;
VAR count: INTEGER := 0;
PROCEDURE addTask(task: STRING);
BEGIN
todos[count] := task; count := count + 1;
FOR i := 0 TO count-1 DO IO.PutLn(todos[i]) END;
END addTask;
PROCEDURE removeTask(index: INTEGER);
VAR i: INTEGER;
BEGIN
FOR i := index TO count-2 DO todos[i] := todos[i+1] END;
count := count - 1;
FOR i := 0 TO count-1 DO IO.PutLn(todos[i]) END;
END removeTask;
BEGIN
addTask("Buy milk");
addTask("Write Modula-3 code");
removeTask(0);
END TodoList.
Maintains a simple todo list with add and remove functionality.
Modula-3 Dice Roller
MODULE DiceRoll;
IMPORT IO, SYSTEM;
VAR i, roll: INTEGER;
BEGIN
FOR i := 1 TO 3 DO
roll := SYSTEM.Random() MOD 6 + 1;
IO.Put("Roll "); IO.PutInt(i,0); IO.Put(" : "); IO.PutInt(roll,0); IO.PutLn();
END;
END DiceRoll.
Rolls a six-sided dice three times.
Modula-3 Countdown Timer
MODULE Countdown;
IMPORT IO;
VAR count: INTEGER := 5;
BEGIN
WHILE count >= 0 DO
IO.Put("Countdown: "); IO.PutInt(count,0); IO.PutLn();
count := count - 1;
END;
IO.PutLn("Done!");
END Countdown.
Counts down from 5 to 0.
Modula-3 Prime Checker
MODULE PrimeCheck;
IMPORT IO;
VAR nums: ARRAY [0..2] OF INTEGER := [7,10,13];
VAR n, i: INTEGER;
VAR isPrime: BOOLEAN;
BEGIN
FOR n IN nums DO
isPrime := TRUE;
FOR i := 2 TO n-1 DO
IF n MOD i = 0 THEN isPrime := FALSE END;
END;
IO.PutInt(n,0);
IF isPrime THEN IO.PutLn(" is Prime") ELSE IO.PutLn(" is Not Prime") END;
END;
END PrimeCheck.
Checks if numbers are prime.
Modula-3 Temperature Converter
MODULE TempConv;
IMPORT IO;
PROCEDURE cToF(c: REAL): REAL;
BEGIN RETURN c*9/5+32 END cToF;
PROCEDURE fToC(f: REAL): REAL;
BEGIN RETURN (f-32)*5/9 END fToC;
BEGIN
IO.Put("25°C = "); IO.PutReal(cToF(25),2); IO.PutLn();
IO.Put("77°F = "); IO.PutReal(fToC(77),2); IO.PutLn();
END TempConv.
Converts Celsius to Fahrenheit and Fahrenheit to Celsius.
Modula-3 Shopping Cart
MODULE ShoppingCart;
IMPORT IO;
VAR cart: ARRAY [0..9] OF STRING;
VAR prices: ARRAY [0..9] OF INTEGER;
VAR count: INTEGER := 0;
PROCEDURE addItem(item: STRING; price: INTEGER);
BEGIN
cart[count] := item; prices[count] := price; count := count + 1;
(* print cart and total sum here *)
END addItem;
PROCEDURE removeItem(index: INTEGER);
VAR i: INTEGER;
BEGIN
FOR i := index TO count-2 DO cart[i] := cart[i+1]; prices[i] := prices[i+1] END;
count := count -1;
(* print cart and total sum here *)
END removeItem;
BEGIN
addItem("Apple", 2);
addItem("Banana", 3);
removeItem(0);
END ShoppingCart.
Adds and removes items in a shopping cart with total cost.
Modula-3 Name Greeting
MODULE Greet;
IMPORT IO;
PROCEDURE greet(name: STRING);
BEGIN
IO.Put("Hello, "); IO.Put(name); IO.PutLn("! Welcome!");
END greet;
BEGIN
greet("Saurav");
greet("Alice");
greet("Bob");
END Greet.
Greets users by name.
Modula-3 Stopwatch
MODULE Stopwatch;
IMPORT IO;
VAR time: INTEGER := 0;
BEGIN
WHILE time < 5 DO
IO.Put("Stopwatch: "); IO.PutInt(time,0); IO.PutLn();
time := time + 1;
END;
IO.PutLn("Done!");
END Stopwatch.
Simulates a stopwatch incrementing seconds.
Frequently Asked Questions about Modula3
What is Modula3?
Modula-3 is a high-level, statically typed programming language designed for safe systems programming, modularity, and object-oriented programming. It emphasizes simplicity, safety, and readability while providing features suitable for building large, robust software systems.
What are the primary use cases for Modula3?
Safe systems programming. Concurrent applications. Compiler development. Networked and distributed systems. Educational projects on modular programming
What are the strengths of Modula3?
High-level abstraction with system-level control. Safe and reliable code through static typing and garbage collection. Supports modular and object-oriented design. Concurrency built into the language. Readable and maintainable syntax
What are the limitations of Modula3?
Smaller community compared to mainstream languages. Limited library ecosystem and tooling. Primarily academic or research use. Not widely used in modern commercial software. Performance can be impacted by garbage collection
How can I practice Modula3 typing speed?
CodeSpeedTest offers 10+ real Modula3 code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.