0
0
Rubyprogramming~15 mins

While loop in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
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 10
Create a variable called count starting at 1
Use a while loop with the condition count <= total_items
Inside the loop, print the current count value
Increase count by 1 inside the loop
After 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
1
Set up the total number of items
Create a variable called total_items and set it to 10.
Ruby
Need a hint?

Use = to assign the number 10 to total_items.

2
Create a counter variable
Create a variable called count and set it to 1.
Ruby
Need a hint?

Start counting from 1 by assigning 1 to count.

3
Write the while loop to count items
Write a while loop with the condition count <= total_items. Inside the loop, print the current count value, then increase count by 1.
Ruby
Need a hint?

Use while count <= total_items to start the loop. Use puts count to print, and count += 1 to add one to count.

4
Print the final message after counting
After the while loop, print the message "Counting finished: 10 items counted.".
Ruby
Need a hint?

Use puts to print the final message after the loop ends.