Bird
Raised Fist0
Kotlinprogramming~10 mins

Repeat function for simple repetition in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Repeat function for simple repetition
Start
↓
Call repeat(n)
↓
Initialize counter i=0
↓
Check i < n?
No→End repeat
Yes↓
Execute block
↓
Increment i
↩Back to Check
The repeat function runs a block of code n times by counting from 0 to n-1 and executing the block each time.
Execution Sample
Kotlin
repeat(3) {
    println("Hello")
}
This code prints "Hello" three times using repeat.
Execution Table
Iterationi (counter)Condition i < 3ActionOutput
10trueExecute blockHello
21trueExecute blockHello
32trueExecute blockHello
43falseExit repeat
💡 i reaches 3, condition 3 < 3 is false, so repeat stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
i-0123
Key Moments - 2 Insights
Why does the repeat function start counting from 0 instead of 1?
The repeat function uses zero-based counting, so i starts at 0 and goes up to n-1. This is shown in the execution_table where i goes 0,1,2 for repeat(3).
What happens when the counter i equals n?
When i equals n (3 in this case), the condition i < n becomes false, so the repeat loop stops. This is the exit condition shown in the last row of execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i during the third iteration?
A2
B3
C1
D0
💡 Hint
Check the 'i (counter)' column in the third row of execution_table.
At which iteration does the repeat function stop executing the block?
AAfter iteration 2
BAfter iteration 4
CAfter iteration 3
DAfter iteration 1
💡 Hint
Look at the 'Condition i < 3' column and see when it becomes false in execution_table.
If repeat(5) was used instead, how many times would the block execute?
A3 times
B4 times
C5 times
D6 times
💡 Hint
repeat(n) runs the block exactly n times, as shown in variable_tracker and execution_table.
Concept Snapshot
repeat(n) { block }
- Runs block n times
- Counter i goes from 0 to n-1
- Stops when i == n
- Simple way to repeat code without loops
Full Transcript
The Kotlin repeat function runs a block of code a set number of times. It starts counting from zero and runs the block while the counter is less than n. When the counter reaches n, it stops. For example, repeat(3) runs the block three times with i values 0, 1, and 2. This is a simple way to repeat actions without writing a full loop.

Practice

(1/5)
1. What does the Kotlin repeat function do?
easy
A. Defines a loop that runs until a condition is false
B. Repeats a string multiple times
C. Runs a block of code a specified number of times
D. Creates a list with repeated elements

Solution

  1. Step 1: Understand the purpose of repeat

    The repeat function is designed to run a block of code a fixed number of times.
  2. Step 2: Compare with other options

    Options B and C describe string or list repetition, which are different functions. Defines a loop that runs until a condition is false describes a conditional loop, not repeat.
  3. Final Answer:

    Runs a block of code a specified number of times -> Option C
  4. Quick Check:

    repeat = run code N times [OK]
Hint: Remember: repeat runs code block N times [OK]
Common Mistakes:
  • Confusing repeat with string repetition
  • Thinking repeat creates collections
  • Mixing repeat with while or for loops
2. Which of the following is the correct syntax to repeat a print statement 3 times in Kotlin?
easy
A. repeat 3 { println("Hello") }
B. repeat(3) { println("Hello") }
C. repeat(3); println("Hello")
D. repeat {3} println("Hello")

Solution

  1. Step 1: Check the function call syntax

    The repeat function requires parentheses around the number of repetitions and a lambda block with curly braces.
  2. Step 2: Validate each option

    repeat(3) { println("Hello") } correctly uses repeat(3) { ... }. Options B, C, and D have syntax errors or misplaced braces.
  3. Final Answer:

    repeat(3) { println("Hello") } -> Option B
  4. Quick Check:

    repeat(number) { code } syntax [OK]
Hint: Use parentheses for count and braces for code block [OK]
Common Mistakes:
  • Omitting parentheses around the count
  • Using semicolon instead of braces
  • Placing braces incorrectly
3. What will be the output of this Kotlin code?
repeat(4) { i -> println(i * 2) }
medium
A. 0 1 2 3
B. 2 4 6 8
C. 1 2 3 4
D. 0 2 4 6

Solution

  1. Step 1: Understand the index parameter

    The lambda receives i from 0 to 3 (4 times). Each iteration prints i * 2.
  2. Step 2: Calculate each output line

    For i=0: 0*2=0, i=1: 2, i=2: 4, i=3: 6. So output lines are 0, 2, 4, 6.
  3. Final Answer:

    0 2 4 6 -> Option D
  4. Quick Check:

    repeat with index * 2 = 0 2 4 6 [OK]
Hint: Index starts at 0 in repeat lambda [OK]
Common Mistakes:
  • Starting index at 1 instead of 0
  • Forgetting to multiply by 2
  • Confusing output with index values
4. Identify the error in this Kotlin code:
repeat(3) 
 println("Hi")
medium
A. Missing curly braces for the code block
B. repeat cannot be used with println
C. repeat requires a semicolon after the number
D. The number 3 should be a string

Solution

  1. Step 1: Check the syntax of repeat

    The repeat function expects a lambda block enclosed in curly braces after the count.
  2. Step 2: Identify the missing braces

    The code has no braces around println("Hi"), so it is a syntax error.
  3. Final Answer:

    Missing curly braces for the code block -> Option A
  4. Quick Check:

    repeat needs braces { } for code [OK]
Hint: Always use braces { } after repeat(count) [OK]
Common Mistakes:
  • Omitting braces for single statement
  • Adding semicolon after number
  • Passing number as string
5. How can you use repeat to create a list of squares of numbers from 0 to 4 in Kotlin?
hard
A. val squares = mutableListOf<Int>(); repeat(5) { squares.add(it * it) }
B. val squares = repeat(5) { it * it }
C. val squares = List(5) { repeat(it * it) }
D. val squares = repeat(5) { println(it * it) }

Solution

  1. Step 1: Understand repeat usage for list building

    The repeat function runs code multiple times but does not return a list itself.
  2. Step 2: Check each option's approach

    val squares = mutableListOf<Int>(); repeat(5) { squares.add(it * it) } creates a mutable list and adds squares inside repeat, which works. val squares = repeat(5) { it * it } tries to assign repeat's result directly, which is Unit (no list). val squares = List(5) { repeat(it * it) } misuses List and repeat. val squares = repeat(5) { println(it * it) } only prints values, no list created.
  3. Final Answer:

    val squares = mutableListOf<Int>(); repeat(5) { squares.add(it * it) } -> Option A
  4. Quick Check:

    repeat + add to list = correct list [OK]
Hint: Use repeat to add items to a mutable list [OK]
Common Mistakes:
  • Expecting repeat to return a list
  • Using repeat inside List constructor incorrectly
  • Only printing values without storing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard