Much Ado About Verada

A cheerful guide to programs that keep their promises.

6 · Once More Unto the Loop

How do I print exactly the ticket labels I want?

The ushers want to practise matching tickets to the seats we just numbered. They ask Puck for three rehearsal stubs. He writes a line for ticket one, another for ticket two, and a third for ticket three. Then the director asks for eight. Puck looks around for a larger quill.

We already have a Seat_Count for a request of zero through eight. Now we need a way to repeat one action for each ticket number in the request. A loop gives that repeated action one place to live.

A strip shows three numbered ticket stubs and a dashed fourth stub labelled Extra.

The fourth ticket is printed on the finest quality mistake.

Say where to start and where to stop

Our first label is 1. Our last label is the requested count. For a request of three, take a pencil and visit 1, 2, and 3 in order. At each visit, write Ticket and the label. The pencil moves to a new label before it repeats the instruction. We call one visit an iteration.

Before opening your example panel, predict what happens for a request of one. Then try zero on paper. Where would the first visit be, and is it inside the requested range?

Example 6-1 · Print the rehearsal tickets

Vada · Ada · Example 6-1

main.vadb · Link to this version

with Ada.Text_IO;

procedure Main is
   subtype Seat_Count is Natural range 0 .. 8;
   Requested : constant Seat_Count := 3;
begin
   for Label in 1 .. Requested loop
      Ada.Text_IO.Put_Line ("Ticket" & Natural'Image (Label));
   end loop;
end Main;
Varc · C-family · Example 6-1

main.va · Link to this version

import Ada.Text_IO;

fn Main() {
    subtype Seat_Count = Natural range 0...8;
    let requested: Seat_Count = 3;
    for label in 1...requested {
        Ada.Text_IO.Put_Line("Ticket" ++ Natural::Image(label));
    }
}
Vibe · Python · Example 6-1

main.vb · Link to this version

from verada import Annotated, Interval

type Seat_Count = Annotated[int, Interval(ge=0, le=8)]


def main() -> None:
    requested: Seat_Count = 3
    for label in range(1, requested + 1):
        print("Ticket", label)


if __name__ == "__main__":
    main()

Syntax note. Vada's 1 .. Requested and Varc's 1...requested include their upper endpoint. Vibe's range stops just before its second argument. With three requested, range(1, requested + 1) stops before four and visits exactly the same three labels. In all three examples, the loop supplies a fresh value for Label or label on each visit.

Puck carries a ticket punch much larger than his ticket.

One instruction, three holes. Puck requests a fourth hole for administrative purposes.

Copy the example project for your syntax. In its directory, prove, then run. With the request set to three, you should see three lines.

Ticket 1
Ticket 2
Ticket 3

Give the loop nothing to do

Set the request to zero, then prove and run again. In Vada and Varc, the upper bound of 0 is below the starting value of 1, so the range is empty. In Vibe the range starts at 1 and stops before 1, so it too is empty. The run succeeds with no ticket lines. The next instruction after the loop, if you add one, would still execute.

This is why it helps to reason about a range before reasoning about its body. The range answers which labels do we visit? The body answers what do we do at each visit? Here we just print a line. When we start keeping a tally, the order and the work already done will matter. For now, you can follow the whole loop with one finger on the ticket strip.

A spool of ticket paper waits to be fed through the box-office printer.

Your cue

  1. Request 8 tickets. Predict the first label, the last label, and how many lines print.
  2. Restore the request for 3 tickets. In Vada and Varc, add one to the inclusive endpoint. In Vibe, change the stop argument to requested + 2. Which extra label prints?
  3. Restore the loop. If Puck also wants a final line saying Done, where should he put it so it prints once even when zero tickets are requested?
Prompter's notes

A request of eight prints eight lines, starting with Ticket 1 and ending with Ticket 8. Moving the endpoint one step adds Ticket 4 for a request of three. The change can still prove and run. The program has a bound on the request, and its output follows the loop as written. The exact number of printed lines is a separate requirement that we are checking by prediction and experiment here.

Put Done after the loop, outside its body. A zero-ticket run then prints Done once.

Puck can now print a ticket for every seat. When a customer asks whether a particular seat is taken, he'll need a chart that goes with those labels.

← Previous: A Seat by Any Other Name · Contents · Next: All the World's an Array →