Swift Repeat-While Loop
A Swift repeat-while loop executes a block of statements and then checks whether it should run again. Because the condition is evaluated after the loop body, the statements inside the loop always execute at least once.
Use a repeat-while loop when an operation must occur before its continuation condition can be tested. Common examples include requesting input before validating it, attempting an operation before checking its result, and displaying a menu before deciding whether to show it again.
Swift Repeat While Loop is used to execute a set of statements repeatedly based on a condition. Repeat While Loop is different from While Loop based on the fact that the expression is evaluated after executing the set of statements. Repeat While Loop ensures that the set of statements gets executed atleast once irrespective of the boolean_expression’s value.
Swift Repeat-While Loop Syntax
Following is syntax of Repeat While Loop in a swift program.
repeat {
// set of statements
} while( boolean_expression )
First the set of statements inside repeat block are executed. Then boolean_expression is evaluated and if it returns true, the set of statements inside the repeat block are executed again. Then boolean_expression is evaluated again and the process continues. The loop is broken only when the boolean_expression is evaluated to false.
Parentheses around the condition are optional in Swift. The following is the commonly used form:
repeat {
// statements
} while condition
How a Swift Repeat-While Loop Executes
- Swift executes the statements inside the
repeatblock. - Swift evaluates the condition after the block finishes.
- If the condition is
true, execution returns to the start of therepeatblock. - If the condition is
false, the loop ends and execution continues with the next statement.
The loop body should normally update a value used by the condition. If the condition always remains true, the loop continues indefinitely unless it encounters a break statement.
Swift Repeat-While Loop Example: Count from 1 to 4
In this example, we will use repeat while loop to iterate a set of statements while i<5. Observe that we are modifying the state of the program by incrementing i in repeat block.
main.swift
var i = 1
repeat {
print("i : \(i)")
i = i + 1
} while( i<5 )
print("rest of the statements")
Output
$swift repeat_while_loop_example.swift
i : 1
i : 2
i : 3
i : 4
rest of the statements
The variable i starts at 1. Each iteration prints the current value and increments it by one. After i becomes 5, the condition i < 5 evaluates to false and the loop stops.
Swift Repeat-While Loop When the First Condition Check Is False
In this example, we will use repeat while loop when the boolean expression evaluates to false in the first run.
main.swift
var i = 8
repeat {
print("i : \(i)")
i = i + 1
} while( i<5 )
print("rest of the statements")
Output
$swift repeat_while_loop_example.swift
i : 8
rest of the statements
The statements inside the repeat block execute once before Swift evaluates i < 5. The condition is false, so the loop does not begin a second iteration.
Swift Repeat-While Countdown Example
A repeat-while loop can also update its control variable by decreasing it. This example prints a countdown and stops when count reaches zero.
var count = 3
repeat {
print(count)
count -= 1
} while count > 0
print("Go")
Output
3
2
1
Go
Exit a Swift Repeat-While Loop with Break
The break statement ends the nearest loop immediately. It is useful when the loop discovers its stopping condition while processing the body.
var number = 1
repeat {
if number == 4 {
break
}
print(number)
number += 1
} while number <= 10
print("Loop ended")
Output
1
2
3
Loop ended
When number becomes 4, Swift executes break and continues with the first statement after the repeat-while loop.
Skip a Swift Repeat-While Iteration with Continue
The continue statement skips the remaining statements in the current iteration. In a repeat-while loop, Swift then evaluates the condition before deciding whether to begin another iteration.
var number = 0
repeat {
number += 1
if number == 3 {
continue
}
print(number)
} while number < 5
Output
1
2
4
5
The value 3 is not printed because that iteration reaches continue. The increment occurs before continue, allowing the loop to keep progressing.
Swift Repeat-While Loop for Input Validation
A repeat-while loop fits situations where a value must be obtained before it can be checked. The following example simulates repeated attempts until a valid value is found.
let attempts = [-2, 0, 7]
var index = 0
var value: Int
repeat {
value = attempts[index]
print("Received: \(value)")
index += 1
} while value <= 0 && index < attempts.count
print("Accepted value: \(value)")
Output
Received: -2
Received: 0
Received: 7
Accepted value: 7
The assignment occurs inside the loop because the value must exist before the condition can test it. In an interactive program, the assignment could instead read and convert user input.
Swift Repeat-While Loop Compared with While Loop
The main difference between repeat-while and while is when Swift evaluates the condition.
| Loop type | Condition evaluation | Minimum executions | Typical use |
|---|---|---|---|
while | Before the loop body | Zero | Run only when a condition is already true |
repeat-while | After the loop body | One | Perform an action before testing whether to repeat it |
var whileValue = 5
while whileValue < 5 {
print("while: \(whileValue)")
whileValue += 1
}
var repeatValue = 5
repeat {
print("repeat-while: \(repeatValue)")
repeatValue += 1
} while repeatValue < 5
Output
repeat-while: 5
The while loop does not execute because its condition is false before the first iteration. The repeat-while loop prints once because it checks the condition afterward.
When to Use Repeat-While, While, or For-In in Swift
- Use
repeat-whilewhen the loop body must execute before the condition can be evaluated. - Use
whilewhen the condition should be checked before any statements run. - Use
for-inwhen iterating over a range, array, string, dictionary, or another sequence.
For example, counting from 1 through 10 is normally clearer with for number in 1...10. Repeating a menu after showing it once is a better match for repeat-while.
Common Swift Repeat-While Loop Mistakes
- Expecting the condition to run first: The body always executes before the first condition check.
- Not changing the condition state: A condition that remains true can create an infinite loop.
- Updating in the wrong direction: The control value must move toward the condition becoming false.
- Placing an update after
continue: The update may be skipped, preventing the loop from progressing. - Using repeat-while for sequence traversal: A
for-inloop is usually clearer when every element of a collection must be visited.
Swift Repeat-While Loop Editorial QA Checklist
- Confirm that each example demonstrates post-condition loop behavior.
- Verify that the loop body executes once when the initial condition would be false.
- Check that every repeat-while example updates the values used in its condition.
- Review
continuepaths to ensure they cannot accidentally create an infinite loop. - Confirm that
breakexits the intended loop and that execution resumes at the correct statement.
Swift Repeat-While Loop FAQs
Does a Swift repeat-while loop always run once?
Yes. Swift executes the repeat block before evaluating the condition, so the loop body runs at least once.
What is the Swift equivalent of a do-while loop?
Swift uses the repeat-while statement instead of the do-while syntax found in several other programming languages.
How do I stop a repeat-while loop in Swift?
Update the values used by the condition until it becomes false, or use break to exit the loop immediately.
Can continue be used inside a Swift repeat-while loop?
Yes. The continue statement skips the rest of the current loop body and moves execution to the condition check. Ensure that any required control-variable update occurs before continue.
When should I use repeat-while instead of while in Swift?
Use repeat-while when the action must happen before its result or continuation condition can be checked. Use while when the condition should be verified before the first execution.
Swift Repeat-While Loop Summary
A Swift repeat-while loop is a post-condition loop: it executes its body first and evaluates its condition afterward. It therefore runs at least once. Use break for an immediate exit, continue to skip the remainder of an iteration, and a regular while loop when the condition must be checked before execution. The official Swift Control Flow documentation provides the language reference for loops and control-transfer statements.
In this Swift Tutorial, we have learned about Swift Repeat While Loop with syntax and examples covering different scenarios.
TutorialKart.com