I think the answer is yes.
1...n - 1 represents a Range object. It is a literal of Range. Therefore, when compilation reaches the loop and sees the literal, it thinks
It seems like that you want to create a new Range<Int> object! Here you go! Hmm... So I guess i is of type Int...
and so on.
This means that n - 1 is evaluated when you create the object. And it stays that way, not evaluating it a second time. This code proves it by not printing only one hello:
var n = 10
for i in 1...n - 1 {
n = 2
print("Hello")
}
So yeah.
Note:
- It's better to use
1..<n instead of 1...n - 1 in this case, they are the same.
- Apple actually recommends you to use this approach instead of the C-style for loop.
- The C-style for loop will be removed in Swift 3
nby some functionn()and check how often the function is called ...