0
0
Javaprogramming~15 mins

Enhanced for loop in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Enhanced for loop
📖 Scenario: You are organizing a small library system. You have a list of book titles that you want to display one by one to the user.
🎯 Goal: Learn how to use the enhanced for loop in Java to go through each item in a list and print it.
📋 What You'll Learn
Create a String array called books with exact titles
Create an integer variable count to count books
Use an enhanced for loop with variable book to iterate over books
Print each book title and the total count of books
💡 Why This Matters
🌍 Real World
Libraries, bookstores, or any system that manages lists of items can use enhanced for loops to process and display data easily.
💼 Career
Understanding enhanced for loops is essential for Java developers to write clean and readable code when working with collections or arrays.
Progress0 / 4 steps
1
Create the list of books
Create a String array called books with these exact titles: "Java Basics", "Data Structures", "Algorithms", "Design Patterns".
Java
Need a hint?

Use curly braces {} to list the book titles inside the array.

2
Add a counter variable
Add an integer variable called count and set it to 0 inside the main method, after the books array.
Java
Need a hint?

Use int count = 0; to start counting from zero.

3
Use enhanced for loop to count books
Use an enhanced for loop with variable book to iterate over the books array. Inside the loop, increase count by 1.
Java
Need a hint?

Use for (String book : books) to loop through each book.

4
Print each book and total count
Inside the enhanced for loop, add a System.out.println(book); statement to print each book title. After the loop, print the total number of books using System.out.println("Total books: " + count);.
Java
Need a hint?

Use System.out.println(book); inside the loop and print the count after the loop.