For-in vs For-each in Swift

For-in and for-each are different variants of for loops in swift which are used to iterate over a range, set or dictionary. Both provide the same functionality but has a few limitations or differences when it comes to conditional access.

To understand their differences, let’s try to understand their examples in details first.

For-in loop

For-in loop is used to iterate over a range, set or dictionary using both the indexes as well an element based iteration.

For-each loop

For-each loop can also be used to iterate over a range, set or dictionary using both the indexes as well an element based iteration.

Difference between for-in and for-each

1. Break and continue statements cannot be used in for-each loop

break and continue are the basic syntaxes of for loop which is used to either break the loop iteration or to continue to the next element.
Since for-each is not an operator but it’s a function, break and continue can’t be used inside a for-each iteration.

Please continue reading at https://agrawalsuneet.github.io/blogs/for-in-vs-for-each-in-swift/

That’s all for now. You can read my other interesting blogs here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store