t27.aiРусский

A program is steps in order

You will learn

A program is a list of small steps done in order, and the order is part of what the program means.

A program is a list of small steps that the computer does one after another. Take the number 2. Add 3, then double it: you get 10. Double it, then add 3: you get 7. The same two steps in another order give another answer, so when you describe a program to an agent you describe the order too. A loop is one step written once and repeated: add 3 four times, from 2, gives 14. When a result surprises you, read the steps in order and find the earliest one that does not do what you meant.

Try it

Find the three programs in the spec: add_then_double, double_then_add and add_three_n_times. Change LOOP_RESULT from 14 to 11 and run it: a_loop_is_one_step_repeated fails, because 11 is three steps, not four. Put the 14 back.

Open the interactive lesson →

Vibecoding 2: a program is steps in order ↗

The same two steps, add 3 and double, in two orders give two answers, and a loop repeats one step. Lesson 2 of the vibecoding course.

specs/vibecoding/steps_in_order.t27

// SPDX-License-Identifier: Apache-2.0
; Vibecoding lesson 2: a program is steps in order.
; The same two steps in another order give another answer, and a loop repeats one step.

module vc_steps_in_order;

; add 3, then double
pub fn add_then_double(x: u16) -> u16 {
    return (x + 3) * 2;
}

; double, then add 3
pub fn double_then_add(x: u16) -> u16 {
    return x * 2 + 3;
}

; one step, repeated: add 3, n times
pub fn add_three_n_times(start: u16, n: u8) -> u16 {
    var s : u16 = start;
    for i in 0..n {
        s = s + 3;
    }
    return s;
}

pub const START : u16 = 2;
pub const TIMES : u8 = 4;

; What each program printed when it ran with START.
pub const ADD_FIRST : u16 = 10;
pub const DOUBLE_FIRST : u16 = 7;
pub const LOOP_RESULT : u16 = 14;

test add_first_then_double {
    assert ADD_FIRST == (START + 3) * 2;
}

test double_first_then_add {
    assert DOUBLE_FIRST == START * 2 + 3;
}

test the_order_changes_the_answer {
    assert ADD_FIRST != DOUBLE_FIRST;
}

test a_loop_is_one_step_repeated {
    assert LOOP_RESULT == START + 3 * 4;
    assert LOOP_RESULT == START + 3 + 3 + 3 + 3;
}

Open the lesson's spec in the player ↗

All lessons