Using a While Loop in R
📖 Scenario: You are helping a cashier count the total number of coins collected in a jar. Each coin is worth 1 unit. The cashier wants to keep adding coins until the total reaches at least 10 units.
🎯 Goal: Build a program that uses a while loop to count coins until the total is 10 or more.
📋 What You'll Learn
Create a variable
total_coins starting at 0Create a variable
coins_to_add set to 1Use a
while loop with the condition total_coins < 10Inside the loop, add
coins_to_add to total_coinsPrint the final value of
total_coins💡 Why This Matters
🌍 Real World
Counting items or accumulating totals until a goal is reached is common in cash registers, inventory systems, and games.
💼 Career
Understanding loops helps automate repetitive tasks and process data efficiently in many programming jobs.
Progress0 / 4 steps