For-In Loop
Part of the Fundamentals section of Coddy's Swift journey — lesson 42 of 86.
Loops let you repeat code multiple times without writing it over and over. The for-in loop is Swift's most common loop, perfect for when you know exactly how many times you want to repeat something.
Here's the basic structure:
for number in 1...5 {
print(number)
}
// Output: 1, 2, 3, 4, 5 (each on a new line)The loop creates a variable called number that automatically takes each value in the range. The code inside the curly braces runs once for each value. In this case, it runs 5 times.
If you don't need the loop variable, use an underscore to ignore it:
for _ in 1...3 {
print("Hello!")
}
// Prints "Hello!" three timesThe 1...5 is a closed range that includes both 1 and 5. You can also use 1..<5 (half-open range) which includes 1 through 4, but excludes 5.
Challenge
EasyRead an integer n from input. Use a for-in loop to print all numbers from 1 to n, each on a new line.
You will receive the following input:
- A single line containing an integer as a string (e.g.,
"5")
Read the input, convert it to an Int, and use a for-in loop with a closed range to print each number.
For example, if the input is 4, the output should be:
1
2
3
4Cheat sheet
The for-in loop repeats code a specific number of times:
for number in 1...5 {
print(number)
}
// Output: 1, 2, 3, 4, 5 (each on a new line)The loop variable (number) automatically takes each value in the range. The code inside the braces runs once for each value.
If you don't need the loop variable, use an underscore:
for _ in 1...3 {
print("Hello!")
}
// Prints "Hello!" three timesRange operators:
1...5- closed range (includes both 1 and 5)1..<5- half-open range (includes 1 through 4, excludes 5)
Try it yourself
// Read input
let input = readLine()!
let n = Int(input)!
// TODO: Write your code below - use a for-in loop to print numbers from 1 to nThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input