From the course: Build Spring Boot Apps with the Kotlin Programming Language

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Using for to loop through a collection

Using for to loop through a collection

- [Instructor] So let's then look at the for loop. Now in Java, the for loop has two slightly different syntaxes. We can either create a for loop providing a local variable, and then modifying that variable until some expression is reached to be true. This is the way we do a typical counter, or we can say for each, something in a collection. So in this example, for each string in a collection of strings, do something with each string. Now in Kotlin, only the second version of this loop exists. So in this chapter, we'll first of all look at this second version, working out how the looping through a collection works, and we'll understand the differences in Kotlin which are really just syntax related and then we'll spend most of the chapter, looking at how you can achieve the same as this version, the first versions, given that that structure for looping isn't available in Kotlin. Okay, so let's start with looping through a collection, and actually let's be a bit more precise, we can use…

Contents