To create a for loop in Swift, you can use the following syntax:
main.swift50 chars4 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.swift74 chars5 lines
In this example, the loop will iterate over the numbers
array and print each value to the console.
gistlibby LogSnag