What a for loop does
A for loop runs a block of code once for every element of a sequence. In Swift the usual form is for-in: you name a loop variable, say which array to walk through, and put the repeated code between braces.
let languages = ["Python", "Java", "JavaScript"]
for language in languages {
print("I love \(language)")
}
On the first pass language is "Python", on the second "Java", on the third "JavaScript". No counter or index is needed: Swift hands you each element directly. The loop variable is a constant that exists only inside the braces.
Braces decide what repeats
Swift uses the braces to mark which lines belong to the loop. Every statement inside them runs once per element. Anything after the closing brace runs once, when the loop is over.
let languages = ["Python", "Java", "JavaScript"]
for language in languages {
print("I love \(language)")
print("I want to get better at it")
}
Both print calls are inside, so the output alternates:
I love Python
I want to get better at it
I love Java
I want to get better at it
I love JavaScript
I want to get better at it
Now move the second call after the closing brace:
for language in languages {
print("I love \(language)")
}
print("I want to get better at them")
I love Python
I love Java
I love JavaScript
I want to get better at them
Indentation helps a human reader, but Swift looks only at the braces. A line that is indented but sits after the closing brace is still outside the loop.
Execution flow
A program starts at its first line and runs one line at a time, from top to bottom. This order is called the execution flow. A loop does not break the flow. When the flow reaches the loop, it runs the body once per element, and when the elements are used up it continues with the line after the closing brace.
let fruits = ["banana", "apple", "kiwi"]
print("I'm hungry!")
for fruit in fruits {
print("I'm gonna eat:")
print(fruit)
}
print("I love my life!")
I'm hungry!
I'm gonna eat:
banana
I'm gonna eat:
apple
I'm gonna eat:
kiwi
I love my life!
The first message prints once because it comes before the loop. The two lines inside print three times, and the last message prints once because it comes after.
The exercise
The editor holds this starting code:
let languages = ["Python", "Java", "JavaScript"]
print("Today we'll learn about:")
// Write the for loop below:
print("Let's code!")
The program must print the heading, then each language on its own line, then Let's code!. The steps are:
- Write the loop header between the two existing
printcalls:for language in languages {. The variable must be namedlanguage. - Print the loop variable inside the braces with
print(language). Each pass prints one name, andprintends its output with a newline, so every name lands on its own line. - Close the brace before the last
print. That keepsLet's code!outside the loop, so it runs once, after the final language.
The finished program:
let languages = ["Python", "Java", "JavaScript"]
print("Today we'll learn about:")
// Write the for loop below:
for language in languages {
print(language)
}
print("Let's code!")
Common mistakes
- Printing the heading inside the loop. It would appear three times.
- Forgetting the closing brace. The compiler reports an error at the end of the file, because the loop is never closed.
- Printing the array itself.
print(languages)shows["Python", "Java", "JavaScript"]on one line, not one name per line. - Adding parentheses around the header. Swift does not need them: write
for language in languages.
Key takeaways
for item in arrayvisits every element, with no index.- The braces mark the loop body. Statements inside run once per element, statements after run once.
- A program runs top to bottom, and a loop repeats its body in place before the flow moves on.
