For loop (rarely used in Ruby)
📖 Scenario: You are helping a small bookstore organize its inventory. The store wants to list the names of some popular books it has in stock.
🎯 Goal: You will create a list of book titles, set up a counter for counting books, use a for loop to go through each book, and finally print each book's name.
📋 What You'll Learn
Create an array called
books with the exact titles: 'The Hobbit', '1984', 'Pride and Prejudice'Create a variable called
count and set it to 0Use a
for loop with the variable book to iterate over booksInside the loop, increment
count by 1Inside the loop, print the current
bookAfter the loop, print the total number of books using
count💡 Why This Matters
🌍 Real World
Bookstores and libraries often need to list and count books in their inventory. Using loops helps automate this task.
💼 Career
Knowing how to loop through lists and count items is a basic skill for many programming jobs, including data processing and software development.
Progress0 / 4 steps