0
0
Kotlinprogramming~15 mins

For loop over collections in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
For loop over collections
📖 Scenario: You work at a small bookstore. You have a list of book titles that you want to display one by one to your customers.
🎯 Goal: Learn how to use a for loop in Kotlin to go through each item in a list and print it.
📋 What You'll Learn
Create a list of book titles with exact values
Create a variable to count the number of books
Use a for loop with variable book to iterate over the list
Print each book title inside the loop
Print the total number of books after the loop
💡 Why This Matters
🌍 Real World
Bookstores and libraries often need to show lists of books to customers or users. Looping through collections helps display or process each item easily.
💼 Career
Knowing how to loop over collections is a basic skill for any programmer. It helps in tasks like processing data, generating reports, or building user interfaces.
Progress0 / 4 steps
1
Create a list of book titles
Create a list called books with these exact titles: "The Hobbit", "1984", "Pride and Prejudice", "To Kill a Mockingbird".
Kotlin
Need a hint?

Use val books = listOf(...) to create the list with the exact book titles.

2
Create a counter variable
Create a variable called count and set it to 0 to count the number of books.
Kotlin
Need a hint?

Use var count = 0 to create a variable that can change.

3
Use a for loop to print each book
Use a for loop with variable book to go through each item in books. Inside the loop, print the book and increase count by 1.
Kotlin
Need a hint?

Use for (book in books) { ... } to loop. Use println(book) to print each title. Use count++ to add one to count.

4
Print the total number of books
After the loop, print the text Total books: followed by the value of count using a Kotlin println statement with string interpolation.
Kotlin
Need a hint?

Use println("Total books: $count") to show the total count after the loop.