ProgramGuru

While and do-while loops

While and do-while loops

Some repetition depends on a condition rather than a known list. A while loop checks its condition before each body execution. A do-while loop checks after the body, so it runs at least once. This distinction matters for retries and input prompts.

While checks before its body. Do-while checks after its body and therefore runs at least once. Identify which state changes toward termination; a condition that stays true indefinitely creates an infinite loop even though the code compiles.

What you’ll learn

  • Compare before-body and after-body condition checks.
  • Identify progress toward loop termination.

While and do-while loops in the example

  1. Check remaining

    Some repetition depends on a condition rather than a known list.

    Check remaining
  2. Print and decrement

    While checks before its body.

    Print and decrement
  3. Stop at zero

    The program prints three, two and one, decrementing after each display.

    Stop at zero
The program prints three, two and one, decrementing after each display. At zero the next condition fails and Ready prints. No timer or asynchronous waiting is being simulated.

Follow while and do-while loops

Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.

Kotlin/JVM · complete program

fun main() {
  var remaining = 3
  while (remaining > 0) {
    println(remaining)
    remaining--
  }
  println("Ready")
}

Expected output

3
2
1
Ready

Follow the execution

The program prints three, two and one, decrementing after each display. At zero the next condition fails and Ready prints. No timer or asynchronous waiting is being simulated.

Try this in the Kotlin compiler by pasting the complete program.

Practice: While and do-while loops

Your task: What happens if remaining starts at zero?

Kotlin/JVM · complete program

fun main() {
  var remaining = 3
  while (remaining > 0) {
    println(remaining)
    remaining--
  }
  println("Ready")
}

Worked answer

The while body is skipped and Ready prints immediately. A do-while body would execute once before testing.

While and do-while loops: the important boundary

Retries need bounds and a failure policy. Repeated polling without waiting or an event mechanism can waste resources. This counter demonstrates control flow rather than a production waiting mechanism.

Check your understanding

What happens if remaining starts at zero?
Choose one answer. You can try again.

Answer and explanation

The while body is skipped and Ready prints immediately.

The while body is skipped and Ready prints immediately. A do-while body would execute once before testing.

While and do-while loops: what to keep in mind

The while body is skipped and Ready prints immediately. A do-while body would execute once before testing. Retries need bounds and a failure policy.

Further reading: official Kotlin documentation for this topic.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Arrays and indexed access →