Learning programming with Jennifer

Jennifer

Your first steps in programming: values, variables, decisions, loops, lists and functions, one small program at a time.

by @mplx

1 / 15

A program is a list of steps

use io;

io.printf("Hello, world!\n");
$ jennifer run hello.j
Hello, world!
2 / 15

Values have types

TypeExamplesWhat it holds
int42, -7, 0whole numbers
float3.14, 0.5numbers with a fractional part
string"Ada", "hello"text, in quotes
booltrue, falseyes or no
3 / 15

Variables are named boxes

use io;

def name as string init "Ada";
def age as int init 36;
$age = $age + 1;
io.printf("{$name} is {$age}\n");
Ada is 37
4 / 15

Doing sums

use io;

io.printf("%d\n", 7 + 3 * 2);
io.printf("%f\n", 7 / 2);
io.printf("%d\n", 7 // 2);
io.printf("%d\n", 7 % 2);
13
3.5
3
1
5 / 15

Making decisions

use io;

def temperature as int init 23;
if ($temperature > 30) {
    io.printf("hot\n");
} elseif ($temperature > 15) {
    io.printf("pleasant\n");
} else {
    io.printf("cold\n");
}
6 / 15

Questions with yes or no answers

You writeIt asks
$a == $bare they equal?
$a != $bare they different?
$a < $b, $a >= $bsmaller? at least as big?
$hungry and $awakeare both true?
$rain or $snowis at least one true?
not $doneis it false?
7 / 15

Doing it again: loops

use io;

def n as int init 3;
while ($n > 0) {
    io.printf("%d... ", $n);
    $n = $n - 1;
}
io.printf("lift-off!\n");

for (def i in 1..4) {
    io.printf("round %d\n", $i);
}
8 / 15

Lists keep things in order

use io;

def fruits as list of string init ["apple", "pear"];
$fruits[] = "plum";
io.printf("%d fruits, the first is %s\n", len($fruits), $fruits[0]);
for (def f in $fruits) {
    io.printf("- %s\n", $f);
}
3 fruits, the first is apple
9 / 15

Maps look things up by name

use io;

def ages as map of string to int init {"Ada": 36, "Alan": 41};
$ages["Grace"] = 85;
for (def who in $ages) {
    io.printf("%s: %d\n", $who, $ages[$who]);
}
Ada: 36
Alan: 41
Grace: 85
10 / 15

Functions give a name to steps

use io;

func area(width as float, height as float) {
    return $width * $height;
}

def room as float init area(4.5, 3.0);
io.printf("%f|prec=2 square metres\n", $room);
13.50 square metres
11 / 15

Mistakes become messages

$ jennifer run typo.j
typo.j: parse error at /home/ada/typo.j:4:19: undefined variable "totl"
  io.printf("%d\n", $totl);
                    ^
$ jennifer run check.j
check.j: runtime error at /home/ada/check.j:2:5: `if` condition must be bool, got int
  if ($count) {
      ^
12 / 15

Putting it together

use io;
func grade(points as int) {
    if ($points >= 90) {
        return "A";
    } elseif ($points >= 50) {
        return "B";
    }
    return "F";
}
def scores as map of string to int init {"Ada": 95, "Alan": 78};
for (def who in $scores) {
    io.printf("%s|pad=6 %s\n", $who, grade($scores[$who]));
}
13 / 15

Try things in the REPL

$ jennifer repl
>>> def x as int init 6 * 7;
>>> $x;
42
>>> $x > 40;
true
>>> "Hello, " + "Ada";
"Hello, Ada"
14 / 15

Where to go next

Based on Jennifer 0.26.0, November 2026.

15 / 15