← Fundamentals
Fundamentals

Expressions and statements

Which value survives this line, and what changed while it ran?

Follow both the value an operation produces and the work it performs.

Outcome

Track the destination of a computed value, distinguish it from an effect, and repair discarded or repeated work using native rules.

Terminology (4)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Expression
A construct evaluated in a value context, possibly with effects. Whether its result can be used depends on its native type and context.
Expression statement
An expression used as a program step, with its result discarded. Each language restricts which expressions may appear in this position.
Side effect
An observable change made while evaluating code, such as updating a counter or writing output. Discarding a returned value does not undo it.
Value destination
The operation that consumes a result: a binding, assignment, enclosing expression, call argument or return.

Values and program steps

An expression is source that is evaluated to produce a result; a statement is a step in a program. Expressions can appear inside larger expressions, supply arguments or provide a value to store or return. The selected language determines which forms fit each position.

Evaluation can also have a side effect: an observable action such as printing or updating an object. A function call may both return a value and change state. When reading a line, follow those two responsibilities separately: where its result goes and what changes while the result is obtained. The later examples test both with calculations and an allocator.

01Where does the value go?

Does computing a new count keep it?

A proof-production tool estimates press work before a print job is released. A correction run adds three front impressions to a stored count of eight. The calculation is present in the source. Does the estimate receive it?

The estimate counts image impressions, including correction runs; front and reverse counts can differ. This reduction keeps only the front correction. Predict what happens with the exact standalone addition. Treat a compiler refusal separately from an accepted program with an unchanged count.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let front = 8;

front + 3;
console.log(front);
What happens with this exact source?
TypeScript · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit && node main.ts

These complete files use the selected language’s setup from the first reading. Replace the file for each experiment. C++ uses the track’s C++26 mode and ordinary local objects; no reference outlives its object. Warnings vary by compiler and settings.

02Does this choice supply a value?

Does selecting the revised reverse count also store it?

The front correction now has a destination. The reverse run needs a different edit: an artwork revision changes its estimate from five impressions to eight. The tool must choose a count for the current calculation; writing that choice back to the stored estimate is a separate decision. Can one branch accidentally do both?

These native reductions print two observations: selected, then the stored reverse count. They deliberately use different constructs. Predict the exact pair before treating an assignment as the branch’s numeric value.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const revised = true;
let reverse = 5;
const selected = revised ? (reverse = 8) : reverse;

console.log(selected, reverse);
What are selected and the stored reverse estimate at the print?

03Keep a value without repeating its effect

The estimate is ready. Which serial reaches both artwork faces?

After estimating the work, the tool prepares the proof job. Its front and reverse artwork must identify the same job, so both faces need one saved serial. Start with this reduced allocator before adding print modes: claim returns the current serial and advances next. This is a local model of the tool’s serial allocator.

The first call discards its result. The second retains a number, then the print reads it for both artwork faces and reports the next serial. No claim happens inside the print arguments. Does ignoring the first returned number leave it available for the job?

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Allocator = { next: number };

function claim(allocator: Allocator): number {
    const result = allocator.next;
    allocator.next += 1;

    return result;
}

const allocator: Allocator = { next: 70 };
claim(allocator);
const retained = claim(allocator);

console.log(retained, retained, allocator.next);
What does this reduced proof-job allocator print: front serial, back serial, next serial?
Check this idea later
A proof estimate stores reverse = 13. It executes reverse + 4; as a standalone statement before reading reverse for the estimate. What count remains?
A proof allocator starts at serial 46. Its claim helper returns the current serial and advances next. A preview calls claim and discards the result. The next real job calls claim once and uses that saved result on both artwork faces. What serials do those faces contain?

04Repair the proof-production tool

Keep the impression count, then protect serial allocation

Repair the production tool with new inputs and complete helper functions. Its estimate first needs the corrected front count returned to the caller. Then the tool needs a total for the selected print mode: simplex includes fronts only; duplex includes the reverse run too. The supplied reverse count can differ from the front count because of corrections.

The serial repair adds decisions absent from the reduced example. A preview must not consume a serial, a reprint must keep its existing one, and a new real job needs one claim shared by both faces. Preview takes priority even if an existing serial is supplied. Zero is a valid serial. The job exists before press work starts, so even a new job with zero impressions needs an identity. Check the next serial and claim count alongside the face values; matching faces alone can hide unwanted allocation.

The lab opens in your primary reading language; its language control lets you switch. Each file includes the caller, imports and custom types. Later tasks supply the earlier correct helpers.

Repair the proof-production tool

Retain a computed value, choose a native contribution, then preserve values and effects across print paths.

  1. easy · F-EXPRESSIONS-V4-1 Keep the correction
  2. medium · F-EXPRESSIONS-V4-2 Choose the contribution
  3. hard · F-EXPRESSIONS-V4-3 Preserve the serial and its effects
In the frontend A value inside JSX, a block around Svelte markup Optional

The proof screen also chooses a status label from a supplied ready flag. JSX braces consume an expression value; a Svelte if block selects which markup exists. Neither status display needs a serial claim.

Proof.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
export default function Proof({ ready }: { ready: boolean }) {
  return <p>{ready ? "Ready" : "Preview"}</p>;
}

The conditional expression supplies text inside JSX. A plain if statement cannot occupy those braces; it can choose a returned element before the return instead.

Sources: React conditional rendering,Svelte if blocks.

Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.