create a for loop in swift

To create a for loop in Swift, you can use the following syntax:

main.swift
for index in 0..<5 {
    // Code to be executed
}
50 chars
4 lines

In the above example, the loop will iterate over the range 0..<5, which includes the values 0 up to (but not including) 5. The index variable is assigned the value of each iteration in turn, and the code within the loop will be executed once for each value.

You can also use the for-in loop to iterate over arrays, dictionaries, and other collections in Swift. For example:

main.swift
let numbers = [1, 2, 3, 4, 5]
for number in numbers {
    print(number)
}
74 chars
5 lines

In this example, the loop will iterate over the numbers array and print each value to the console.

gistlibby LogSnag