Learn BCPL with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
BCPL Counter and Theme Toggle
LET count=0
LET isDark=0
FUN updateUI() =
WRITEF("Counter: %N\n", count)
IF isDark THEN
WRITEF("Theme: Dark\n")
ELSE
WRITEF("Theme: Light\n")
ENDIF
FUN increment() =
count = count + 1
updateUI()
FUN decrement() =
count = count - 1
updateUI()
FUN reset() =
count = 0
updateUI()
FUN toggleTheme() =
isDark = 1 - isDark
updateUI()
# Simulate actions
updateUI()
increment()
increment()
toggleTheme()
decrement()
reset()
Demonstrates a simple counter with theme toggling using BCPL variables and functions.
2
BCPL Random Number Generator
LET i=0
FOR i=1 TO 3 DO
LET num = RAND() MOD 100 + 1
WRITEF("Random %N: %N\n", i, num)
OD
Generates random numbers between 1 and 100 and prints them.
3
BCPL Todo List
LET todos = []
FUN addTask(task) =
APPEND(todos, task)
WRITEF("Todos: %S\n", todos)
FUN removeTask(index) =
DELETE(todos, index)
WRITEF("Todos: %S\n", todos)
# Simulate actions
addTask("Buy milk")
addTask("Write BCPL code")
removeTask(1)
Maintains a simple todo list with add and remove functionality.
4
BCPL Dice Roller
LET i=0
FOR i=1 TO 3 DO
LET roll = RAND() MOD 6 + 1
WRITEF("Roll %N: %N\n", i, roll)
OD
Rolls a six-sided dice three times.
5
BCPL Countdown Timer
LET count = 5
WHILE count >= 0 DO
WRITEF("Countdown: %N\n", count)
count = count - 1
OD
WRITEF("Done!\n")
Counts down from 5 to 0.
6
BCPL Prime Checker
LET nums = [7,10,13]
LET n=0
FOR n IN nums DO
LET isPrime = 1
LET i=2
WHILE i<n DO
IF n MOD i = 0 THEN isPrime = 0 END
i = i + 1
OD
IF isPrime THEN WRITEF("%N is Prime\n", n) ELSE WRITEF("%N is Not Prime\n", n) END
OD
Checks if numbers are prime.
7
BCPL Temperature Converter
FUN cToF(c) = c*9/5+32
FUN fToC(f) = (f-32)*5/9
WRITEF("25°C = %N°F\n", cToF(25))
WRITEF("77°F = %N°C\n", fToC(77))
Converts Celsius to Fahrenheit and Fahrenheit to Celsius.
8
BCPL Shopping Cart
LET cart = []
LET prices = []
FUN addItem(item, price) = APPEND(cart, item); APPEND(prices, price)
FUN removeItem(index) = DELETE(cart,index); DELETE(prices,index)
# Simulate actions
addItem("Apple",2)
addItem("Banana",3)
removeItem(1)
Adds and removes items in a shopping cart with total cost.
9
BCPL Name Greeting
FUN greet(name) = WRITEF("Hello, %S! Welcome!\n", name)
# Simulate greetings
greet("Saurav")
greet("Alice")
greet("Bob")
Greets users by name.
10
BCPL Stopwatch
LET time = 0
WHILE time < 5 DO
WRITEF("Stopwatch: %N seconds\n", time)
time = time + 1
OD
WRITEF("Done!\n")
Simulates a stopwatch incrementing seconds.