We all would have written a piece of code similar to this, The usual way,

let items = [1,2,3,4]
for item in items {
    if(item % 2 == 0){
        print(item)
    }
}
Result: 2 4 

As a Swift developer we always want to write better code. In Swift, we can use the “where” clause along with the for loop. The Swift way,

for item in items where item % 2 == 0{
    print(item)
}
Result: 2 4

Neat isn’t it? Playground sample,

Playground sample

Edit — Thanks to Md. Ibrahim Hassan for giving a better sample code. Follow me on Twitter. Have any queries? Feel free to DM me.