0
0
Kotlinprogramming~15 mins

For loop with ranges in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
For loop with ranges
📖 Scenario: You are helping a small bakery count how many cupcakes they have baked each day over a week.
🎯 Goal: Build a Kotlin program that uses a for loop with a range to count cupcakes baked from day 1 to day 7.
📋 What You'll Learn
Create a variable called totalCupcakes and set it to 0
Create a for loop using day in the range 1 to 7
Inside the loop, add 10 cupcakes for each day to totalCupcakes
Print the final value of totalCupcakes
💡 Why This Matters
🌍 Real World
Counting items or events over a fixed number of days or steps is common in business and daily life.
💼 Career
Understanding loops and ranges helps automate repetitive tasks and process data efficiently in software development.
Progress0 / 4 steps
1
Create the initial variable
Create a variable called totalCupcakes and set it to 0.
Kotlin
Need a hint?

Use var to create a variable that can change.

2
Set up the for loop with range
Create a for loop with variable day that goes from 1 to 7 using a range.
Kotlin
Need a hint?

Use 1..7 to create a range from 1 to 7 inclusive.

3
Add cupcakes inside the loop
Inside the for loop, add 10 to totalCupcakes for each day.
Kotlin
Need a hint?

Use += to add to the existing value of totalCupcakes.

4
Print the total cupcakes
Write a println statement to print the value of totalCupcakes.
Kotlin
Need a hint?

Use println(totalCupcakes) to show the total cupcakes baked.