0
0
Kotlinprogramming~15 mins

Repeat function for simple repetition in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Repeat function for simple repetition
📖 Scenario: You want to greet your friends multiple times to make them feel special. Instead of writing the greeting many times, you will use Kotlin's repeat function to say hello several times easily.
🎯 Goal: Build a Kotlin program that uses the repeat function to print a greeting message multiple times.
📋 What You'll Learn
Create a variable called times with the value 3.
Use the repeat function with the variable times to print "Hello, friend!" exactly 3 times.
💡 Why This Matters
🌍 Real World
Repeating actions many times is common in apps, like showing notifications or processing lists.
💼 Career
Understanding how to repeat tasks efficiently helps you write cleaner and shorter code in real projects.
Progress0 / 4 steps
1
Create the times variable
Create a variable called times and set it to 3.
Kotlin
Need a hint?

Use val times = 3 to create the variable.

2
Use repeat to print greetings
Use the repeat function with the variable times to print "Hello, friend!" exactly 3 times.
Kotlin
Need a hint?

Use repeat(times) { println("Hello, friend!") } to print the message multiple times.

3
Add a message count inside the repeat
Modify the repeat block to print the message number before each greeting, like "Greeting 1: Hello, friend!".
Kotlin
Need a hint?

Use repeat(times) { count -> println("Greeting ${count + 1}: Hello, friend!") } to include the count.

4
Print the final greetings
Run the program to print the greetings with their numbers using println.
Kotlin
Need a hint?

Just run the program to see the greetings printed.