Swift 'for' loop with enumerated() and zip()
'for' loop is used every where in programming languages as the part of Control flow statements.
Consider names of few users, I have initialized them below.
Simple for Loop
A simple 'for' loop iterating through the items is listed below.
So, lets correct it now and use a much shorter syntax.
Output:
for Loop with Counter
Now what if I want the counter to be also accessible within the same loop. That counter can be used to perform some operation , let's say a simple example of even or odd counter.
Using enumerated() function used with array , creates a sequence of (n,x) where n is consecutive Int starting from zero and x is the item which in this case would be a String Object.
for Loop with Index
One more simple variation where start index will be defined from 0 to any other start value.
Consider names of few users, I have initialized them below.
let names = ["John","Fred","Maria","Rose"]
Simple for Loop
A simple 'for' loop iterating through the items is listed below.
for var i = 0; i < names.count; i++ {
print("Name=\(names[i])")
}
This will result in compile time error in Swift 3 onwards. Below is the compile time error "C-style for statement has been removed in Swift 3"So, lets correct it now and use a much shorter syntax.
for item in names {
print("Name = \(item)")
}
Output:
Name = John
Name = Fred
Name = Maria
Name = Rose
for Loop with Counter
Now what if I want the counter to be also accessible within the same loop. That counter can be used to perform some operation , let's say a simple example of even or odd counter.
Using enumerated() function used with array , creates a sequence of (n,x) where n is consecutive Int starting from zero and x is the item which in this case would be a String Object.
for (index,name) in names.enumerated() {
if index%2 == 0 {
print ("Even Counter Assignment \(index) => name=\(name)")
} else {
print ("Odd Counter Assignment \(index) => name=\(name)")
}
}
Output:
Output:
Even Counter Assignment 0 => name=John
Odd Counter Assignment 1 => name=Fred
Even Counter Assignment 2 => name=Maria
Odd Counter Assignment 3 => name=Rose
for Loop with Index
One more simple variation where start index will be defined from 0 to any other start value.
let nameIndices = 9...12
for(i,name) in zip(nameIndices,names ) {
print ("name=\(name) at index= \(i)")
}
Output:
Output:
name=John at index= 9
name=Fred at index= 10
name=Maria at index= 11
name=Rose at index= 12
We can interchange the sequence parameters, meaning
zip(names,nameIndices) is valid too. Only thing changes is the sequence parameters which changes . If you notice "name" object is now first and index "i" object is the second.
We can interchange the sequence parameters, meaning
zip(names,nameIndices) is valid too. Only thing changes is the sequence parameters which changes . If you notice "name" object is now first and index "i" object is the second.
for(name,i) in zip(names,nameIndices ) {
print ("name=\(name) at index= \(i)")
}
Comments
Post a Comment