From the course: Kotlin Essential Training: Functions, Collections, and I/O

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

while loops

while loops

- [Instructor] In nearly any complex application the need will arise to perform some repeated operation. This could be repeating a task a certain number of times or repeating for every item in some range or a collection. Let's explore one mechanism for this type of repeated operation, the while loop. While loops in kotlin behave very similarly to other programming languages. First, we need to define some counter value. So in this case, we'll create a variable called counter and assign it the value of zero to start. Next we set up the looping condition. In this example, we'll set up our loop to run five times by checking whether our counter is less than five or not. So we can do that by using the while keyword and then our condition in this case is going to be, counter less than five, and then within open and close curly braces here we can define what we want to do on each iteration of the loop. If we were to run this code…

Contents