Repeat-While Loop
Part of the Fundamentals section of Coddy's Swift journey — lesson 44 of 86.
The repeat-while loop is similar to the while loop, but with one key difference: it checks the condition after executing the code block, not before. This guarantees the loop body runs at least once.
var count = 1
repeat {
print(count)
count += 1
} while count <= 3
// Output: 1, 2, 3Notice the structure: the repeat keyword comes first, followed by the code block, and the condition appears at the end after while.
The difference becomes clear when the condition is false from the start:
var number = 10
repeat {
print("This runs once!")
} while number < 5
// Output: This runs once!Even though number < 5 is false, the message still prints once. With a regular while loop, nothing would print at all.
Use repeat-while when you need the code to execute at least once before checking whether to continue, such as prompting a user for input until they provide valid data.
Challenge
EasyRead an integer n from input. Use a repeat-while 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 repeat-while loop to print each number starting from 1.
Requirements:
- Start a counter at
1 - Print the counter value
- Increment the counter
- Continue while the counter is less than or equal to
n
For example, if the input is 4, the output should be:
1
2
3
4Cheat sheet
The repeat-while loop checks the condition after executing the code block, guaranteeing the loop body runs at least once.
Syntax:
repeat {
// code block
} while conditionExample:
var count = 1
repeat {
print(count)
count += 1
} while count <= 3
// Output: 1, 2, 3Key difference from while loop:
var number = 10
repeat {
print("This runs once!")
} while number < 5
// Output: This runs once!Even when the condition is false from the start, the code block executes once. A regular while loop would not execute at all in this case.
Use repeat-while when you need the code to execute at least once before checking whether to continue.
Try it yourself
// Read input and convert to Int
let n = Int(readLine()!)!
// TODO: Write your code below
// Start a counter at 1 and use a repeat-while 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