Bird
Raised Fist0
Kotlinprogramming~10 mins

While and do-while loops in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - While and do-while loops
Initialize counter
↓
Check condition
↓
Execute body
↓
Update counter
↩Back to Check
The loop starts by checking a condition. If true, it runs the code inside, updates variables, then checks again. If false, it stops.
Execution Sample
Kotlin
var i = 1
while (i <= 3) {
  println(i)
  i++
}

var j = 1
do {
  println(j)
  j++
} while (j <= 3)
This code prints numbers 1 to 3 using a while loop and then again using a do-while loop.
Execution Table
StepVariable iCondition i <= 3ActionOutput
11truePrint i=1, i++1
22truePrint i=2, i++2
33truePrint i=3, i++3
44falseExit loop
💡 i reaches 4, condition 4 <= 3 is false, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
i12344
j12344
Key Moments - 2 Insights
Why does the while loop sometimes not run at all?
Because the condition is checked before the loop body. If false at start (see Step 4 in execution_table), the loop body never runs.
Why does the do-while loop always run at least once?
Because the loop body runs first, then the condition is checked. So even if condition is false initially, body runs once.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i at Step 3?
A4
B2
C3
D1
💡 Hint
Check the 'Variable i' column at Step 3 in execution_table
At which step does the while loop stop running?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look at the 'Condition i <= 3' column and see when it becomes false
If the initial value of i was 5, what would happen to the while loop?
AIt does not run at all
BIt runs once
CIt runs three times
DIt runs infinitely
💡 Hint
Refer to key_moments about condition checked before loop body
Concept Snapshot
while (condition) {
  // code runs while condition is true
}

- Condition checked before loop body
- May run zero or more times

do {
  // code runs at least once
} while (condition)

- Condition checked after loop body
- Runs one or more times
Full Transcript
This lesson shows how while and do-while loops work in Kotlin. The while loop checks the condition first. If true, it runs the code inside and updates variables, then checks again. If false, it stops immediately. The do-while loop runs the code first, then checks the condition. This means do-while always runs at least once. We traced a simple example printing numbers 1 to 3. The variable i starts at 1 and increases each loop. The while loop stops when i becomes 4 because 4 <= 3 is false. Key points: while loop may not run if condition is false at start; do-while runs once no matter what. The quiz asks about variable values and loop stopping points to reinforce understanding.

Practice

(1/5)
1. What is the main difference between a while loop and a do-while loop in Kotlin?
easy
A. while loops are faster than do-while loops.
B. while runs code once before checking; do-while checks the condition before running code.
C. while checks the condition before running the code; do-while runs code once before checking.
D. while loops only run once; do-while loops run multiple times.

Solution

  1. Step 1: Understand while loop behavior

    A while loop checks its condition before running the code inside it, so if the condition is false initially, the code inside won't run at all.
  2. Step 2: Understand do-while loop behavior

    A do-while loop runs the code inside once first, then checks the condition to decide if it should repeat.
  3. Final Answer:

    while checks the condition before running the code; do-while runs code once before checking. -> Option C
  4. Quick Check:

    Loop condition check timing = while checks the condition before running the code; do-while runs code once before checking. [OK]
Hint: Remember: do-while runs code once before condition check [OK]
Common Mistakes:
  • Thinking both loops check condition before running code
  • Confusing which loop runs code first
  • Assuming do-while runs only once
2. Which of the following is the correct syntax for a do-while loop in Kotlin?
easy
A. do { /* code */ } while (condition)
B. while (condition) { /* code */ } do
C. do (condition) { /* code */ } while
D. while { /* code */ } do (condition)

Solution

  1. Step 1: Recall correct do-while syntax

    The do-while loop syntax starts with do, then the code block in braces, followed by while with the condition in parentheses.
  2. Step 2: Compare options to syntax

    do { /* code */ } while (condition) matches the correct syntax exactly: do { /* code */ } while (condition). Others have wrong order or misplaced keywords.
  3. Final Answer:

    do { /* code */ } while (condition) -> Option A
  4. Quick Check:

    Syntax order for do-while = do { /* code */ } while (condition) [OK]
Hint: Remember: do { } while(condition) is the correct order [OK]
Common Mistakes:
  • Placing while before do
  • Using parentheses incorrectly
  • Mixing up braces and parentheses
3. What will be the output of this Kotlin code?
var count = 3
while (count > 0) {
    println(count)
    count--
}
medium
A. 3 2 1
B. 1 2 3
C. 3 2 1 0
D. 0 1 2 3

Solution

  1. Step 1: Analyze initial value and condition

    The variable count starts at 3. The loop runs while count > 0, so it runs for 3, 2, and 1.
  2. Step 2: Trace each iteration and output

    Each loop prints the current count then decreases it by 1. So it prints 3, then 2, then 1, then stops before printing 0.
  3. Final Answer:

    3 2 1 -> Option A
  4. Quick Check:

    Loop prints count down from 3 to 1 = 3 2 1 [OK]
Hint: Count down loops print from start to 1, not zero [OK]
Common Mistakes:
  • Including 0 in output
  • Printing in ascending order
  • Not decrementing count
4. Identify the error in this Kotlin code snippet:
var i = 1
do {
    println(i)
    i++
} while i <= 3
medium
A. Missing braces around the do block
B. Incorrect use of do keyword
C. Variable i should be declared inside the loop
D. Missing parentheses around the while condition

Solution

  1. Step 1: Check do-while syntax

    The while condition must be inside parentheses after the while keyword.
  2. Step 2: Identify the error in the code

    The code has while i <= 3 without parentheses. It should be while (i <= 3).
  3. Final Answer:

    Missing parentheses around the while condition -> Option D
  4. Quick Check:

    Parentheses required for while condition = Missing parentheses around the while condition [OK]
Hint: Always put condition in parentheses after while [OK]
Common Mistakes:
  • Omitting parentheses in while condition
  • Placing variable declaration inside loop unnecessarily
  • Confusing braces and parentheses
5. You want to write a Kotlin program that asks the user to enter a positive number and keeps asking until the user enters a number greater than zero. Which loop should you use and why?
hard
A. Use a while loop because it checks the condition before asking the user.
B. Use a do-while loop because it asks the user at least once before checking the condition.
C. Use a while loop because it runs the code at least once.
D. Use a do-while loop because it never checks the condition.

Solution

  1. Step 1: Understand the problem requirement

    The program must ask the user at least once to enter a number, then repeat if the number is not positive.
  2. Step 2: Choose the correct loop type

    A do-while loop runs the code block once before checking the condition, so it ensures the user is asked at least once.
  3. Final Answer:

    Use a do-while loop because it asks the user at least once before checking the condition. -> Option B
  4. Quick Check:

    Ask user once then check = do-while loop = Use a do-while loop because it asks the user at least once before checking the condition. [OK]
Hint: Use do-while to run code once before condition check [OK]
Common Mistakes:
  • Choosing while loop which may skip asking once
  • Thinking do-while never checks condition
  • Confusing loop behavior with input requirements
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