Part I — Foundations · Chapter 1 · Lesson 4 of 5 · 6 min read
Lesson 4: Your First .pea Program
Lesson level: Foundation
Purpose: Create, understand, check, format, run, and modify a small but complete PeaLang program.
Learning objectives
By the end of this lesson, you should be able to:
- create a UTF-8 .pea source file;
- declare values with let and const;
- use arithmetic and string interpolation;
- make a simple decision with if and else;
- print useful program output;
- apply the format–check–run workflow; and
- trace a short program from top to bottom.
4.1 The program you will build
Create a file named first_program.pea. It will calculate an order subtotal, apply a simple discount rule, and print a readable summary. The example deliberately uses only foundational constructs so every line can be explained without relying on later chapters.
const product = "Notebook"
let quantity = 3
let unit_price = 12.5
let subtotal = quantity * unit_price
let discount = 0
if subtotal >= 30 {
discount = 5
} else {
discount = 0
}
let total = subtotal - discount
print "Product: {product}"
print "Quantity: {quantity}"
print "Subtotal: {subtotal}"
print "Discount: {discount}"
print "Total: {total}"
4.2 Read the program from top to bottom
PeaLang executes top-level statements in source order. Each statement can create a name, calculate a value, choose a branch, update a variable, or produce output. Reading sequentially is therefore the simplest way to understand this first program.
4.3 Constants and variables
const product = "Notebook" creates a name whose binding is intended not to change after initialization. Use const when reassignment would make the program harder to reason about.
let quantity = 3 and let unit_price = 12.5 create mutable bindings. The initial values are inferred from the literals: quantity begins as an integer and unit_price as a floating-point number.
Naming convention. This example uses lowercase names separated by underscores. Names should describe meaning rather than storage details. unit_price communicates more than a vague name such as x.
4.4 Expressions and calculated values
The statement let subtotal = quantity * unit_price evaluates the multiplication expression first, then binds its result to subtotal. With the supplied values, the subtotal is 37.5.
The variable discount starts at zero so that it has a defined value before the conditional changes it. The final expression subtotal - discount produces the payable total.
Calculated names make the program easier to inspect. Repeating quantity * unit_price everywhere would hide the business concept and increase the chance of inconsistent changes.
4.5 A simple conditional
The condition subtotal >= 30 evaluates to either true or false. If it is true, the statements inside the first block execute and discount becomes 5. Otherwise, the else block executes and discount remains zero.
Braces group the statements belonging to each branch. Indentation makes those blocks readable, while formatting keeps their layout consistent.
For the current inputs, 37.5 is at least 30, so the program selects the first branch. The total becomes 32.5.
4.6 Output and interpolation
The print statement sends a value to the program’s output channel. Text inside double quotation marks is a string literal. A name enclosed in braces inside that string is interpolated, meaning its current value is inserted into the output.
For example, print "Product: {product}" produces Product: Notebook. Interpolation is often clearer than manually joining several fragments.
Programs should print information that helps a reader understand the result. A bare number such as 32.5 is less useful than a labeled total.
4.7 Format, check, and run
Apply the workflow from the previous lesson:
pea fmt first_program.pea
pea check first_program.pea
pea run first_program.pea
Expected output:
Product: Notebook
Quantity: 3
Subtotal: 37.5
Discount: 5
Total: 32.5
If formatting or checking fails, correct that stage before interpreting runtime output. If execution succeeds but the values are unexpected, trace the inputs, branch condition, assignments, and final expression in that order.
4.8 Trace the state
| After statement | Relevant state |
|---|---|
| Product declaration | product = "Notebook" |
| Input declarations | quantity = 3; unit_price = 12.5 |
| Subtotal calculation | subtotal = 37.5 |
| Discount initialization | discount = 0 |
| Conditional branch | 37.5 >= 30 is true; discount = 5 |
| Total calculation | total = 32.5 |
A trace is a deliberate snapshot of relevant names after each important statement. It is one of the best debugging methods for short programs because it reveals where the actual state first differs from the expected state.
4.9 Modify one input at a time
Change quantity from 3 to 2. The subtotal becomes 25, the condition becomes false, the else branch keeps the discount at zero, and the total becomes 25.
Then restore quantity to 3 and change unit_price. Predict the output before running the program. Comparing a prediction with actual output develops the habit of reasoning about code rather than relying exclusively on trial and error.
4.10 Improve clarity without adding complexity
The example separates input-like values, calculations, decisions, and output. Preserve that broad order in small scripts:
- declare configuration and inputs;
- calculate intermediate values;
- apply decisions or transformations;
- calculate final results; and
- print or return the outcome.
This structure makes later refactoring into functions easier. It also makes a dry run more natural because state changes follow the problem’s reasoning.
Use const deliberately. The product name is fixed in this example, so const communicates intent. Quantity and unit price use let because the next exercises will change them. In a mature program, prefer const whenever a binding should not be reassigned.
Name the business rule. The values 30 and 5 are easy to understand here, but unexplained literals become confusing as programs grow. A clearer next version can introduce discount_threshold and discount_amount.
4.11 Common mistakes
- Saving as rich text: .pea files must be plain UTF-8 text.
- Using a filename without the .pea extension: consistent extensions help tools discover source files.
- Misspelling a name: quantity and quanity are different identifiers.
- Using a name before declaration: create inputs and intermediate values before expressions depend on them.
- Forgetting quotes around text: an unquoted word is treated as a name rather than a string literal.
- Forgetting a closing brace: blocks must balance.
- Using = inside the condition: assignment and comparison have different purposes; the threshold condition uses >=.
- Printing an unlabeled result: include context so output remains understandable.
4.12 Guided practice
- Predict the result when quantity is 1.
- Change product to another string and verify interpolation.
- Replace the threshold with 50 and predict which branch executes.
- Introduce const discount_threshold = 30 and use it in the condition.
- Introduce const discount_amount = 5 and use it in the assignment.
- Add a final line that prints whether a discount was applied, using another if statement.
4.13 Troubleshooting sequence
- Confirm that the file is plain text and has the correct name.
- Run the formatter to normalize layout.
- Run the checker and correct the first reported error before later ones.
- Run the program and compare output with the expected values.
- Trace variables after every calculation and branch.
- Reduce the program to the smallest version that still shows the problem.
4.14 Hands-on lab: Build a shipping quote
Scenario: Write shipping_quote.pea to calculate a simple shipping charge.
Requirements:
- Declare a constant destination label.
- Declare mutable package_weight and base_charge values.
- Start surcharge at zero.
- If package_weight is greater than 10, set surcharge to 7.5; otherwise keep it at zero.
- Calculate final_charge as base_charge plus surcharge.
- Print labeled destination, weight, base charge, surcharge, and final charge.
- Format, check, and run the file.
- Test one weight below the threshold, one exactly at the threshold, and one above it.
Deliverable: The source file, expected output for all three cases, actual output, and a short explanation of the boundary behavior at exactly 10.
4.15 Review questions
- What is the practical difference between let and const in this lesson?
- In what order are top-level statements executed?
- What value does the comparison expression produce?
- Why is discount initialized before the conditional?
- How does string interpolation improve output?
- What does the dry-run trace reveal?
- Why should formatting and checking precede execution?
- Why are named thresholds clearer than unexplained numeric literals?
4.16 Applied challenge
Extend the order program with two independent rules: a quantity-based discount and a shipping charge. Keep each intermediate value named, print a complete breakdown, and test inputs that activate neither rule, each rule separately, and both rules together. Do not use functions or loops yet.
Lesson summary
Your first PeaLang program combines declarations, expressions, assignment, branching, and interpolated output in a readable top-to-bottom flow. A disciplined developer predicts results, formats and checks source, runs the smallest useful example, and traces state when behavior differs from expectations.
Lesson status: Approved.