import Foundation
let numbers = [5, 1, 6, 2, 4, 3]
print(numbers.min) // 1
print(numbers.max) // 6
В общем, цикл здесь не нужен.
Но если очень хочется цикл:
var minNumber: Int = 0
var maxNumber: Int = 0
for number in numbers {
if number < minNumber {
minNumber = number
}
else if maxNumber < number {
maxNumber = number
}
}
print(minNumber) // 1
print(maxNumber) // 6
Или:
var minNumber: Int = 0
var maxNumber: Int = 0
numbers.forEach { number in
if number < minNumber {
minNumber = number
}
else if maxNumber < number {
maxNumber = number
}
}
print(minNumber) // 1
print(maxNumber) // 6