← Fundamentals
Fundamentals

Declaration and initialization

After this line, what does the name hold, and what type did it fix?

Establish the first useful value at the point where it is needed.

Outcome

Predict what a declaration supplies (type, initial value or none) and when its initializer runs, then repair declarations in a parcel-fee program.

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.

Declaration
Introduces a name and fixes its type for the rest of its scope.
Initialization
Establishes a variable’s initial state. An initializer can supply a usable value; some declaration forms require a later first assignment before a read.
Initializer
An expression in a declaration that supplies an initial value and contributes type information when the type is inferred.
Definite assignment
A static check that every path assigns a variable before that path reads it.

Declaring a name and establishing its value

A declaration introduces a name; initialization establishes its first value. A type annotation can state the permitted type, while type inference derives it from the surrounding code or initializer. The selected native rules determine whether a declaration supplies a default value or requires an assignment before a read.

An initializer is an operation reached during execution, so its location matters. A value that belongs to one item and a total that spans a batch need different starting points. Before using either, ask what type it has, which paths establish its value and when the declaration is reached.

01What a declaration supplies

A name and a type, but not always a value

A dispatch desk uses a parcel-fee command to review a batch before charging customers. The command must retain fractional kilograms, apply the zone schedule and keep each parcel’s surcharge separate. Start with the declarations that establish those values, then follow when each initializer actually runs.

The batch summary starts with a parcel count. Before adding any parcels, the developer checks whether a count declaration alone gives a readable starting value. The reduced program reads it immediately.

Declaration introduces a name; an initializer can supply its first value. Inspect the exact native declaration before predicting the read.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let count: number;

console.log(count);
What happens when the program reads the variable?
TypeScript · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc --noEmit --strict main.ts && node main.ts

Use the selected language’s setup from the first reading and replace the file for each experiment. TypeScript runs tsc --strict before node; several experiments are expected to be refused. C++ uses the track’s C++26 mode; the examples need only C++17.

02The initializer’s type

Does a zero initializer allow fractional kilograms?

The count is an integer, but the batch’s total weight must retain fractions. The next reduced check declares total from 0 without an annotation, then adds the first 1.5 kg parcel. Predict whether that declaration can represent the required result.

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

total += 1.5;

console.log(total);
What does the program print?

03Every path initializes

A declaration before a branch needs a value on every path

With weight representation settled, the command needs a charge for every zone. The schedule is A: 5, B: 8, all other zones: 12. This reduced version declares fee before branches for A and B, then processes C. Predict the read before approving the schedule.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const zone: string = "C";

let fee: number;

if (zone === "A") {
  fee = 5;
} else if (zone === "B") {
  fee = 8;
}

console.log(fee);
What happens for zone C?

04Where the initializer runs

A declaration runs each time control reaches it

The operator now reviews multiple parcels in one batch. Before adding surcharge arithmetic, this reduced check labels a 3 kg parcel and then a 1 kg parcel as heavy or light. note is declared before the loop and changed only for a heavy weight. Predict both displayed labels.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const weights = [3, 1];

let note = "light";

for (const weight of weights) {
  if (weight > 2) {
    note = "heavy";
  }

  console.log(weight, note);
}
What are the two printed lines?

05Repair the parcel fees

Check the batch before issuing its charges

The batch review develops from weight totals to fee lines, then to the customer receipt. First preserve fractional kilograms. The fee-report task supplies the complete zone schedule and asks you to connect each reported fee to its parcel. The receipt then adds a per-parcel surcharge and total, where a plausible first repair can still retain another parcel’s value or lose a fraction.

The lab opens in your reading language and has its own language control. Each file contains its imports, types, reader and complete caller. Later levels supply the earlier correct helpers. Some starters are refused by the compiler or checker; that refusal is part of the evidence.

Repair the parcel-fee program

Retain weight precision, report the current parcel’s fee and keep receipt calculations independent.

  1. easy · F-VARIABLES-V4-1 Keep every kilogram
  2. medium · F-VARIABLES-V4-2 Give every zone a fee
  3. hard · F-VARIABLES-V4-3 Print a per-parcel receipt
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.