Basics, 6 of 8
Loops
Count with For, go over values with For ... In, repeat with While, Repeat, and Loop, and leave early.
What you’ll build
Three number puzzles: a times table, the steps of the Collatz sequence, and a search for the first pair of numbers with a given product. Each uses a different loop, and the last one leaves two loops at once.
Counting with For
For i = a To b counts from a up to b, both included. Until stops before the end instead,
and Step counts by another amount, or down. The loop declares its variable, which exists only
inside the loop and can’t be assigned there.
Import std.core
For row = 1 To 4
Local line = ""
For column = 1 To 4
line += "{row * column:>4}"
End For
Print(line)
End For
For i = 0 Until 10 Step 3
Print(i)
End For1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 0 3 6 9
Going over values
For x In values gives each element of an array in turn, and For i, x In values gives each
with its index, from 0. A range, 1 To 5 or 0 Until n, is a value too, which a loop can go
over, and which has Methods like Sum.
Import std.core
Local primes = [2, 3, 5, 7]
For i, p In primes
Print("prime {i}: {p}")
End For
For n In 1 To 3
Print(n * n)
End For
Print((1 To 100).Sum())prime 0: 2 prime 1: 3 prime 2: 5 prime 3: 7 1 4 9 5050
While and Repeat
While tests its condition before each pass, so its body may not run at all. Repeat ... Until
runs its body first and tests after, so the body always runs at least once. The Collatz sequence
halves an even number and takes an odd one to three times it plus one, until it reaches 1.
Import std.core
Local n = 27
Local steps = 0
Local highest = n
While n != 1
If n % 2 == 0
n /= 2
Else
n = 3 * n + 1
End If
If n > highest Then highest = n
steps++
End While
Print("27 reaches 1 in {steps} steps, going as high as {highest}")
Local square = 0
Repeat
square++
Until square * square > 500
Print("the first square over 500 is {square * square}")27 reaches 1 in 111 steps, going as high as 9232 the first square over 500 is 529
Leaving early
Break leaves a loop, and Continue skips to its next pass. Loop ... End Loop repeats until a
Break or a Return ends it. A label before a loop, like Search:, lets Break and Continue
name it, to leave an outer loop from inside an inner one.
Import std.core
Local found = ""
Search: For a = 2 To 20
For b = a To 20
If (a + b) % 2 != 0 Then Continue
If a * b == 96
found = "{a} x {b}"
Break Search
End If
End For
End For
Print("the first even-sum pair with product 96: {found}")
Local tries = 0
Loop
tries++
If tries * 7 % 5 == 1 Then Break
End Loop
Print("7 x {tries} leaves 1 when divided by 5")the first even-sum pair with product 96: 6 x 16 7 x 3 leaves 1 when divided by 5
What you learned
For i = a To bcounts with both ends included;Untilleaves the end out, andStepsets the step.For x In valuesgoes over an array or a range, andFor i, x Inadds the index.Whiletests first, andRepeat ... Untiltests after the body.Looprepeats untilBreak;Continueskips to the next pass.- A label lets
BreakandContinuename an outer loop.
The rules are in the language reference.
Report an issue with this page or with FutureBlitz on GitHub.