Step 7: How many steps?

Now let’s look at the body of the function:

var steps = 0
loop {
  if n == 1 { break steps }
  n := (if n % 2 == 0 { n / 2 } else { n * 3 + 1 })
  steps := steps + 1
}

What does this do? Notice the variable steps: that seems to be a key; it’s mentioned several times. If we focus just on the steps variable, we get this view of the code:

var steps = 0
loop {
  if n == 1 { break steps }
  
  steps := steps + 1
}

We’re counting again! We have a loop, the “more fundamental form” promised earlier. This sort of loop will repeat until it is Or until the “flow of control”, as we say, escapes in some other way, as we’ll see later. with a break expression. Almost always, such a break will be “conditional”; that is, it will depend on some condition being fulfilled; otherwise, the loop will never repeat, which is rather pointless.

Each time around this loop, the variable steps is increased by 1. How? The expression steps := steps + 1 sets, or “assigns” the variable steps to the value steps + 1.

When do we stop the loop? When the variable n is equal to 1: in the expression if n == 1 { break steps} we see our first conditional expression, or if expression. This has two parts: the condition, and the Hopefully you are beginning to notice a consistent use of the word “body” to mean something like “the main part of an expression”.. Here, the condition is n == 1. This tests whether the variable n is equal to be number 1. If it is, we evaluate break steps, which exits the loop, making the value of the loop expression steps. Since the loop is the last expression in the function body, that is also the value of the function.

A quick aside: notice that we have three quite similar symbols which do different things:

  • = which we use to give the initial value of a variable in a let expression (more on that later)
  • := which we use to assign a new value to an existing variable
  • == which we use to test equality of two values

So, how many steps do we take? To find out, we need to examine the bit of the function body we omitted above.

Conjectural calculation