A conditional is a statement that runs one instruction if a condition is true and a different instruction if it is false.
Structure:
if (condition) do A; else do B;
The order matters; we are only adding decision branches inside it.
Log in to post a comment.
// UNIT 2 – Variable Toy
// Title: First-Order Hilbert Curve (Explicit Sequence)
//
// PSEUDOCODE (explicit instructions, no recursion or loops)
//
// Define step length (step)
// Move to starting position (bottom-left of curve)
// Face upward
// Move up by step
// Turn right and move by step
// Turn right and move down by step
//
// This produces the first-order Hilbert curve: a centered “U” shape.
//
// Later units (loops/recursion) can build higher-order curves
// by repeating this shape inside itself.
// Turtletoy setup
Canvas.setpenopacity(1);
const turtle = new Turtle();
// --------------------------------------------------
// Variables controlling geometry
// (these make size and placement easy to adjust)
let step = 80; // length of each segment
let startX = -40; // bottom-left start coordinate
let startY = 40;
// --------------------------------------------------
// Initial positioning
turtle.penup();
turtle.goto(startX, startY);
turtle.pendown();
// --------------------------------------------------
// Ordered movement sequence
function walk(i) {
// Step 0: face up + rise to top-left cell
if (i === 0) {
turtle.left(90);
turtle.forward(step);
}
// Step 1: face right + move to top-right cell
else if (i === 1) {
turtle.right(90);
turtle.forward(step);
}
// Step 2: face down + return to bottom-right cell
else if (i === 2) {
turtle.right(90);
turtle.forward(step);
}
// Stop after the third segment
return i < 2;
}