While

Just as if is fairly common in programming languages, so is while or some other loop that is based on evaluating a condition.

So we have while in Java:

int i = 0;

while (i < 10) {
  i++;
}

…and JavaScript:

var i = 0;

while (i < 10) {
  i++;
}

…and Ruby:

i = 0

while i < 10 do
  i += 1
end

Kotlin offers much the same thing:

  var i = 0

  while (i < 10) {
    i++
    println(i)
  }

However, while is not as common in Kotlin as it is in other languages. More often we use operations on collections (e.g., forEach()) rather than while. However, it exists, should you find the need for it.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.