While Loop in Kotlin

Level 259 keystrokes

Counts down from 3 to 1 with a while loop, then prints "Done!".

Kotlin code

// var is needed because n changes inside the loop
1var n = 3
// Repeats the block while n is greater than 0
2while (n > 0) {
// Prints the current value: 3, 2, 1
3 println(n)
// Decrease n by 1; the loop ends when n reaches 0
4 n--
5}
// Runs once after the loop has finished
6println("Done!")

Output

3
2
1
Done!
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Kotlin code and how often they appear.

  • (× 3
  • )× 3
  • -× 2
  • "× 2
  • !× 1
  • {× 1
  • }× 1
  • =× 1
  • >× 1

More Kotlin examples at level 2

← Kotlin typing practice