Loops
A loop runs the same block of code again and again. Emerald has two: while repeats as long as a
condition is true, and for repeats once for each item in a collection or number in a range.
var lives = 3while lives > 0 { print("Lives left: #{lives}") lives -= 1}print("Game over")Lives left: 3Lives left: 2Lives left: 1Game overBefore each time through the block, the condition is checked. While it is true, the block runs;
once it is false, the program carries on after the loop. Something inside the block has to change
the condition eventually, as lives -= 1 does here. Without that, the loop would run forever.
For each item
Section titled “For each item”for gives each item of a list a name, one at a time:
const names = ["Ada", "Grace", "Hopper"]for name in names { print("Hello, #{name}!")}Hello, Ada!Hello, Grace!Hello, Hopper!name is a new name for each item, and it exists only inside the loop. Lists have a page of their
own, Lists. A for loop can go through a string’s characters too: for letter in "café" visits c, a, f, and é.
For each number
Section titled “For each number”A range gives the numbers to count through. 1..5 means 1 up to 5, including 5:
for number in 1..5 { print(number)}12345..< stops before its end, which is what you want for positions that start at 0: 0..<3 is 0, 1,
and 2, and 0..<list.count visits every position in a list.
When you only want to repeat something and don’t need the number, name it _:
for _ in 1..3 { print("Hip hip hooray!")}Hip hip hooray!Hip hip hooray!Hip hip hooray!Counting down and skipping
Section titled “Counting down and skipping”Ranges only count upward. To count down, say so with down_to:
for n in 3.down_to(1) { print(n)}print("Liftoff!")321Liftoff!step counts by more than one:
for n in (0..10).step(3) { print(n)}0369Because ranges only count upward, a range that starts past its end simply counts nothing. That makes
a loop like for i in 1..count safe when count is 0: it doesn’t run. When both ends are written as
numbers, though, an empty range can only be a mistake, and Emerald says what you probably meant:
for n in 5..1 { print(n)}loops.em:1:10: this range is empty, because ranges count upward for n in 5..1 { ^^^^Count down with `5.down_to(1)`, or up with `1..5`.Ranges and Slicing covers ranges in full.
Stopping early and skipping ahead
Section titled “Stopping early and skipping ahead”break leaves the loop at once. continue skips the rest of this time through and goes on to the
next. Both are often written with an if after them, on one line:
for n in 1..10 { continue if n % 2 == 0 # skip even numbers break if n > 7 # stop after 7 print(n)}1357while true makes a loop that only break can end. It suits a loop that should keep going until
something happens:
while true { const answer = input("Say something (or quit): ") break if answer == "quit" print("You said #{answer}")}print("Bye!")Say something (or quit): hiYou said hiSay something (or quit): quitBye!A loop might not run at all
Section titled “A loop might not run at all”Emerald remembers that a loop’s block might run zero times, for example over an empty list. So a variable given its value only inside a loop may still be without one afterwards:
var found: Boolfor n in 1..3 { found = true}print(found)loops.em:5:7: `found` may not have been assigned print(found) ^^^^^The loop that assigns `found` might not run at all. Give it a value before the loop.Give it a starting value, var found = false, and the program works.
The name a for loop gives each item can’t be changed, since it simply takes the next item each
time. Copy it into a var if you need one that changes.
What you’ve learned
Section titled “What you’ve learned”while condition { ... }repeats while the condition istrue; something must eventually make itfalse, or abreakmust end it.for item in list { ... }visits each item, andfor n in 1..5each number;..<leaves out the end, and_is a name for a value you don’t need.- Ranges count upward;
down_tocounts down andstepskips. breakleaves a loop andcontinuemoves on to the next time through.- A loop’s block might not run, so a variable assigned only inside it needs a starting value.
Next, functions: naming a piece of code so you can use it again.