Using a While Loop in Ruby
📖 Scenario: You are helping a small shop count how many items they have in stock. The shop owner wants to count items one by one until they reach the total number of items.
🎯 Goal: Build a Ruby program that uses a while loop to count items from 1 up to a total number and then shows the final count.
📋 What You'll Learn
Create a variable called
total_items with the value 10Create a variable called
count starting at 1Use a
while loop with the condition count <= total_itemsInside the loop, print the current
count valueIncrease
count by 1 inside the loopAfter the loop, print
"Counting finished: 10 items counted."💡 Why This Matters
🌍 Real World
Counting items one by one is common in shops, inventories, or any place where you need to process things step by step.
💼 Career
Understanding loops is essential for automating repetitive tasks in programming jobs, such as data processing or user input handling.
Progress0 / 4 steps