Kotlin While and Do While
Repeat a task while an explicit amount of work remains.
How it works
while checks its condition before the body; do-while checks afterward and runs at least once. Each loop needs progress toward termination. These forms suit state-based repetition better than a for loop when the number of iterations is not known up front.
Run this Kotlin example
These are local Kotlin/JVM programs. Install a JDK and the compiler using the official Kotlin compiler guide, or use an IDE with Kotlin support. Check java -version and kotlinc -version in a new terminal. The reviewed compiler is Kotlin 2.4.21 with a JVM 21 target, executed on Temurin JDK 25.0.5. Use JDK 21 or newer for this target. Android, Kotlin/JS and Kotlin/Native require separate platform projects and are not the tested environment here.
Save the complete source as while-do-while.kt in its own learning folder. The package line gives this example a namespace, not a network address. Bhylu has no Kotlin browser runner; do not use the JavaScript or Python editor. Apache, PHP and MySQL are not required.
Compile with kotlinc while-do-while.kt -jvm-target 21 -include-runtime -d lesson.jar, then run java -jar lesson.jar. Use a learning folder you own and keep generated JARs outside the website. These sources require no third-party application libraries.
Download the complete while-do-while.kt · Download all source files with hash checks
For the optional collection, save download-examples.mjs in a new learning folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/kotlin/. It refuses an existing bhylu-kotlin-examples folder and installs or executes no Kotlin code. Compile one ordinary lesson at a time by filename.
Example
package bhylu.lessons.k_while_do_while
fun main() {
var remaining = 3
while (remaining > 0) {
println(remaining)
remaining--
}
var checks = 0
do { checks++ } while (checks < 1)
println("checks=$checks")
}
Expected result
3 2 1 checks=1
remaining decreases every time, so the first loop reaches zero. The second loop demonstrates the guaranteed first pass without inventing input to make it run.
For exact language and library contracts, consult the official Kotlin documentation. These fictional fixtures are learning programs, not a deployed hire service.